users.py 17 KB

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