users.py 19 KB

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