Files
tsumugu/backend/app/api/roomshare.py
T
LarsandClaude Sonnet 5.5 d39c7d7999 Liefere Zähler, Rauminhalt und Teilen im Stapel für die neue Ablage.
Akten und Vorgänge melden Unterakten, Vorgänge und die Räume, mit denen sie geteilt sind, jeweils nur aus Lesbarem. Neue Endpunkte zeigen die Räume als Kacheln, den Inhalt eines Raums und teilen mehrere Einträge auf einmal oder gar nicht. Beim Anlegen lässt sich ein Eintrag gleich in einen Raum legen.

Die lokale Suite läuft mit 148 Tests grün.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-22 11:38:02 +02:00

140 lines
5.2 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 = []
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"],
"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")