users.py 20 KB

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