"""Download policy: which room role may save the original of a document or mail. Reading inside the app stays possible. The policy is a rule of use, not a copy protection: what the screen shows has to reach the browser. """ from __future__ import annotations from uuid import UUID from fastapi import APIRouter, Depends from fastapi.responses import JSONResponse from psycopg import Connection from psycopg.types.json import Json from app.access.policy import audit from app.access.roles import require_main_user, room_role from app.api.deps import Principal, get_conn, get_principal from app.api.schemas import DownloadPolicyIn from app.core.errors import forbidden, not_found router = APIRouter() _NO_STORE = {"Cache-Control": "no-store"} ROOM_KEYS = ("manager", "member", "reader") def _policy(raw: dict, keys: tuple[str, ...]) -> dict[str, bool]: return {key: bool(raw.get(key, True)) for key in keys} def _room_of_member(conn, account_id: UUID, room_id: UUID) -> dict: # A stranger and an unknown room answer alike. room = conn.execute("SELECT * FROM data_rooms WHERE id = %s AND kind = 'organization'", (room_id,)).fetchone() if room is None or room_role(conn, account_id, room_id) is None: raise not_found() return room @router.get("/api/v1/rooms/{room_id}/download-policy") def get_room_policy(room_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): room = _room_of_member(conn, principal.account_id, room_id) return JSONResponse( {"roles": _policy(room["download_roles"], ROOM_KEYS), "can_change": room["owner_id"] == principal.account_id}, headers=_NO_STORE, ) @router.put("/api/v1/rooms/{room_id}/download-policy") def put_room_policy( room_id: UUID, body: DownloadPolicyIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): room = _room_of_member(conn, principal.account_id, room_id) if room["owner_id"] != principal.account_id: raise forbidden("Das darf nur der Eigentümer des Datenraums.") roles = {"manager": body.manager, "member": body.member, "reader": body.reader} conn.execute("UPDATE data_rooms SET download_roles = %s WHERE id = %s", (Json(roles), room_id)) audit(conn, principal.account_id, room_id, "room_download_policy_changed", "room", room_id, detail=roles) return JSONResponse({"roles": roles, "can_change": True}, headers=_NO_STORE) @router.get("/api/v1/auth/download-defaults") def get_defaults(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): require_main_user(conn, principal.account_id) row = conn.execute("SELECT download_defaults FROM auth_accounts WHERE id = %s", (principal.account_id,)).fetchone() return JSONResponse({"roles": _policy(row["download_defaults"], (*ROOM_KEYS, "direct"))}, headers=_NO_STORE) @router.put("/api/v1/auth/download-defaults") def put_defaults( body: DownloadPolicyIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): require_main_user(conn, principal.account_id) roles = {"manager": body.manager, "member": body.member, "reader": body.reader, "direct": True if body.direct is None else body.direct} conn.execute("UPDATE auth_accounts SET download_defaults = %s WHERE id = %s", (Json(roles), principal.account_id)) audit(conn, principal.account_id, None, "download_defaults_changed", "account", principal.account_id, detail=roles) return JSONResponse({"roles": roles}, headers=_NO_STORE)