users.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. from datetime import datetime, timezone
  2. from typing import Annotated
  3. import jwt as _jwt
  4. from fastapi import APIRouter, Depends, HTTPException, Query, status
  5. from fastapi.security import HTTPAuthorizationCredentials
  6. from sqlalchemy import delete, func, select
  7. from sqlalchemy.ext.asyncio import AsyncSession
  8. from sqlalchemy.orm import selectinload
  9. from backend.app.api.routes.settings import get_external_login_url
  10. from backend.app.core.auth import (
  11. ALGORITHM,
  12. SECRET_KEY,
  13. RequirePermissionIfAuthEnabled,
  14. get_current_user_optional,
  15. get_password_hash,
  16. revoke_jti,
  17. security,
  18. verify_password,
  19. )
  20. from backend.app.core.database import get_db
  21. from backend.app.core.permissions import Permission
  22. from backend.app.models.api_key import APIKey
  23. from backend.app.models.archive import PrintArchive
  24. from backend.app.models.group import Group
  25. from backend.app.models.library import LibraryFile
  26. from backend.app.models.long_lived_token import LongLivedToken
  27. from backend.app.models.oidc_provider import UserOIDCLink
  28. from backend.app.models.print_batch import PrintBatch
  29. from backend.app.models.print_queue import PrintQueueItem
  30. from backend.app.models.settings import Settings
  31. from backend.app.models.user import User
  32. from backend.app.models.user_otp_code import UserOTPCode
  33. from backend.app.models.user_totp import UserTOTP
  34. from backend.app.schemas.auth import ChangePasswordRequest, GroupBrief, UserCreate, UserResponse, UserUpdate
  35. from backend.app.services.email_service import (
  36. create_welcome_email_from_template,
  37. generate_secure_password,
  38. get_smtp_settings,
  39. send_email,
  40. )
  41. router = APIRouter(prefix="/users", tags=["users"])
  42. def _user_to_response(user: User) -> UserResponse:
  43. """Convert a User model to UserResponse schema."""
  44. return UserResponse(
  45. id=user.id,
  46. username=user.username,
  47. email=user.email,
  48. role=user.role,
  49. is_active=user.is_active,
  50. is_admin=user.is_admin,
  51. auth_source=getattr(user, "auth_source", "local"),
  52. groups=[GroupBrief(id=g.id, name=g.name) for g in user.groups],
  53. permissions=sorted(user.get_permissions()),
  54. created_at=user.created_at.isoformat(),
  55. )
  56. @router.get("", response_model=list[UserResponse])
  57. @router.get("/", response_model=list[UserResponse])
  58. async def list_users(
  59. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_READ),
  60. db: AsyncSession = Depends(get_db),
  61. ):
  62. """List all users."""
  63. result = await db.execute(select(User).options(selectinload(User.groups)).order_by(User.created_at))
  64. users = result.scalars().all()
  65. return [_user_to_response(user) for user in users]
  66. @router.post("", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
  67. @router.post("/", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
  68. async def create_user(
  69. user_data: UserCreate,
  70. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_CREATE),
  71. db: AsyncSession = Depends(get_db),
  72. ):
  73. """Create a new user.
  74. When advanced authentication is enabled:
  75. - Email is required
  76. - Password is auto-generated and emailed to user
  77. - Admin cannot set or see the password
  78. """
  79. import logging
  80. logger = logging.getLogger(__name__)
  81. # Check if advanced auth is enabled
  82. result = await db.execute(select(Settings).where(Settings.key == "advanced_auth_enabled"))
  83. advanced_auth_setting = result.scalar_one_or_none()
  84. advanced_auth_enabled = advanced_auth_setting and advanced_auth_setting.value.lower() == "true"
  85. # Check if username already exists (case-insensitive)
  86. existing_user = await db.execute(select(User).where(func.lower(User.username) == func.lower(user_data.username)))
  87. if existing_user.scalar_one_or_none():
  88. raise HTTPException(
  89. status_code=status.HTTP_400_BAD_REQUEST,
  90. detail="Username already exists",
  91. )
  92. # Validate role
  93. if user_data.role not in ["admin", "user"]:
  94. raise HTTPException(
  95. status_code=status.HTTP_400_BAD_REQUEST,
  96. detail="Role must be 'admin' or 'user'",
  97. )
  98. # Advanced auth validation
  99. if advanced_auth_enabled:
  100. if not user_data.email:
  101. raise HTTPException(
  102. status_code=status.HTTP_400_BAD_REQUEST,
  103. detail="Email is required when advanced authentication is enabled",
  104. )
  105. # Check if email already exists (case-insensitive)
  106. existing_email = await db.execute(select(User).where(func.lower(User.email) == func.lower(user_data.email)))
  107. if existing_email.scalar_one_or_none():
  108. raise HTTPException(
  109. status_code=status.HTTP_400_BAD_REQUEST,
  110. detail="Email already exists",
  111. )
  112. # Generate password if advanced auth enabled, otherwise require password
  113. if advanced_auth_enabled:
  114. password = generate_secure_password()
  115. else:
  116. if not user_data.password:
  117. raise HTTPException(
  118. status_code=status.HTTP_400_BAD_REQUEST,
  119. detail="Password is required when advanced authentication is disabled",
  120. )
  121. password = user_data.password
  122. new_user = User(
  123. username=user_data.username,
  124. email=user_data.email,
  125. password_hash=get_password_hash(password),
  126. role=user_data.role,
  127. is_active=True,
  128. )
  129. # Handle group assignments
  130. if user_data.group_ids:
  131. groups_result = await db.execute(select(Group).where(Group.id.in_(user_data.group_ids)))
  132. groups = groups_result.scalars().all()
  133. if len(groups) != len(user_data.group_ids):
  134. raise HTTPException(
  135. status_code=status.HTTP_400_BAD_REQUEST,
  136. detail="One or more group IDs are invalid",
  137. )
  138. new_user.groups = list(groups)
  139. db.add(new_user)
  140. await db.commit()
  141. await db.refresh(new_user)
  142. # Send welcome email if advanced auth enabled
  143. if advanced_auth_enabled and new_user.email:
  144. try:
  145. smtp_settings = await get_smtp_settings(db)
  146. if smtp_settings:
  147. login_url = await get_external_login_url(db)
  148. subject, text_body, html_body = await create_welcome_email_from_template(
  149. db, new_user.username, password, login_url
  150. )
  151. send_email(smtp_settings, new_user.email, subject, text_body, html_body)
  152. logger.info(f"Welcome email sent to {new_user.email}")
  153. else:
  154. logger.warning(f"SMTP not configured, could not send welcome email to {new_user.email}")
  155. except Exception as e:
  156. logger.error(f"Failed to send welcome email: {e}")
  157. # Don't fail user creation if email fails
  158. return _user_to_response(new_user)
  159. @router.get("/{user_id}", response_model=UserResponse)
  160. async def get_user(
  161. user_id: int,
  162. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_READ),
  163. db: AsyncSession = Depends(get_db),
  164. ):
  165. """Get a user by ID."""
  166. result = await db.execute(select(User).where(User.id == user_id).options(selectinload(User.groups)))
  167. user = result.scalar_one_or_none()
  168. if not user:
  169. raise HTTPException(
  170. status_code=status.HTTP_404_NOT_FOUND,
  171. detail="User not found",
  172. )
  173. return _user_to_response(user)
  174. @router.patch("/{user_id}", response_model=UserResponse)
  175. async def update_user(
  176. user_id: int,
  177. user_data: UserUpdate,
  178. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_UPDATE),
  179. db: AsyncSession = Depends(get_db),
  180. ):
  181. """Update a user."""
  182. result = await db.execute(select(User).where(User.id == user_id).options(selectinload(User.groups)))
  183. user = result.scalar_one_or_none()
  184. if not user:
  185. raise HTTPException(
  186. status_code=status.HTTP_404_NOT_FOUND,
  187. detail="User not found",
  188. )
  189. # Prevent deactivating the last admin
  190. if user_data.is_active is False and user.is_admin:
  191. # Count admins by role or Administrators group membership
  192. admin_count_result = await db.execute(select(User).where(User.role == "admin", User.is_active.is_(True)))
  193. role_admins = admin_count_result.scalars().all()
  194. # Also check for users in Administrators group
  195. admin_group_result = await db.execute(
  196. select(Group).where(Group.name == "Administrators").options(selectinload(Group.users))
  197. )
  198. admin_group = admin_group_result.scalar_one_or_none()
  199. group_admins = [u for u in (admin_group.users if admin_group else []) if u.is_active]
  200. # Combine unique admins
  201. all_admins = {u.id for u in role_admins} | {u.id for u in group_admins}
  202. if len(all_admins) <= 1 and user.id in all_admins:
  203. raise HTTPException(
  204. status_code=status.HTTP_400_BAD_REQUEST,
  205. detail="Cannot deactivate the last admin user",
  206. )
  207. # Prevent changing role of last admin
  208. if user_data.role and user_data.role != "admin" and user.role == "admin":
  209. admin_count_result = await db.execute(select(User).where(User.role == "admin", User.is_active.is_(True)))
  210. admin_count = len(admin_count_result.scalars().all())
  211. if admin_count <= 1:
  212. raise HTTPException(
  213. status_code=status.HTTP_400_BAD_REQUEST,
  214. detail="Cannot change role of the last admin user",
  215. )
  216. if user_data.username is not None:
  217. # Check if new username already exists (case-insensitive)
  218. existing_user = await db.execute(
  219. select(User).where(func.lower(User.username) == func.lower(user_data.username), User.id != user_id)
  220. )
  221. if existing_user.scalar_one_or_none():
  222. raise HTTPException(
  223. status_code=status.HTTP_400_BAD_REQUEST,
  224. detail="Username already exists",
  225. )
  226. user.username = user_data.username
  227. if user_data.email is not None:
  228. # Check if new email already exists (case-insensitive)
  229. existing_email = await db.execute(
  230. select(User).where(func.lower(User.email) == func.lower(user_data.email), User.id != user_id)
  231. )
  232. if existing_email.scalar_one_or_none():
  233. raise HTTPException(
  234. status_code=status.HTTP_400_BAD_REQUEST,
  235. detail="Email already exists",
  236. )
  237. user.email = user_data.email
  238. if user_data.password is not None:
  239. if getattr(user, "auth_source", "local") == "ldap":
  240. raise HTTPException(
  241. status_code=status.HTTP_400_BAD_REQUEST,
  242. detail="Cannot set password for LDAP users",
  243. )
  244. user.password_hash = get_password_hash(user_data.password)
  245. if user_data.role is not None:
  246. if user_data.role not in ["admin", "user"]:
  247. raise HTTPException(
  248. status_code=status.HTTP_400_BAD_REQUEST,
  249. detail="Role must be 'admin' or 'user'",
  250. )
  251. user.role = user_data.role
  252. if user_data.is_active is not None:
  253. user.is_active = user_data.is_active
  254. # Handle group assignments
  255. if user_data.group_ids is not None:
  256. groups_result = await db.execute(select(Group).where(Group.id.in_(user_data.group_ids)))
  257. groups = groups_result.scalars().all()
  258. if len(groups) != len(user_data.group_ids):
  259. raise HTTPException(
  260. status_code=status.HTTP_400_BAD_REQUEST,
  261. detail="One or more group IDs are invalid",
  262. )
  263. user.groups = list(groups)
  264. await db.commit()
  265. result = await db.execute(select(User).where(User.id == user_id).options(selectinload(User.groups)))
  266. user = result.scalar_one()
  267. return _user_to_response(user)
  268. @router.get("/{user_id}/items-count")
  269. async def get_user_items_count(
  270. user_id: int,
  271. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_READ),
  272. db: AsyncSession = Depends(get_db),
  273. ):
  274. """Get count of items created by this user."""
  275. # Verify user exists
  276. result = await db.execute(select(User).where(User.id == user_id))
  277. if not result.scalar_one_or_none():
  278. raise HTTPException(
  279. status_code=status.HTTP_404_NOT_FOUND,
  280. detail="User not found",
  281. )
  282. # Count archives
  283. archives_result = await db.execute(select(func.count(PrintArchive.id)).where(PrintArchive.created_by_id == user_id))
  284. archives_count = archives_result.scalar() or 0
  285. # Count queue items
  286. queue_result = await db.execute(
  287. select(func.count(PrintQueueItem.id)).where(PrintQueueItem.created_by_id == user_id)
  288. )
  289. queue_items_count = queue_result.scalar() or 0
  290. # Count library files
  291. library_result = await db.execute(
  292. select(func.count(LibraryFile.id)).where(
  293. LibraryFile.created_by_id == user_id,
  294. LibraryFile.deleted_at.is_(None),
  295. )
  296. )
  297. library_files_count = library_result.scalar() or 0
  298. return {
  299. "archives": archives_count,
  300. "queue_items": queue_items_count,
  301. "library_files": library_files_count,
  302. }
  303. @router.delete("/{user_id}", status_code=status.HTTP_204_NO_CONTENT)
  304. async def delete_user(
  305. user_id: int,
  306. delete_items: bool = Query(False, description="Delete all items created by this user"),
  307. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_DELETE),
  308. db: AsyncSession = Depends(get_db),
  309. ):
  310. """Delete a user.
  311. If delete_items=True, all archives, queue items, and library files created by
  312. this user will also be deleted. Otherwise, these items will become "ownerless"
  313. (created_by_id set to NULL by the foreign key constraint).
  314. """
  315. result = await db.execute(select(User).where(User.id == user_id).options(selectinload(User.groups)))
  316. user = result.scalar_one_or_none()
  317. if not user:
  318. raise HTTPException(
  319. status_code=status.HTTP_404_NOT_FOUND,
  320. detail="User not found",
  321. )
  322. # Prevent deleting the last admin
  323. if user.is_admin:
  324. # Count admins by role or Administrators group membership
  325. admin_count_result = await db.execute(select(User).where(User.role == "admin", User.id != user_id))
  326. other_role_admins = admin_count_result.scalars().all()
  327. # Also check for users in Administrators group
  328. admin_group_result = await db.execute(
  329. select(Group).where(Group.name == "Administrators").options(selectinload(Group.users))
  330. )
  331. admin_group = admin_group_result.scalar_one_or_none()
  332. other_group_admins = [u for u in (admin_group.users if admin_group else []) if u.id != user_id and u.is_active]
  333. # Combine unique admins
  334. all_other_admins = {u.id for u in other_role_admins} | {u.id for u in other_group_admins}
  335. if len(all_other_admins) == 0:
  336. raise HTTPException(
  337. status_code=status.HTTP_400_BAD_REQUEST,
  338. detail="Cannot delete the last admin user",
  339. )
  340. # Prevent deleting yourself (only if auth is enabled and we have a current user)
  341. if current_user and user.id == current_user.id:
  342. raise HTTPException(
  343. status_code=status.HTTP_400_BAD_REQUEST,
  344. detail="Cannot delete your own account",
  345. )
  346. if delete_items:
  347. # Delete all items created by this user
  348. await db.execute(delete(PrintArchive).where(PrintArchive.created_by_id == user_id))
  349. await db.execute(delete(PrintQueueItem).where(PrintQueueItem.created_by_id == user_id))
  350. await db.execute(delete(LibraryFile).where(LibraryFile.created_by_id == user_id))
  351. await db.execute(delete(PrintBatch).where(PrintBatch.created_by_id == user_id))
  352. else:
  353. # Explicitly set created_by_id to NULL for all items (ensures consistent behavior
  354. # across different database backends, including SQLite without foreign key support).
  355. # PrintBatch carries the same created_by_id FK with ondelete=SET NULL — admin-deleted
  356. # users would otherwise leave dangling created_by_id on SQLite (#1295 review nit).
  357. from sqlalchemy import update
  358. await db.execute(update(PrintArchive).where(PrintArchive.created_by_id == user_id).values(created_by_id=None))
  359. await db.execute(
  360. update(PrintQueueItem).where(PrintQueueItem.created_by_id == user_id).values(created_by_id=None)
  361. )
  362. await db.execute(update(LibraryFile).where(LibraryFile.created_by_id == user_id).values(created_by_id=None))
  363. await db.execute(update(PrintBatch).where(PrintBatch.created_by_id == user_id).values(created_by_id=None))
  364. # Drop API keys owned by this user. The model declares ON DELETE CASCADE
  365. # so Postgres handles this automatically, but SQLite ships with FK
  366. # enforcement off (the project's existing pattern — same reason the
  367. # blocks above set created_by_id = NULL by hand). Without an explicit
  368. # DELETE here, deleting a user on SQLite would leave their API keys
  369. # with a dangling user_id and ``_user_from_api_key`` would return None,
  370. # silently degrading the keys to anonymous (and locking them out of
  371. # /cloud/* — but the rest of the API would still accept them, which is
  372. # exactly the orphan-key state the CASCADE was meant to prevent).
  373. await db.execute(delete(APIKey).where(APIKey.user_id == user_id))
  374. # Drop OIDC links, MFA state, and long-lived camera-stream tokens
  375. # owned by this user. Same SQLite/FK pattern as APIKey above. Without
  376. # these, deleting a user on SQLite leaves:
  377. # - UserOIDCLink: the OIDC callback finds the orphan link, fails to
  378. # resolve the (now missing) user, and falls through to
  379. # "account_inactive" instead of triggering auto_create (#1285).
  380. # - UserTOTP: MFA secrets persist in the DB after the owning user.
  381. # - UserOTPCode: pending email OTP codes linger.
  382. # - LongLivedToken: per-user camera-stream tokens whose secret_hash
  383. # is still valid — verify() would happily match them by lookup
  384. # prefix even though the user is gone.
  385. await db.execute(delete(UserOIDCLink).where(UserOIDCLink.user_id == user_id))
  386. await db.execute(delete(UserTOTP).where(UserTOTP.user_id == user_id))
  387. await db.execute(delete(UserOTPCode).where(UserOTPCode.user_id == user_id))
  388. await db.execute(delete(LongLivedToken).where(LongLivedToken.user_id == user_id))
  389. await db.delete(user)
  390. await db.commit()
  391. @router.post("/me/change-password", response_model=dict)
  392. async def change_own_password(
  393. password_data: ChangePasswordRequest,
  394. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  395. current_user: User | None = Depends(get_current_user_optional),
  396. db: AsyncSession = Depends(get_db),
  397. ):
  398. """Change the current user's password. Requires current password verification."""
  399. if not current_user:
  400. raise HTTPException(
  401. status_code=status.HTTP_401_UNAUTHORIZED,
  402. detail="Authentication required to change password",
  403. )
  404. # Block password change for LDAP users
  405. if getattr(current_user, "auth_source", "local") == "ldap":
  406. raise HTTPException(
  407. status_code=status.HTTP_400_BAD_REQUEST,
  408. detail="Cannot change password for LDAP users — passwords are managed by the LDAP server",
  409. )
  410. # Verify current password
  411. if not current_user.password_hash:
  412. raise HTTPException(
  413. status_code=status.HTTP_400_BAD_REQUEST,
  414. detail="Account has no local password set",
  415. )
  416. # Rate-limit failed password-change attempts (H-R5-A)
  417. from backend.app.api.routes.mfa import MAX_2FA_ATTEMPTS, check_rate_limit, record_failed_attempt
  418. await check_rate_limit(db, current_user.username, event_type="password_change", max_attempts=MAX_2FA_ATTEMPTS)
  419. if not verify_password(password_data.current_password, current_user.password_hash):
  420. await record_failed_attempt(db, current_user.username, event_type="password_change")
  421. raise HTTPException(
  422. status_code=status.HTTP_400_BAD_REQUEST,
  423. detail="Current password is incorrect",
  424. )
  425. # Fetch user from this session to ensure changes are persisted
  426. result = await db.execute(select(User).where(User.id == current_user.id))
  427. user = result.scalar_one_or_none()
  428. if not user:
  429. raise HTTPException(
  430. status_code=status.HTTP_404_NOT_FOUND,
  431. detail="User not found",
  432. )
  433. # Update password
  434. user.password_hash = get_password_hash(password_data.new_password)
  435. user.password_changed_at = datetime.now(timezone.utc) # M-R7-B: invalidate all prior JWTs
  436. await db.commit()
  437. # L-R6-A: Password verified successfully — reset the failure counter
  438. from backend.app.api.routes.mfa import clear_failed_attempts
  439. await clear_failed_attempts(db, user.username, event_type="password_change")
  440. # Revoke the current session token so the caller must re-authenticate (M-R5-A)
  441. if credentials is not None:
  442. try:
  443. payload = _jwt.decode(credentials.credentials, SECRET_KEY, algorithms=[ALGORITHM])
  444. jti = payload.get("jti")
  445. exp = payload.get("exp")
  446. if jti and exp:
  447. try:
  448. await revoke_jti(jti, datetime.fromtimestamp(exp, tz=timezone.utc), user.username)
  449. except Exception as exc:
  450. # B4: log so operators know revocation is broken; password was
  451. # already changed so the token will fail freshness checks anyway.
  452. import logging
  453. logging.getLogger(__name__).error(
  454. "Failed to revoke JTI after password change for user %s: %s", user.username, exc
  455. )
  456. except Exception:
  457. pass # Decode failure is harmless — token is already invalidated by password_changed_at
  458. return {"message": "Password changed successfully"}