Der Betreiber legt Konten mit Einmal-Passwort an, deaktiviert und aktiviert sie, setzt Passwörter zurück, beendet Sitzungen und vergibt die Betreiberrolle; der letzte Betreiber und das eigene Konto sind geschützt. Nutzer ändern ihr Passwort und sehen ihre Geräte. Räume kennen neben Mitglied und Verwalter die Rolle Leser, die nichts anlegt und keine Bearbeiten- oder Freigeben-Grants erhält. Verwalter pflegen Mitglieder, Rollen und Einladungen mit Rolle; der Betreiber sieht Räume ohne Inhalte und liest das Verwaltungsprotokoll. Die Login-Begrenzung steht in der Datenbank, unbekannte Konten laufen durch einen Zeitausgleich. Die lokale Suite läuft mit 116 Tests grün, der Frontend-Build und 16 Node-Tests ebenfalls. Die Oberfläche wurde noch nicht im Browser bedient. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
251 lines
8.8 KiB
Python
251 lines
8.8 KiB
Python
"""Account administration for the operator and the own account (password, sessions)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, Response
|
|
from fastapi.responses import JSONResponse
|
|
from psycopg import Connection
|
|
|
|
from app.access.policy import audit
|
|
from app.access.roles import require_operator
|
|
from app.api.deps import Principal, get_conn, get_principal, get_session
|
|
from app.api.schemas import AccountIn, AccountPatchIn, PasswordChangeIn
|
|
from app.auth.accounts import (
|
|
change_own_password,
|
|
create_account,
|
|
generate_password,
|
|
reset_password,
|
|
revoke_sessions,
|
|
set_display_name,
|
|
set_operator,
|
|
set_status,
|
|
)
|
|
from app.core.errors import not_found, unprocessable
|
|
from app.core.timeutil import dump_dt
|
|
|
|
router = APIRouter()
|
|
|
|
_NO_STORE = {"Cache-Control": "no-store"}
|
|
|
|
|
|
def _account_item(row) -> dict:
|
|
return {
|
|
"id": str(row["id"]),
|
|
"email": row["email"],
|
|
"display_name": row["display_name"],
|
|
"is_superadmin": row["is_superadmin"],
|
|
"status": row["status"],
|
|
"must_change_password": row["must_change_password"],
|
|
"created_at": dump_dt(row["created_at"]),
|
|
"last_login_at": dump_dt(row["last_login_at"]),
|
|
}
|
|
|
|
|
|
@router.get("/api/v1/accounts")
|
|
def accounts(
|
|
status: str | None = None,
|
|
principal: Principal = Depends(get_principal),
|
|
conn: Connection = Depends(get_conn),
|
|
):
|
|
require_operator(conn, principal.account_id)
|
|
if status not in {None, "active", "disabled"}:
|
|
raise unprocessable("Der Statusfilter ist ungültig.")
|
|
rows = conn.execute(
|
|
"""
|
|
SELECT id, email, display_name, is_superadmin, status, must_change_password, created_at, last_login_at
|
|
FROM auth_accounts
|
|
WHERE origin = 'operator' AND (%s::text IS NULL OR status = %s::text)
|
|
ORDER BY status, display_name, email
|
|
""",
|
|
(status, status),
|
|
).fetchall()
|
|
return {"items": [_account_item(row) for row in rows], "next_cursor": None}
|
|
|
|
|
|
@router.get("/api/v1/accounts/{account_id}")
|
|
def account_detail(account_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
|
require_operator(conn, principal.account_id)
|
|
row = conn.execute(
|
|
"""
|
|
SELECT id, email, display_name, is_superadmin, status, must_change_password, created_at, last_login_at
|
|
FROM auth_accounts WHERE id = %s AND origin = 'operator'
|
|
""",
|
|
(account_id,),
|
|
).fetchone()
|
|
if row is None:
|
|
raise not_found()
|
|
rooms = conn.execute(
|
|
"""
|
|
SELECT r.id, r.name, r.status, m.role
|
|
FROM room_memberships m
|
|
JOIN data_rooms r ON r.id = m.room_id
|
|
WHERE m.subject_id = %s AND m.status = 'active' AND r.kind = 'organization'
|
|
ORDER BY r.name, r.id
|
|
""",
|
|
(account_id,),
|
|
).fetchall()
|
|
sessions = conn.execute(
|
|
"""
|
|
SELECT count(*) AS n FROM auth_sessions
|
|
WHERE account_id = %s AND revoked_at IS NULL AND expires_at > now()
|
|
""",
|
|
(account_id,),
|
|
).fetchone()
|
|
body = _account_item(row)
|
|
body["rooms"] = [
|
|
{"id": str(room["id"]), "name": room["name"], "status": room["status"], "role": room["role"]} for room in rooms
|
|
]
|
|
body["active_sessions"] = sessions["n"]
|
|
return JSONResponse(body, headers=_NO_STORE)
|
|
|
|
|
|
@router.post("/api/v1/accounts")
|
|
def post_account(body: AccountIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
|
require_operator(conn, principal.account_id)
|
|
password = body.password or generate_password()
|
|
account = create_account(
|
|
conn,
|
|
body.email,
|
|
body.display_name,
|
|
password,
|
|
is_superadmin=body.is_operator,
|
|
must_change_password=True,
|
|
)
|
|
audit(conn, principal.account_id, None, "account_created", "account", account["id"])
|
|
if body.is_operator:
|
|
audit(conn, principal.account_id, None, "operator_granted", "account", account["id"])
|
|
payload = {
|
|
"id": str(account["id"]),
|
|
"email": account["email"],
|
|
"display_name": account["display_name"],
|
|
"is_superadmin": account["is_superadmin"],
|
|
"status": account["status"],
|
|
"must_change_password": True,
|
|
}
|
|
if not body.password:
|
|
payload["temporary_password"] = password
|
|
return JSONResponse(
|
|
status_code=201,
|
|
content=payload,
|
|
headers={"Location": f"/api/v1/accounts/{account['id']}", **_NO_STORE},
|
|
)
|
|
|
|
|
|
@router.patch("/api/v1/accounts/{account_id}")
|
|
def patch_account(
|
|
account_id: UUID,
|
|
body: AccountPatchIn,
|
|
principal: Principal = Depends(get_principal),
|
|
conn: Connection = Depends(get_conn),
|
|
):
|
|
require_operator(conn, principal.account_id)
|
|
exists = conn.execute("SELECT 1 FROM auth_accounts WHERE id = %s AND origin = 'operator'", (account_id,)).fetchone()
|
|
if exists is None:
|
|
raise not_found()
|
|
if body.display_name is not None:
|
|
set_display_name(conn, principal.account_id, account_id, body.display_name)
|
|
if body.is_operator is not None:
|
|
set_operator(conn, principal.account_id, account_id, body.is_operator)
|
|
if body.status is not None:
|
|
set_status(conn, principal.account_id, account_id, body.status)
|
|
row = conn.execute(
|
|
"""
|
|
SELECT id, email, display_name, is_superadmin, status, must_change_password, created_at, last_login_at
|
|
FROM auth_accounts WHERE id = %s
|
|
""",
|
|
(account_id,),
|
|
).fetchone()
|
|
return _account_item(row)
|
|
|
|
|
|
@router.post("/api/v1/accounts/{account_id}/password-reset")
|
|
def post_password_reset(
|
|
account_id: UUID,
|
|
principal: Principal = Depends(get_principal),
|
|
conn: Connection = Depends(get_conn),
|
|
):
|
|
require_operator(conn, principal.account_id)
|
|
password = reset_password(conn, principal.account_id, account_id)
|
|
return JSONResponse({"temporary_password": password}, headers=_NO_STORE)
|
|
|
|
|
|
@router.post("/api/v1/accounts/{account_id}/sessions/revoke")
|
|
def post_revoke_sessions(
|
|
account_id: UUID,
|
|
principal: Principal = Depends(get_principal),
|
|
conn: Connection = Depends(get_conn),
|
|
):
|
|
require_operator(conn, principal.account_id)
|
|
if conn.execute("SELECT 1 FROM auth_accounts WHERE id = %s", (account_id,)).fetchone() is None:
|
|
raise not_found()
|
|
count = revoke_sessions(conn, account_id)
|
|
audit(conn, principal.account_id, None, "sessions_revoked", "account", account_id, detail={"count": count})
|
|
return {"revoked": count}
|
|
|
|
|
|
@router.post("/api/v1/auth/password", status_code=204)
|
|
def post_own_password(
|
|
body: PasswordChangeIn,
|
|
session: dict = Depends(get_session),
|
|
conn: Connection = Depends(get_conn),
|
|
):
|
|
change_own_password(conn, session["account_id"], session["id"], body.current_password, body.new_password)
|
|
return Response(status_code=204)
|
|
|
|
|
|
@router.get("/api/v1/auth/sessions")
|
|
def own_sessions(session: dict = Depends(get_session), conn: Connection = Depends(get_conn)):
|
|
rows = conn.execute(
|
|
"""
|
|
SELECT id, label, created_at, last_seen_at, expires_at
|
|
FROM auth_sessions
|
|
WHERE account_id = %s AND revoked_at IS NULL AND expires_at > now()
|
|
ORDER BY created_at DESC, id
|
|
""",
|
|
(session["account_id"],),
|
|
).fetchall()
|
|
return JSONResponse(
|
|
{
|
|
"items": [
|
|
{
|
|
"id": str(row["id"]),
|
|
"label": row["label"] or "Browser",
|
|
"created_at": dump_dt(row["created_at"]),
|
|
"last_seen_at": dump_dt(row["last_seen_at"]),
|
|
"expires_at": dump_dt(row["expires_at"]),
|
|
"current": row["id"] == session["id"],
|
|
}
|
|
for row in rows
|
|
]
|
|
},
|
|
headers=_NO_STORE,
|
|
)
|
|
|
|
|
|
@router.delete("/api/v1/auth/sessions/{session_id}", status_code=204)
|
|
def end_own_session(session_id: UUID, session: dict = Depends(get_session), conn: Connection = Depends(get_conn)):
|
|
if session_id == session["id"]:
|
|
raise unprocessable("Diese Sitzung beendest du mit Abmelden.")
|
|
row = conn.execute(
|
|
"""
|
|
UPDATE auth_sessions SET revoked_at = now()
|
|
WHERE id = %s AND account_id = %s AND revoked_at IS NULL
|
|
RETURNING id
|
|
""",
|
|
(session_id, session["account_id"]),
|
|
).fetchone()
|
|
if row is None:
|
|
raise not_found()
|
|
audit(conn, session["account_id"], None, "session_ended", "session", session_id)
|
|
return Response(status_code=204)
|
|
|
|
|
|
@router.post("/api/v1/auth/sessions/revoke-others")
|
|
def end_other_sessions(session: dict = Depends(get_session), conn: Connection = Depends(get_conn)):
|
|
count = revoke_sessions(conn, session["account_id"], keep=session["id"])
|
|
audit(conn, session["account_id"], None, "sessions_revoked", "account", session["account_id"], detail={"count": count})
|
|
return {"revoked": count}
|
|
|