Files
tsumugu/backend/app/api/roomshare.py
T
LarsandClaude Opus 5.5 15e0dc7e52
Deploy Development / deploy (push) Successful in 46s
Test Suite / pytest-backend (push) Successful in 6m39s
Test Suite / frontend-build (push) Successful in 24s
Test Suite / smoke-dev (push) Successful in 0s
Gib verknüpfte Originale im Datenraum je Person über die eigenen Zugänge frei, damit Raummitglieder Paperless-Dokumente und E-Mails geteilter Akten lesen können.
Die Raumfreigabe allein gab die Zugänge des Owners nicht weiter. Ein Leser im Raum sah die Akte,
aber kein Original. Neu ist room_source_access: Wer im Raum teilen darf, schaltet für ein Mitglied
„Originale über meine Zugänge“ ein. Das gilt für alle eigenen, mit dem Raum geteilten Einträge.
Ausnahmen, Ende der Raumfreigabe und Verlassen des Raums schließen den Abruf. Beschluss R-01 ist
entsprechend ergänzt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 15:19:15 +01:00

146 lines
5.5 KiB
Python

"""Room shares of a case or matter. A room is a team: the owner shares an entry with any of the
rooms he is a member of, independent of where the entry is filed."""
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 ROOM_RIGHTS, Rights
from app.api.deps import Principal, get_conn, get_principal
from app.api.schemas import ExclusionIn, RoomShareIn
from app.library import room_effect
from app.ops import _shareable, set_person_exclusion, share_with_room, unshare_from_room
router = APIRouter()
_NO_STORE = {"Cache-Control": "no-store"}
def _capped(role: str, access: str) -> Rights:
rights = ROOM_RIGHTS.get(role, Rights())
return Rights(read=rights.read) if access == "read" else rights
def _view(conn, actor: UUID, kind: str, resource_id: UUID) -> dict:
_shareable(conn, actor, kind, resource_id)
shares = conn.execute(
"""
SELECT s.room_id, s.access, r.name, r.status
FROM room_shares s JOIN data_rooms r ON r.id = s.room_id
WHERE s.resource_kind = %s AND s.resource_id = %s
ORDER BY r.name, r.id
""",
(kind, resource_id),
).fetchall()
result = []
lent = conn.execute(
"SELECT room_id, subject_id FROM room_source_access WHERE owner_id = %s",
(actor,),
).fetchall()
lent_pairs = {(row["room_id"], row["subject_id"]) for row in lent}
for share in shares:
members = conn.execute(
"""
SELECT m.subject_id, m.role, a.display_name,
EXISTS (
SELECT 1 FROM access_grants g
WHERE g.is_exception AND g.grantee_subject_id = m.subject_id
AND g.resource_kind = %s AND g.resource_id = %s AND g.context_kind IS NULL
) AS excluded
FROM room_memberships m
JOIN auth_accounts a ON a.id = m.subject_id
WHERE m.room_id = %s AND m.status = 'active' AND m.subject_id <> %s
ORDER BY a.display_name, a.id
""",
(kind, resource_id, share["room_id"], actor),
).fetchall()
result.append(
{
"room_id": str(share["room_id"]),
"room_name": share["name"],
"archived": share["status"] != "active",
"access": share["access"],
"members": [
{
"subject_id": str(row["subject_id"]),
"display_name": row["display_name"],
"role": row["role"],
"excluded": row["excluded"],
"sources": (share["room_id"], row["subject_id"]) in lent_pairs,
"rights": {
"read": _capped(row["role"], share["access"]).read,
"edit": _capped(row["role"], share["access"]).edit,
},
}
for row in members
],
}
)
shared_ids = {item["room_id"] for item in result}
candidates = conn.execute(
"""
SELECT r.id, r.name, 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 m.role IN ('member', 'manager')
AND r.kind = 'organization' AND r.status = 'active'
ORDER BY r.name, r.id
""",
(actor,),
).fetchall()
return {
"id": str(resource_id),
"shares": result,
"available": [
{"id": str(row["id"]), "name": row["name"]} for row in candidates if str(row["id"]) not in shared_ids
],
}
def _register(kind: str, plural: str) -> None:
base = f"/api/v1/{plural}/{{resource_id}}/room-share"
@router.get(base, name=f"get_{kind}_room_share")
def get_share(resource_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
return JSONResponse(_view(conn, principal.account_id, kind, resource_id), headers=_NO_STORE)
@router.put(base + "/rooms/{room_id}", name=f"put_{kind}_room_share")
def put_share(
resource_id: UUID,
room_id: UUID,
body: RoomShareIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
shared = share_with_room(conn, principal.account_id, kind, resource_id, room_id, body.access)
shared["effect"] = room_effect(conn, room_id, principal.account_id, body.access)
return shared
@router.delete(base + "/rooms/{room_id}", status_code=204, name=f"delete_{kind}_room_share")
def delete_share(
resource_id: UUID,
room_id: UUID,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
unshare_from_room(conn, principal.account_id, kind, resource_id, room_id)
return Response(status_code=204)
@router.put(base + "/people/{subject_id}", name=f"put_{kind}_room_share_person")
def put_person(
resource_id: UUID,
subject_id: UUID,
body: ExclusionIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
set_person_exclusion(conn, principal.account_id, kind, resource_id, subject_id, body.excluded)
return {"subject_id": str(subject_id), "excluded": body.excluded}
_register("case", "cases")
_register("matter", "matters")