feat(users): automatically send the set-password email on user creation
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

Admin used to have to click 'Send Email' separately after creating a
passwordless user. Now the first set-password email is sent right away
as part of creation — best-effort: if the webhook isn't configured or
the send fails, the user is still created successfully, and the existing
'Send Email' button remains available to retry with proper error
surfacing.
This commit is contained in:
kitos
2026-07-27 11:31:24 +02:00
parent f13764d9e2
commit 8985eeaa03
2 changed files with 51 additions and 0 deletions
+16
View File
@@ -1,5 +1,8 @@
"""User management router (admin only)."""
# Import logging
import logging
# Import uuid
import uuid
@@ -47,6 +50,8 @@ from app.services.password_setup_service import send_password_setup_email
# Assign router = APIRouter(prefix="/users", tags=["users"])
router = APIRouter(prefix="/users", tags=["users"])
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# PATCH /users/me/preferences — update current user preferences
@@ -187,6 +192,17 @@ def create_user_route(
# Reload ORM object attributes from the database
db.refresh(user)
# Send the set-password email right away — best-effort. If the webhook
# isn't configured or rejects the request, the user is still created
# successfully; the admin can retry via the "Send Email" button, which
# surfaces the failure properly (unlike this automatic first attempt).
try:
with UnitOfWork(db) as uow:
send_password_setup_email(db, user)
uow.commit()
except Exception:
logger.warning("Automatic set-password email failed for new user %s", user.id, exc_info=True)
from app.services.notification_service import notify_roles_by_email
notify_roles_by_email(
db, roles=["admin"],