"""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 account_kind(conn, account_id: UUID) -> str | None: row = conn.execute("SELECT account_kind FROM auth_accounts WHERE id = %s AND status = 'active'", (account_id,)).fetchone() return row["account_kind"] if row else None def require_main_user(conn, account_id: UUID) -> None: """Employees work in the rooms they were invited to. They create no rooms of their own.""" if account_kind(conn, account_id) != "main": raise forbidden("Als Mitarbeiter arbeitest du in den Räumen, in die du eingeladen wurdest.") 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.") require_main_user(conn, account_id) 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)