Files
tsumugu/backend/app/access/roles.py
T
LarsandClaude Sonnet 5.5 037c0724a8
Deploy Development / deploy (push) Successful in 42s
Test Suite / pytest-backend (push) Successful in 3m28s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 20s
Ergänze die Benutzerverwaltung, damit der Betreiber Konten, Rollen und Zugänge verwalten kann.
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>
2026-10-20 12:49:23 +02:00

95 lines
3.3 KiB
Python

"""Fixed role bundles. Rights are read from the current database state, never from the session.
Platform: operator or user. Data room: member, manager or reader. There is no role editor.
An operator has no read right on content. Unknown role or status is refused.
"""
from __future__ import annotations
from uuid import UUID
from app.core.errors import forbidden
ROOM_ROLES = ("member", "manager", "reader")
ROOM_ROLE_LABELS = {
"member": "Mitglied",
"manager": "Verwalter",
"reader": "Leser",
}
def is_operator(conn, account_id: UUID) -> bool:
row = conn.execute(
"SELECT is_superadmin FROM auth_accounts WHERE id = %s AND status = 'active'",
(account_id,),
).fetchone()
return bool(row and row["is_superadmin"])
def require_operator(conn, account_id: UUID) -> None:
if not is_operator(conn, account_id):
raise forbidden("Das darf nur der Betreiber.")
def room_role(conn, account_id: UUID, room_id: UUID) -> str | None:
"""The active role of an account in an organization room, or None."""
row = conn.execute(
"""
SELECT m.role
FROM room_memberships m
JOIN data_rooms r ON r.id = m.room_id
WHERE m.room_id = %s AND m.subject_id = %s AND m.status = 'active' AND r.kind = 'organization'
""",
(room_id, account_id),
).fetchone()
if row is None or row["role"] not in ROOM_ROLES:
return None
return row["role"]
def require_room_manager(conn, account_id: UUID, room_id: UUID) -> None:
if room_role(conn, account_id, room_id) != "manager":
raise forbidden("Das darf nur ein Verwalter dieses Datenraums.")
def require_room_write(conn, account_id: UUID, room: dict) -> None:
"""Creating content needs the own personal room or a member or manager role."""
if room["kind"] == "personal":
if room["id"] != account_id:
raise forbidden("Der persönliche Datenraum gehört zu einem anderen Konto.")
return
role = room_role(conn, account_id, room["id"])
if role in {"member", "manager"}:
return
if role == "reader":
raise forbidden("Als Leser kannst du in diesem Datenraum nichts anlegen.")
raise forbidden("Du bist in diesem Datenraum nicht Mitglied.")
def count_active_operators(conn, *, lock: bool = False) -> int:
suffix = " FOR UPDATE" if lock else ""
rows = conn.execute(f"SELECT id FROM auth_accounts WHERE is_superadmin AND status = 'active'{suffix}").fetchall()
return len(rows)
def count_active_managers(conn, room_id: UUID, *, lock: bool = False) -> int:
suffix = " FOR UPDATE" if lock else ""
rows = conn.execute(
f"""
SELECT m.subject_id FROM room_memberships m
JOIN auth_accounts a ON a.id = m.subject_id
WHERE m.room_id = %s AND m.role = 'manager' AND m.status = 'active' AND a.status = 'active'{suffix}
""",
(room_id,),
).fetchall()
return len(rows)
def ensure_can_create(conn, account_id: UUID, scope_id: UUID) -> None:
"""Gate for every operation that creates content with this home room."""
room = conn.execute("SELECT id, kind FROM data_rooms WHERE id = %s", (scope_id,)).fetchone()
if room is None:
raise forbidden("Der gewählte Datenraum ist nicht verfügbar.")
require_room_write(conn, account_id, room)