users.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. from fastapi import APIRouter, Depends, HTTPException, Query, status
  2. from sqlalchemy import delete, func, select
  3. from sqlalchemy.ext.asyncio import AsyncSession
  4. from sqlalchemy.orm import selectinload
  5. from backend.app.core.auth import (
  6. RequirePermissionIfAuthEnabled,
  7. get_current_user_optional,
  8. get_password_hash,
  9. verify_password,
  10. )
  11. from backend.app.core.database import get_db
  12. from backend.app.core.permissions import Permission
  13. from backend.app.models.archive import PrintArchive
  14. from backend.app.models.group import Group
  15. from backend.app.models.library import LibraryFile
  16. from backend.app.models.print_queue import PrintQueueItem
  17. from backend.app.models.settings import Settings
  18. from backend.app.models.user import User
  19. from backend.app.schemas.auth import ChangePasswordRequest, GroupBrief, UserCreate, UserResponse, UserUpdate
  20. from backend.app.services.email_service import (
  21. create_welcome_email,
  22. generate_secure_password,
  23. get_smtp_settings,
  24. send_email,
  25. )
  26. from backend.app.api.routes.settings import get_external_login_url
  27. router = APIRouter(prefix="/users", tags=["users"])
  28. def _user_to_response(user: User) -> UserResponse:
  29. """Convert a User model to UserResponse schema."""
  30. return UserResponse(
  31. id=user.id,
  32. username=user.username,
  33. email=user.email,
  34. role=user.role,
  35. is_active=user.is_active,
  36. is_admin=user.is_admin,
  37. groups=[GroupBrief(id=g.id, name=g.name) for g in user.groups],
  38. permissions=sorted(user.get_permissions()),
  39. created_at=user.created_at.isoformat(),
  40. )
  41. @router.get("", response_model=list[UserResponse])
  42. @router.get("/", response_model=list[UserResponse])
  43. async def list_users(
  44. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_READ),
  45. db: AsyncSession = Depends(get_db),
  46. ):
  47. """List all users."""
  48. result = await db.execute(select(User).options(selectinload(User.groups)).order_by(User.created_at))
  49. users = result.scalars().all()
  50. return [_user_to_response(user) for user in users]
  51. @router.post("", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
  52. @router.post("/", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
  53. async def create_user(
  54. user_data: UserCreate,
  55. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_CREATE),
  56. db: AsyncSession = Depends(get_db),
  57. ):
  58. """Create a new user.
  59. When advanced authentication is enabled:
  60. - Email is required
  61. - Password is auto-generated and emailed to user
  62. - Admin cannot set or see the password
  63. """
  64. import logging
  65. logger = logging.getLogger(__name__)
  66. # Check if advanced auth is enabled
  67. result = await db.execute(select(Settings).where(Settings.key == "advanced_auth_enabled"))
  68. advanced_auth_setting = result.scalar_one_or_none()
  69. advanced_auth_enabled = advanced_auth_setting and advanced_auth_setting.value.lower() == "true"
  70. # Check if username already exists (case-insensitive)
  71. existing_user = await db.execute(
  72. select(User).where(func.lower(User.username) == func.lower(user_data.username))
  73. )
  74. if existing_user.scalar_one_or_none():
  75. raise HTTPException(
  76. status_code=status.HTTP_400_BAD_REQUEST,
  77. detail="Username already exists",
  78. )
  79. # Validate role
  80. if user_data.role not in ["admin", "user"]:
  81. raise HTTPException(
  82. status_code=status.HTTP_400_BAD_REQUEST,
  83. detail="Role must be 'admin' or 'user'",
  84. )
  85. # Advanced auth validation
  86. if advanced_auth_enabled:
  87. if not user_data.email:
  88. raise HTTPException(
  89. status_code=status.HTTP_400_BAD_REQUEST,
  90. detail="Email is required when advanced authentication is enabled",
  91. )
  92. # Check if email already exists (case-insensitive)
  93. existing_email = await db.execute(
  94. select(User).where(func.lower(User.email) == func.lower(user_data.email))
  95. )
  96. if existing_email.scalar_one_or_none():
  97. raise HTTPException(
  98. status_code=status.HTTP_400_BAD_REQUEST,
  99. detail="Email already exists",
  100. )
  101. # Generate password if advanced auth enabled, otherwise require password
  102. if advanced_auth_enabled:
  103. password = generate_secure_password()
  104. else:
  105. if not user_data.password:
  106. raise HTTPException(
  107. status_code=status.HTTP_400_BAD_REQUEST,
  108. detail="Password is required when advanced authentication is disabled",
  109. )
  110. password = user_data.password
  111. new_user = User(
  112. username=user_data.username,
  113. email=user_data.email,
  114. password_hash=get_password_hash(password),
  115. role=user_data.role,
  116. is_active=True,
  117. )
  118. # Handle group assignments
  119. if user_data.group_ids:
  120. groups_result = await db.execute(select(Group).where(Group.id.in_(user_data.group_ids)))
  121. groups = groups_result.scalars().all()
  122. if len(groups) != len(user_data.group_ids):
  123. raise HTTPException(
  124. status_code=status.HTTP_400_BAD_REQUEST,
  125. detail="One or more group IDs are invalid",
  126. )
  127. new_user.groups = list(groups)
  128. db.add(new_user)
  129. await db.commit()
  130. await db.refresh(new_user)
  131. # Send welcome email if advanced auth enabled
  132. if advanced_auth_enabled and new_user.email:
  133. try:
  134. smtp_settings = await get_smtp_settings(db)
  135. if smtp_settings:
  136. login_url = await get_external_login_url(db)
  137. subject, text_body, html_body = create_welcome_email(new_user.username, password, login_url)
  138. send_email(smtp_settings, new_user.email, subject, text_body, html_body)
  139. logger.info(f"Welcome email sent to {new_user.email}")
  140. else:
  141. logger.warning(f"SMTP not configured, could not send welcome email to {new_user.email}")
  142. except Exception as e:
  143. logger.error(f"Failed to send welcome email: {e}")
  144. # Don't fail user creation if email fails
  145. return _user_to_response(new_user)
  146. @router.get("/{user_id}", response_model=UserResponse)
  147. async def get_user(
  148. user_id: int,
  149. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_READ),
  150. db: AsyncSession = Depends(get_db),
  151. ):
  152. """Get a user by ID."""
  153. result = await db.execute(select(User).where(User.id == user_id).options(selectinload(User.groups)))
  154. user = result.scalar_one_or_none()
  155. if not user:
  156. raise HTTPException(
  157. status_code=status.HTTP_404_NOT_FOUND,
  158. detail="User not found",
  159. )
  160. return _user_to_response(user)
  161. @router.patch("/{user_id}", response_model=UserResponse)
  162. async def update_user(
  163. user_id: int,
  164. user_data: UserUpdate,
  165. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_UPDATE),
  166. db: AsyncSession = Depends(get_db),
  167. ):
  168. """Update a user."""
  169. result = await db.execute(select(User).where(User.id == user_id).options(selectinload(User.groups)))
  170. user = result.scalar_one_or_none()
  171. if not user:
  172. raise HTTPException(
  173. status_code=status.HTTP_404_NOT_FOUND,
  174. detail="User not found",
  175. )
  176. # Prevent deactivating the last admin
  177. if user_data.is_active is False and user.is_admin:
  178. # Count admins by role or Administrators group membership
  179. admin_count_result = await db.execute(select(User).where(User.role == "admin", User.is_active.is_(True)))
  180. role_admins = admin_count_result.scalars().all()
  181. # Also check for users in Administrators group
  182. admin_group_result = await db.execute(
  183. select(Group).where(Group.name == "Administrators").options(selectinload(Group.users))
  184. )
  185. admin_group = admin_group_result.scalar_one_or_none()
  186. group_admins = [u for u in (admin_group.users if admin_group else []) if u.is_active]
  187. # Combine unique admins
  188. all_admins = {u.id for u in role_admins} | {u.id for u in group_admins}
  189. if len(all_admins) <= 1 and user.id in all_admins:
  190. raise HTTPException(
  191. status_code=status.HTTP_400_BAD_REQUEST,
  192. detail="Cannot deactivate the last admin user",
  193. )
  194. # Prevent changing role of last admin
  195. if user_data.role and user_data.role != "admin" and user.role == "admin":
  196. admin_count_result = await db.execute(select(User).where(User.role == "admin", User.is_active.is_(True)))
  197. admin_count = len(admin_count_result.scalars().all())
  198. if admin_count <= 1:
  199. raise HTTPException(
  200. status_code=status.HTTP_400_BAD_REQUEST,
  201. detail="Cannot change role of the last admin user",
  202. )
  203. if user_data.username is not None:
  204. # Check if new username already exists (case-insensitive)
  205. existing_user = await db.execute(
  206. select(User).where(func.lower(User.username) == func.lower(user_data.username), User.id != user_id)
  207. )
  208. if existing_user.scalar_one_or_none():
  209. raise HTTPException(
  210. status_code=status.HTTP_400_BAD_REQUEST,
  211. detail="Username already exists",
  212. )
  213. user.username = user_data.username
  214. if user_data.email is not None:
  215. # Check if new email already exists (case-insensitive)
  216. existing_email = await db.execute(
  217. select(User).where(func.lower(User.email) == func.lower(user_data.email), User.id != user_id)
  218. )
  219. if existing_email.scalar_one_or_none():
  220. raise HTTPException(
  221. status_code=status.HTTP_400_BAD_REQUEST,
  222. detail="Email already exists",
  223. )
  224. user.email = user_data.email
  225. if user_data.password is not None:
  226. user.password_hash = get_password_hash(user_data.password)
  227. if user_data.role is not None:
  228. if user_data.role not in ["admin", "user"]:
  229. raise HTTPException(
  230. status_code=status.HTTP_400_BAD_REQUEST,
  231. detail="Role must be 'admin' or 'user'",
  232. )
  233. user.role = user_data.role
  234. if user_data.is_active is not None:
  235. user.is_active = user_data.is_active
  236. # Handle group assignments
  237. if user_data.group_ids is not None:
  238. groups_result = await db.execute(select(Group).where(Group.id.in_(user_data.group_ids)))
  239. groups = groups_result.scalars().all()
  240. if len(groups) != len(user_data.group_ids):
  241. raise HTTPException(
  242. status_code=status.HTTP_400_BAD_REQUEST,
  243. detail="One or more group IDs are invalid",
  244. )
  245. user.groups = list(groups)
  246. await db.commit()
  247. await db.refresh(user)
  248. return _user_to_response(user)
  249. @router.get("/{user_id}/items-count")
  250. async def get_user_items_count(
  251. user_id: int,
  252. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_READ),
  253. db: AsyncSession = Depends(get_db),
  254. ):
  255. """Get count of items created by this user."""
  256. # Verify user exists
  257. result = await db.execute(select(User).where(User.id == user_id))
  258. if not result.scalar_one_or_none():
  259. raise HTTPException(
  260. status_code=status.HTTP_404_NOT_FOUND,
  261. detail="User not found",
  262. )
  263. # Count archives
  264. archives_result = await db.execute(select(func.count(PrintArchive.id)).where(PrintArchive.created_by_id == user_id))
  265. archives_count = archives_result.scalar() or 0
  266. # Count queue items
  267. queue_result = await db.execute(
  268. select(func.count(PrintQueueItem.id)).where(PrintQueueItem.created_by_id == user_id)
  269. )
  270. queue_items_count = queue_result.scalar() or 0
  271. # Count library files
  272. library_result = await db.execute(select(func.count(LibraryFile.id)).where(LibraryFile.created_by_id == user_id))
  273. library_files_count = library_result.scalar() or 0
  274. return {
  275. "archives": archives_count,
  276. "queue_items": queue_items_count,
  277. "library_files": library_files_count,
  278. }
  279. @router.delete("/{user_id}", status_code=status.HTTP_204_NO_CONTENT)
  280. async def delete_user(
  281. user_id: int,
  282. delete_items: bool = Query(False, description="Delete all items created by this user"),
  283. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_DELETE),
  284. db: AsyncSession = Depends(get_db),
  285. ):
  286. """Delete a user.
  287. If delete_items=True, all archives, queue items, and library files created by
  288. this user will also be deleted. Otherwise, these items will become "ownerless"
  289. (created_by_id set to NULL by the foreign key constraint).
  290. """
  291. result = await db.execute(select(User).where(User.id == user_id).options(selectinload(User.groups)))
  292. user = result.scalar_one_or_none()
  293. if not user:
  294. raise HTTPException(
  295. status_code=status.HTTP_404_NOT_FOUND,
  296. detail="User not found",
  297. )
  298. # Prevent deleting the last admin
  299. if user.is_admin:
  300. # Count admins by role or Administrators group membership
  301. admin_count_result = await db.execute(select(User).where(User.role == "admin", User.id != user_id))
  302. other_role_admins = admin_count_result.scalars().all()
  303. # Also check for users in Administrators group
  304. admin_group_result = await db.execute(
  305. select(Group).where(Group.name == "Administrators").options(selectinload(Group.users))
  306. )
  307. admin_group = admin_group_result.scalar_one_or_none()
  308. other_group_admins = [u for u in (admin_group.users if admin_group else []) if u.id != user_id and u.is_active]
  309. # Combine unique admins
  310. all_other_admins = {u.id for u in other_role_admins} | {u.id for u in other_group_admins}
  311. if len(all_other_admins) == 0:
  312. raise HTTPException(
  313. status_code=status.HTTP_400_BAD_REQUEST,
  314. detail="Cannot delete the last admin user",
  315. )
  316. # Prevent deleting yourself (only if auth is enabled and we have a current user)
  317. if current_user and user.id == current_user.id:
  318. raise HTTPException(
  319. status_code=status.HTTP_400_BAD_REQUEST,
  320. detail="Cannot delete your own account",
  321. )
  322. if delete_items:
  323. # Delete all items created by this user
  324. await db.execute(delete(PrintArchive).where(PrintArchive.created_by_id == user_id))
  325. await db.execute(delete(PrintQueueItem).where(PrintQueueItem.created_by_id == user_id))
  326. await db.execute(delete(LibraryFile).where(LibraryFile.created_by_id == user_id))
  327. else:
  328. # Explicitly set created_by_id to NULL for all items (ensures consistent behavior
  329. # across different database backends, including SQLite without foreign key support)
  330. from sqlalchemy import update
  331. await db.execute(update(PrintArchive).where(PrintArchive.created_by_id == user_id).values(created_by_id=None))
  332. await db.execute(
  333. update(PrintQueueItem).where(PrintQueueItem.created_by_id == user_id).values(created_by_id=None)
  334. )
  335. await db.execute(update(LibraryFile).where(LibraryFile.created_by_id == user_id).values(created_by_id=None))
  336. await db.delete(user)
  337. await db.commit()
  338. @router.post("/me/change-password", response_model=dict)
  339. async def change_own_password(
  340. password_data: ChangePasswordRequest,
  341. current_user: User | None = Depends(get_current_user_optional),
  342. db: AsyncSession = Depends(get_db),
  343. ):
  344. """Change the current user's password. Requires current password verification."""
  345. if not current_user:
  346. raise HTTPException(
  347. status_code=status.HTTP_401_UNAUTHORIZED,
  348. detail="Authentication required to change password",
  349. )
  350. # Verify current password
  351. if not verify_password(password_data.current_password, current_user.password_hash):
  352. raise HTTPException(
  353. status_code=status.HTTP_400_BAD_REQUEST,
  354. detail="Current password is incorrect",
  355. )
  356. # Validate new password
  357. if len(password_data.new_password) < 6:
  358. raise HTTPException(
  359. status_code=status.HTTP_400_BAD_REQUEST,
  360. detail="New password must be at least 6 characters",
  361. )
  362. # Fetch user from this session to ensure changes are persisted
  363. result = await db.execute(select(User).where(User.id == current_user.id))
  364. user = result.scalar_one_or_none()
  365. if not user:
  366. raise HTTPException(
  367. status_code=status.HTTP_404_NOT_FOUND,
  368. detail="User not found",
  369. )
  370. # Update password
  371. user.password_hash = get_password_hash(password_data.new_password)
  372. await db.commit()
  373. return {"message": "Password changed successfully"}