Add username login and expiring reset links so accounts stay recoverable.
Deploy Development / deploy (push) Successful in 58s
Test Suite / pytest-backend (push) Failing after 3m11s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 16s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-10-12 20:00:17 +02:00
1 parent 0277039b07
commit 89d149684e
24 files changed
+1595 -67

No files matched your search

+132 -17
View File
@@ -5,19 +5,34 @@ import uuid
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, Field
from account_access import (
admin_reset_recipients,
apply_identifier_edits,
assert_address_free,
assert_username_free,
deliver_jobs,
issue_password_reset,
issue_verification,
load_profile,
normalize_email,
normalize_username,
profile_view,
reset_message,
verify_message,
)
from auth import hash_password, require_admin_dep, require_auth
from db import get_db, row_to_dict
from db import get_db
from mailer import MailNotConfigured
router = APIRouter(prefix="/api/users", tags=["users"])
PUBLIC_FIELDS = "id, email, name, role, status, tier_id, created"
class CreateUserRequest(BaseModel):
email: str
name: str = Field(min_length=1)
password: str = Field(min_length=4)
role: str = "user"
username: str | None = None
class PatchUserRequest(BaseModel):
@@ -25,6 +40,16 @@ class PatchUserRequest(BaseModel):
role: str | None = None
status: str | None = None
password: str | None = None
username: str | None = None
email: str | None = None
secondary_email: str | None = None
class SelfPatchRequest(BaseModel):
username: str | None = None
email: str | None = None
secondary_email: str | None = None
password: str | None = None
def _active_admin_count(conn, exclude_id: str | None = None) -> int:
@@ -40,45 +65,104 @@ def _active_admin_count(conn, exclude_id: str | None = None) -> int:
return int(row["n"])
def _send_jobs(jobs: list[tuple[list[str], str, str]]) -> bool:
if not jobs:
return True
try:
deliver_jobs(jobs)
except MailNotConfigured:
return False
return True
@router.get("")
def list_users(session: dict = Depends(require_admin_dep)):
with get_db() as conn:
rows = conn.execute(f"SELECT {PUBLIC_FIELDS} FROM profiles ORDER BY created").fetchall()
return [row_to_dict(row) for row in rows]
rows = conn.execute("SELECT * FROM profiles ORDER BY created").fetchall()
return [profile_view(dict(row)) for row in rows]
@router.post("")
def create_user(req: CreateUserRequest, session: dict = Depends(require_admin_dep)):
if req.role not in {"user", "admin"}:
raise HTTPException(400, "Rolle muss user oder admin sein")
email = normalize_email(req.email)
username = normalize_username(req.username) if req.username and req.username.strip() else None
profile_id = str(uuid.uuid4())
jobs: list[tuple[list[str], str, str]] = []
try:
with get_db() as conn:
assert_address_free(conn, email, None)
if username:
assert_username_free(conn, username, None)
conn.execute(
"""
INSERT INTO profiles (id, email, name, password_hash, role, status, tier_id)
VALUES (?, ?, ?, ?, ?, 'active', 'local')
INSERT INTO profiles (id, email, name, password_hash, role, status, tier_id, username)
VALUES (?, ?, ?, ?, ?, 'active', 'local', ?)
""",
(
profile_id,
req.email.lower().strip(),
email,
req.name.strip(),
hash_password(req.password),
req.role,
username,
),
)
prof = row_to_dict(conn.execute(f"SELECT {PUBLIC_FIELDS} FROM profiles WHERE id = ?", (profile_id,)).fetchone())
raw = issue_verification(conn, profile_id, "primary", email)
if raw:
subject, body = verify_message(raw)
jobs.append(([email], subject, body))
prof = profile_view(load_profile(conn, profile_id))
except HTTPException:
raise
except Exception as exc:
if "UNIQUE" in str(exc).upper():
raise HTTPException(409, "E-Mail ist bereits vergeben") from exc
raise
if not _send_jobs(jobs):
prof = dict(prof or {})
prof["mail_sent"] = False
return prof
@router.patch("/me")
def patch_me(req: SelfPatchRequest, session: dict = Depends(require_auth)):
provided = req.model_fields_set
jobs: list[tuple[list[str], str, str]] = []
with get_db() as conn:
current = load_profile(conn, session["profile_id"])
if not current:
raise HTTPException(401, "Profil nicht gefunden")
if req.password:
if len(req.password) < 4:
raise HTTPException(400, "Passwort muss mind. 4 Zeichen haben")
conn.execute(
"UPDATE profiles SET password_hash = ? WHERE id = ?",
(hash_password(req.password), current["id"]),
)
jobs = apply_identifier_edits(
conn,
current,
username=req.username,
email=req.email,
secondary_email=req.secondary_email,
username_set="username" in provided,
email_set="email" in provided,
secondary_set="secondary_email" in provided,
)
prof = profile_view(load_profile(conn, current["id"]))
if not _send_jobs(jobs):
raise HTTPException(503, "Die Adresse ist vorgemerkt, der Mailversand ist nicht konfiguriert.")
return prof
@router.patch("/{profile_id}")
def patch_user(profile_id: str, req: PatchUserRequest, session: dict = Depends(require_admin_dep)):
provided = req.model_fields_set
jobs: list[tuple[list[str], str, str]] = []
with get_db() as conn:
current = row_to_dict(conn.execute("SELECT * FROM profiles WHERE id = ?", (profile_id,)).fetchone())
current = load_profile(conn, profile_id)
if not current:
raise HTTPException(404, "Nutzer nicht gefunden")
name = req.name.strip() if req.name is not None else current["name"]
@@ -105,18 +189,49 @@ def patch_user(profile_id: str, req: PatchUserRequest, session: dict = Depends(r
)
if status == "disabled":
conn.execute("DELETE FROM sessions WHERE profile_id = ?", (profile_id,))
return row_to_dict(conn.execute(f"SELECT {PUBLIC_FIELDS} FROM profiles WHERE id = ?", (profile_id,)).fetchone())
current = load_profile(conn, profile_id) or current
jobs = apply_identifier_edits(
conn,
current,
username=req.username,
email=req.email,
secondary_email=req.secondary_email,
username_set="username" in provided,
email_set="email" in provided,
secondary_set="secondary_email" in provided,
)
prof = profile_view(load_profile(conn, profile_id))
if not _send_jobs(jobs):
raise HTTPException(503, "Die Adresse ist vorgemerkt, der Mailversand ist nicht konfiguriert.")
return prof
@router.post("/{profile_id}/password-reset")
def admin_password_reset(profile_id: str, session: dict = Depends(require_admin_dep)):
if profile_id == session["profile_id"]:
raise HTTPException(400, "Den eigenen Reset startest du über die Anmeldung")
jobs: list[tuple[list[str], str, str]] = []
with get_db() as conn:
profile = load_profile(conn, profile_id)
if not profile:
raise HTTPException(404, "Nutzer nicht gefunden")
recipients = admin_reset_recipients(profile)
if not recipients:
raise HTTPException(400, "Für dieses Konto ist keine E-Mail hinterlegt")
raw = issue_password_reset(conn, profile_id)
if not raw:
raise HTTPException(429, "Zu viele Reset-Links. Bitte später erneut versuchen.")
subject, body = reset_message(raw)
jobs.append((recipients, subject, body))
if not _send_jobs(jobs):
raise HTTPException(503, "Mailversand ist nicht konfiguriert")
return {"ok": True}
@router.get("/me")
def my_account(session: dict = Depends(require_auth)):
with get_db() as conn:
prof = row_to_dict(
conn.execute(
f"SELECT {PUBLIC_FIELDS} FROM profiles WHERE id = ?",
(session["profile_id"],),
).fetchone()
)
prof = profile_view(load_profile(conn, session["profile_id"]))
if not prof:
raise HTTPException(401, "Profil nicht gefunden")
return prof