feat(users): admin can permanently delete users, fix set-password redirect bug
Aegis CI / lint-and-test (push) Has been cancelled
Snyk Security Scan / Python vulnerabilities (backend) (push) Has been cancelled
Snyk Security Scan / npm vulnerabilities (frontend) (push) Has been cancelled
Snyk Security Scan / Docker image vulnerabilities (backend) (push) Has been cancelled

- New DELETE /users/{id}: hard-deletes a user only if they have zero
  activity footprint (no tests, evidence, worklogs, audit entries, Jira
  links, procedure/template suggestions, reviewed imports) — otherwise
  rejects with a clear message to deactivate instead, to keep the audit
  trail intact. Cleans up the user's own notifications/password-setup
  tokens as part of deletion. Self-delete is blocked. Wired to a trash
  icon in the Users page with a confirm dialog.
- Registered the EvaluationImport model in app/models/__init__.py — it
  was missing, so its table never existed in the SQLite test DB; only
  surfaced once the new delete-user footprint check queried it.
- Fixed a redirect bug: the axios response interceptor's on-401 redirect
  to /login didn't exempt /set-password, so the auth provider's routine
  mount-time /auth/me check (401 for a logged-out visitor) bounced anyone
  opening their emailed set-password link away before they could use it.
This commit is contained in:
kitos
2026-07-23 08:58:17 +02:00
parent 545a84b137
commit d932134975
7 changed files with 242 additions and 2 deletions
+31
View File
@@ -36,6 +36,7 @@ from app.services.audit_service import log_action
# Import from app.services.user_service
from app.services.user_service import (
create_user_without_password,
delete_user,
get_user_or_raise,
list_users,
switch_active_role,
@@ -307,6 +308,36 @@ def update_user_route(
return user
# ---------------------------------------------------------------------------
# DELETE /users/{id} — permanently delete a user
# ---------------------------------------------------------------------------
@router.delete("/{user_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_user_route(
user_id: uuid.UUID,
db: Session = Depends(get_db),
current_user: User = Depends(require_role("admin")),
) -> None:
"""Permanently delete a user. **Requires admin role.**.
Only users with no activity footprint (no tests, evidence, worklogs,
audit entries, etc.) can be hard-deleted — anyone else must be
deactivated instead, to preserve the audit trail.
"""
with UnitOfWork(db) as uow:
delete_user(db, user_id, current_user.id)
log_action(
db,
user_id=current_user.id,
action="delete_user",
entity_type="user",
entity_id=user_id,
details={},
)
uow.commit()
# ---------------------------------------------------------------------------
# POST /users/{id}/send-password-email — set-password / reset link
# ---------------------------------------------------------------------------