Führe Raum-Eigentümer und Raumfreigabe ein, damit aus einem privaten Raum ein Mehrbenutzer-Raum wird.
Jeder Datenraum hat einen Eigentümer, der Verwalter bleibt, den Raum übertragen kann und im Notfall vom Betreiber ersetzt wird. Neue Akten und Vorgänge folgen der Voreinstellung des Raums: Bei "im Raum teilen" lesen Leser sie und bearbeiten Mitglieder und Verwalter; Weitergeben und Löschen bleiben beim Owner, der eine Akte privat stellen oder einzelne Personen ausnehmen kann. Bestehende Akten bleiben privat. Beim ersten weiteren Mitglied zeigt die Oberfläche, was sichtbar wird. Das Löschen von Akte und Vorgang verlangt jetzt den Owner statt nur das Bearbeitungsrecht und schließt damit R-01. Die Rechte der Raumfreigabe gelten nur bei aktivem Raum, aktiver Mitgliedschaft und aktivem Konto. Die lokale Suite läuft mit 128 Tests grün, der Frontend-Build und 19 Node-Tests ebenfalls. Die Oberfläche wurde noch nicht im Browser bedient. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
037c0724a8
commit
1c2f045e7d
25 files changed
+1088
-61
No files matched your search
@@ -35,6 +35,14 @@ class Rights:
|
||||
return cls()
|
||||
return cls(bool(row["can_read"]), bool(row["can_edit"]), bool(row["can_share"]), bool(row["can_delete"]))
|
||||
|
||||
def union(self, other: Rights) -> Rights:
|
||||
return Rights(
|
||||
self.read or other.read,
|
||||
self.edit or other.edit,
|
||||
self.share or other.share,
|
||||
self.delete or other.delete,
|
||||
)
|
||||
|
||||
def without(self, blocked: Rights) -> Rights:
|
||||
return Rights(
|
||||
self.read and not blocked.read,
|
||||
@@ -50,6 +58,15 @@ class Rights:
|
||||
return {"read": self.read, "edit": self.edit, "share": self.share, "delete": self.delete}
|
||||
|
||||
|
||||
# The room share: what a room role may do with a case or matter that is shared with the room.
|
||||
# Sharing, deleting and ownership never come from a room role. An unknown role gets nothing.
|
||||
ROOM_RIGHTS = {
|
||||
"reader": Rights(read=True),
|
||||
"member": Rights(read=True, edit=True),
|
||||
"manager": Rights(read=True, edit=True),
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Ctx:
|
||||
kind: str
|
||||
@@ -223,6 +240,29 @@ def _exceptions(conn, actor: UUID, kind: str, resource_id: UUID, context: Ctx |
|
||||
return blocked
|
||||
|
||||
|
||||
def room_default(conn, actor: UUID, resource: dict) -> Rights:
|
||||
"""Rights of the current room members on a case or matter that is shared with the room.
|
||||
|
||||
Only an active room, an active membership and an active account count. The personal
|
||||
room is never shared. Anything unclear gives no rights.
|
||||
"""
|
||||
if resource.get("visibility") != "room":
|
||||
return Rights()
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT m.role
|
||||
FROM room_memberships m
|
||||
JOIN data_rooms r ON r.id = m.room_id AND r.kind = 'organization' AND r.status = 'active'
|
||||
JOIN auth_accounts a ON a.id = m.subject_id AND a.status = 'active'
|
||||
WHERE m.room_id = %s AND m.subject_id = %s AND m.status = 'active'
|
||||
""",
|
||||
(resource["home_scope_id"], actor),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
return Rights()
|
||||
return ROOM_RIGHTS.get(row["role"], Rights())
|
||||
|
||||
|
||||
def case_rights(conn, actor: UUID, case: dict) -> Rights:
|
||||
if case["parent_case_id"] is not None and case["rights_mode"] == "inherit_parent":
|
||||
parent = get_case(conn, case["parent_case_id"])
|
||||
@@ -230,7 +270,7 @@ def case_rights(conn, actor: UUID, case: dict) -> Rights:
|
||||
elif case["owner_subject_id"] == actor:
|
||||
base = Rights.full()
|
||||
else:
|
||||
base = _direct_grant(conn, actor, "case", case["id"])
|
||||
base = _direct_grant(conn, actor, "case", case["id"]).union(room_default(conn, actor, case))
|
||||
return base.without(_exceptions(conn, actor, "case", case["id"], None))
|
||||
|
||||
|
||||
@@ -244,7 +284,10 @@ def _linked(conn, matter_id: UUID, case_id: UUID) -> bool:
|
||||
|
||||
def matter_rights(conn, actor: UUID, matter: dict, context: Ctx | None) -> Rights:
|
||||
if context is None:
|
||||
base = Rights.full() if matter["owner_subject_id"] == actor else _direct_grant(conn, actor, "matter", matter["id"])
|
||||
if matter["owner_subject_id"] == actor:
|
||||
base = Rights.full()
|
||||
else:
|
||||
base = _direct_grant(conn, actor, "matter", matter["id"]).union(room_default(conn, actor, matter))
|
||||
return base.without(_exceptions(conn, actor, "matter", matter["id"], None))
|
||||
|
||||
if context.kind == "case":
|
||||
@@ -571,6 +614,11 @@ def list_cases(conn, actor: UUID) -> list[dict]:
|
||||
AND g.grantee_subject_id = %(actor)s AND NOT g.is_exception AND g.can_read
|
||||
AND g.context_id IS NULL
|
||||
)
|
||||
OR (c.visibility = 'room' AND EXISTS (
|
||||
SELECT 1 FROM room_memberships m
|
||||
JOIN data_rooms r ON r.id = m.room_id AND r.kind = 'organization' AND r.status = 'active'
|
||||
WHERE m.room_id = c.home_scope_id AND m.subject_id = %(actor)s AND m.status = 'active'
|
||||
))
|
||||
OR (
|
||||
c.rights_mode = 'inherit_parent' AND c.parent_case_id IS NOT NULL AND EXISTS (
|
||||
SELECT 1 FROM cases p
|
||||
@@ -583,6 +631,11 @@ def list_cases(conn, actor: UUID) -> list[dict]:
|
||||
AND g.grantee_subject_id = %(actor)s AND NOT g.is_exception
|
||||
AND g.can_read AND g.context_id IS NULL
|
||||
)
|
||||
OR (p.visibility = 'room' AND EXISTS (
|
||||
SELECT 1 FROM room_memberships pm
|
||||
JOIN data_rooms r ON r.id = pm.room_id AND r.kind = 'organization' AND r.status = 'active'
|
||||
WHERE pm.room_id = p.home_scope_id AND pm.subject_id = %(actor)s AND pm.status = 'active'
|
||||
))
|
||||
)
|
||||
)
|
||||
)
|
||||
@@ -613,6 +666,11 @@ def list_matters(conn, actor: UUID) -> list[tuple[dict, list[dict]]]:
|
||||
AND g.grantee_subject_id = %(actor)s AND NOT g.is_exception AND g.can_read
|
||||
AND g.context_id IS NULL
|
||||
)
|
||||
OR (m.visibility = 'room' AND EXISTS (
|
||||
SELECT 1 FROM room_memberships mm
|
||||
JOIN data_rooms r ON r.id = mm.room_id AND r.kind = 'organization' AND r.status = 'active'
|
||||
WHERE mm.room_id = m.home_scope_id AND mm.subject_id = %(actor)s AND mm.status = 'active'
|
||||
))
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM matter_case_links l
|
||||
JOIN cases c ON c.id = l.case_id AND c.deleted_at IS NULL
|
||||
@@ -625,6 +683,11 @@ def list_matters(conn, actor: UUID) -> list[tuple[dict, list[dict]]]:
|
||||
AND g.grantee_subject_id = %(actor)s AND NOT g.is_exception
|
||||
AND g.can_read AND g.context_id IS NULL
|
||||
)
|
||||
OR (c.visibility = 'room' AND EXISTS (
|
||||
SELECT 1 FROM room_memberships cm
|
||||
JOIN data_rooms r ON r.id = cm.room_id AND r.kind = 'organization' AND r.status = 'active'
|
||||
WHERE cm.room_id = c.home_scope_id AND cm.subject_id = %(actor)s AND cm.status = 'active'
|
||||
))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
+177
-6
@@ -15,6 +15,7 @@ from app.access.roles import (
|
||||
count_active_managers,
|
||||
require_operator,
|
||||
require_room_manager,
|
||||
room_role,
|
||||
)
|
||||
from app.api.deps import Principal, get_conn, get_principal
|
||||
from app.api.schemas import (
|
||||
@@ -22,8 +23,11 @@ from app.api.schemas import (
|
||||
MemberIn,
|
||||
MemberRoleIn,
|
||||
OperatorManagerIn,
|
||||
OperatorOwnerIn,
|
||||
OwnerIn,
|
||||
RoomIn,
|
||||
RoomPatchIn,
|
||||
VisibilityIn,
|
||||
)
|
||||
from app.auth.accounts import normalize_email
|
||||
from app.auth.security import hash_secret, new_secret
|
||||
@@ -41,6 +45,18 @@ def _created(path: str, payload: dict) -> JSONResponse:
|
||||
return JSONResponse(status_code=201, content=payload, headers={"Location": path, **_NO_STORE})
|
||||
|
||||
|
||||
def _require_owner(conn, account_id: UUID, room_id: UUID) -> dict:
|
||||
room = _room(conn, room_id)
|
||||
if room["owner_id"] != account_id:
|
||||
raise forbidden("Das darf nur der Eigentümer des Datenraums.")
|
||||
return room
|
||||
|
||||
|
||||
def _protect_owner(room: dict, subject_id: UUID) -> None:
|
||||
if room["owner_id"] == subject_id:
|
||||
raise unprocessable("Der Eigentümer bleibt Verwalter. Übertrage den Datenraum zuerst.")
|
||||
|
||||
|
||||
def _room(conn, room_id: UUID) -> dict:
|
||||
row = conn.execute("SELECT * FROM data_rooms WHERE id = %s AND kind = 'organization'", (room_id,)).fetchone()
|
||||
if row is None:
|
||||
@@ -115,7 +131,7 @@ def rooms(
|
||||
if include_archived:
|
||||
archived = conn.execute(
|
||||
"""
|
||||
SELECT r.id, r.name, m.role
|
||||
SELECT r.id, r.name, m.role, r.owner_id
|
||||
FROM room_memberships m JOIN data_rooms r ON r.id = m.room_id
|
||||
WHERE m.subject_id = %s AND m.status = 'active' AND r.kind = 'organization' AND r.status = 'archived'
|
||||
ORDER BY r.name, r.id
|
||||
@@ -123,7 +139,14 @@ def rooms(
|
||||
(principal.account_id,),
|
||||
).fetchall()
|
||||
items += [
|
||||
{"id": str(row["id"]), "kind": "organization", "name": row["name"], "role": row["role"], "status": "archived"}
|
||||
{
|
||||
"id": str(row["id"]),
|
||||
"kind": "organization",
|
||||
"name": row["name"],
|
||||
"role": row["role"],
|
||||
"status": "archived",
|
||||
"is_owner": row["owner_id"] == principal.account_id,
|
||||
}
|
||||
for row in archived
|
||||
]
|
||||
return {"items": items, "next_cursor": None}
|
||||
@@ -134,15 +157,21 @@ def create_room(body: RoomIn, principal: Principal = Depends(get_principal), con
|
||||
room_id = uuid4()
|
||||
name = body.name.strip()
|
||||
conn.execute(
|
||||
"INSERT INTO data_rooms (id, kind, name, created_by) VALUES (%s, 'organization', %s, %s)",
|
||||
(room_id, name, principal.account_id),
|
||||
"""
|
||||
INSERT INTO data_rooms (id, kind, name, created_by, owner_id, default_visibility)
|
||||
VALUES (%s, 'organization', %s, %s, %s, %s)
|
||||
""",
|
||||
(room_id, name, principal.account_id, principal.account_id, body.default_visibility),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO room_memberships (room_id, subject_id, role) VALUES (%s, %s, 'manager')",
|
||||
(room_id, principal.account_id),
|
||||
)
|
||||
audit(conn, principal.account_id, room_id, "room_created", "room", room_id)
|
||||
return _created(f"/api/v1/rooms/{room_id}", {"id": str(room_id), "kind": "organization", "name": name})
|
||||
return _created(
|
||||
f"/api/v1/rooms/{room_id}",
|
||||
{"id": str(room_id), "kind": "organization", "name": name, "default_visibility": body.default_visibility},
|
||||
)
|
||||
|
||||
|
||||
@router.patch("/api/v1/rooms/{room_id}")
|
||||
@@ -157,7 +186,20 @@ def patch_room(
|
||||
if body.name is not None and body.name.strip() != room["name"]:
|
||||
conn.execute("UPDATE data_rooms SET name = %s WHERE id = %s", (body.name.strip(), room_id))
|
||||
audit(conn, principal.account_id, room_id, "room_renamed", "room", room_id)
|
||||
if body.default_visibility is not None and body.default_visibility != room["default_visibility"]:
|
||||
_require_owner(conn, principal.account_id, room_id)
|
||||
conn.execute("UPDATE data_rooms SET default_visibility = %s WHERE id = %s", (body.default_visibility, room_id))
|
||||
audit(
|
||||
conn,
|
||||
principal.account_id,
|
||||
room_id,
|
||||
"room_default_changed",
|
||||
"room",
|
||||
room_id,
|
||||
detail={"visibility": body.default_visibility},
|
||||
)
|
||||
if body.status is not None and body.status != room["status"]:
|
||||
_require_owner(conn, principal.account_id, room_id)
|
||||
conn.execute("UPDATE data_rooms SET status = %s WHERE id = %s", (body.status, room_id))
|
||||
if body.status == "archived":
|
||||
# Sessions working in the room return to their personal room.
|
||||
@@ -178,12 +220,115 @@ def patch_room(
|
||||
room_id,
|
||||
)
|
||||
row = _room(conn, room_id)
|
||||
return {"id": str(row["id"]), "name": row["name"], "status": row["status"]}
|
||||
return {
|
||||
"id": str(row["id"]),
|
||||
"name": row["name"],
|
||||
"status": row["status"],
|
||||
"default_visibility": row["default_visibility"],
|
||||
}
|
||||
|
||||
|
||||
def _set_owner(conn, actor: UUID, room_id: UUID, new_owner: UUID, previous: UUID | None) -> None:
|
||||
conn.execute("UPDATE data_rooms SET owner_id = %s WHERE id = %s", (new_owner, room_id))
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO room_memberships (room_id, subject_id, role, status)
|
||||
VALUES (%s, %s, 'manager', 'active')
|
||||
ON CONFLICT (room_id, subject_id) DO UPDATE SET role = 'manager', status = 'active'
|
||||
""",
|
||||
(room_id, new_owner),
|
||||
)
|
||||
audit(
|
||||
conn,
|
||||
actor,
|
||||
room_id,
|
||||
"room_owner_changed",
|
||||
"room",
|
||||
room_id,
|
||||
detail={"from": str(previous) if previous else None, "to": str(new_owner)},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/v1/rooms/{room_id}/owner")
|
||||
def transfer_owner(
|
||||
room_id: UUID,
|
||||
body: OwnerIn,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
"""The owner hands the room to an active member. The new owner becomes manager."""
|
||||
room = _require_owner(conn, principal.account_id, room_id)
|
||||
target = _membership(conn, room_id, body.subject_id, lock=True)
|
||||
account = conn.execute("SELECT status FROM auth_accounts WHERE id = %s", (body.subject_id,)).fetchone()
|
||||
if target is None or target["status"] != "active" or account is None or account["status"] != "active":
|
||||
raise unprocessable("Der Datenraum geht nur an ein aktives Mitglied.")
|
||||
if body.subject_id == room["owner_id"]:
|
||||
raise unprocessable("Diese Person ist bereits Eigentümer.")
|
||||
_set_owner(conn, principal.account_id, room_id, body.subject_id, room["owner_id"])
|
||||
return {"owner_id": str(body.subject_id)}
|
||||
|
||||
|
||||
@router.get("/api/v1/rooms/{room_id}/exposure")
|
||||
def exposure(room_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
||||
"""How many of the own cases and matters in this room are private or shared with the room."""
|
||||
_room(conn, room_id)
|
||||
if room_role(conn, principal.account_id, room_id) is None:
|
||||
raise forbidden()
|
||||
result: dict = {}
|
||||
for table, key in (("cases", "cases"), ("matters", "matters")):
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
SELECT visibility, count(*) AS n FROM {table}
|
||||
WHERE home_scope_id = %s AND owner_subject_id = %s AND deleted_at IS NULL
|
||||
GROUP BY visibility
|
||||
""",
|
||||
(room_id, principal.account_id),
|
||||
).fetchall()
|
||||
counts = {"private": 0, "room": 0}
|
||||
counts.update({row["visibility"]: row["n"] for row in rows})
|
||||
result[key] = counts
|
||||
return JSONResponse(result, headers=_NO_STORE)
|
||||
|
||||
|
||||
@router.post("/api/v1/rooms/{room_id}/my-content/visibility")
|
||||
def set_my_content(
|
||||
room_id: UUID,
|
||||
body: VisibilityIn,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
"""Set the room share of every own case and matter in this room at once."""
|
||||
_room(conn, room_id)
|
||||
if room_role(conn, principal.account_id, room_id) is None:
|
||||
raise forbidden()
|
||||
changed = 0
|
||||
for table in ("cases", "matters"):
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
UPDATE {table} SET visibility = %s
|
||||
WHERE home_scope_id = %s AND owner_subject_id = %s AND deleted_at IS NULL AND visibility <> %s
|
||||
RETURNING id
|
||||
""",
|
||||
(body.visibility, room_id, principal.account_id, body.visibility),
|
||||
).fetchall()
|
||||
changed += len(rows)
|
||||
if changed:
|
||||
audit(
|
||||
conn,
|
||||
principal.account_id,
|
||||
room_id,
|
||||
"visibility_changed",
|
||||
"room",
|
||||
room_id,
|
||||
detail={"visibility": body.visibility, "count": changed},
|
||||
)
|
||||
return {"changed": changed}
|
||||
|
||||
|
||||
@router.get("/api/v1/rooms/{room_id}/members")
|
||||
def members(room_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
||||
require_room_manager(conn, principal.account_id, room_id)
|
||||
owner_id = _room(conn, room_id)["owner_id"]
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT m.subject_id, m.role, a.email, a.display_name, a.status
|
||||
@@ -202,6 +347,7 @@ def members(room_id: UUID, principal: Principal = Depends(get_principal), conn:
|
||||
"display_name": row["display_name"],
|
||||
"role": row["role"],
|
||||
"account_status": row["status"],
|
||||
"is_owner": row["subject_id"] == owner_id,
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
@@ -220,6 +366,8 @@ def add_member(
|
||||
if account is None or account["status"] != "active":
|
||||
raise not_found()
|
||||
existing = _membership(conn, room_id, account["id"], lock=True)
|
||||
if body.role != "manager":
|
||||
_protect_owner(_room(conn, room_id), account["id"])
|
||||
if existing and existing["status"] == "active" and existing["role"] == "manager" and body.role != "manager":
|
||||
if count_active_managers(conn, room_id, lock=True) <= 1:
|
||||
raise unprocessable("Der letzte Verwalter bleibt Verwalter.")
|
||||
@@ -243,6 +391,7 @@ def change_member_role(
|
||||
raise not_found()
|
||||
if current["role"] == body.role:
|
||||
return {"subject_id": str(subject_id), "role": body.role}
|
||||
_protect_owner(_room(conn, room_id), subject_id)
|
||||
if current["role"] == "manager" and count_active_managers(conn, room_id) <= 1:
|
||||
raise unprocessable("Der letzte Verwalter bleibt Verwalter.")
|
||||
_set_role(conn, principal.account_id, room_id, subject_id, body.role, added=False)
|
||||
@@ -261,6 +410,7 @@ def remove_member(
|
||||
current = _membership(conn, room_id, subject_id, lock=True)
|
||||
if current is None or current["status"] != "active":
|
||||
raise not_found()
|
||||
_protect_owner(_room(conn, room_id), subject_id)
|
||||
if current["role"] == "manager" and count_active_managers(conn, room_id) <= 1:
|
||||
raise unprocessable("Der letzte Verwalter kann den Datenraum nicht verlassen.")
|
||||
conn.execute(
|
||||
@@ -411,6 +561,27 @@ def admin_assign_manager(
|
||||
return {"subject_id": str(account["id"]), "role": "manager"}
|
||||
|
||||
|
||||
@router.post("/api/v1/admin/rooms/{room_id}/owner")
|
||||
def admin_set_owner(
|
||||
room_id: UUID,
|
||||
body: OperatorOwnerIn,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
"""The operator steps in only when the owner can no longer act: disabled or not a member any more."""
|
||||
require_operator(conn, principal.account_id)
|
||||
room = _room(conn, room_id)
|
||||
owner = conn.execute("SELECT status FROM auth_accounts WHERE id = %s", (room["owner_id"],)).fetchone()
|
||||
active_owner = bool(owner and owner["status"] == "active" and room_role(conn, room["owner_id"], room_id))
|
||||
if active_owner:
|
||||
raise forbidden("Der Eigentümer ist aktiv. Er überträgt den Datenraum selbst.")
|
||||
account = account_by_email(conn, body.email)
|
||||
if account is None or account["status"] != "active":
|
||||
raise not_found()
|
||||
_set_owner(conn, principal.account_id, room_id, account["id"], room["owner_id"])
|
||||
return {"owner_id": str(account["id"])}
|
||||
|
||||
|
||||
_ADMIN_KINDS = ("account", "session", "room", "membership", "invitation")
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
"""Room share of a case or matter: shared with the room members by role, or private."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi.responses import JSONResponse
|
||||
from psycopg import Connection
|
||||
|
||||
from app.access.policy import ROOM_RIGHTS
|
||||
from app.api.deps import Principal, get_conn, get_principal
|
||||
from app.api.schemas import ExclusionIn, VisibilityIn
|
||||
from app.ops import _shareable, set_person_exclusion, set_visibility
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
_NO_STORE = {"Cache-Control": "no-store"}
|
||||
|
||||
|
||||
def _view(conn, actor: UUID, kind: str, resource_id: UUID) -> dict:
|
||||
resource = _shareable(conn, actor, kind, resource_id)
|
||||
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, resource["home_scope_id"], actor),
|
||||
).fetchall()
|
||||
return {
|
||||
"id": str(resource_id),
|
||||
"visibility": resource["visibility"],
|
||||
"members": [
|
||||
{
|
||||
"subject_id": str(row["subject_id"]),
|
||||
"display_name": row["display_name"],
|
||||
"role": row["role"],
|
||||
"excluded": row["excluded"],
|
||||
"rights": {
|
||||
"read": ROOM_RIGHTS[row["role"]].read if row["role"] in ROOM_RIGHTS else False,
|
||||
"edit": ROOM_RIGHTS[row["role"]].edit if row["role"] in ROOM_RIGHTS else False,
|
||||
},
|
||||
}
|
||||
for row in members
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
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, name=f"put_{kind}_room_share")
|
||||
def put_share(
|
||||
resource_id: UUID,
|
||||
body: VisibilityIn,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
return set_visibility(conn, principal.account_id, kind, resource_id, body.visibility)
|
||||
|
||||
@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")
|
||||
|
||||
|
||||
@@ -65,6 +65,23 @@ class InvitationIn(APIModel):
|
||||
|
||||
class RoomIn(APIModel):
|
||||
name: str = Field(min_length=1, max_length=160)
|
||||
default_visibility: Literal["private", "room"] = "room"
|
||||
|
||||
|
||||
class VisibilityIn(APIModel):
|
||||
visibility: Literal["private", "room"]
|
||||
|
||||
|
||||
class ExclusionIn(APIModel):
|
||||
excluded: bool
|
||||
|
||||
|
||||
class OwnerIn(APIModel):
|
||||
subject_id: UUID
|
||||
|
||||
|
||||
class OperatorOwnerIn(APIModel):
|
||||
email: Email
|
||||
|
||||
|
||||
class MemberIn(APIModel):
|
||||
@@ -79,6 +96,7 @@ class MemberRoleIn(APIModel):
|
||||
class RoomPatchIn(APIModel):
|
||||
name: str | None = Field(default=None, min_length=1, max_length=160)
|
||||
status: Literal["active", "archived"] | None = None
|
||||
default_visibility: Literal["private", "room"] | None = None
|
||||
|
||||
|
||||
class OperatorManagerIn(APIModel):
|
||||
|
||||
@@ -68,10 +68,10 @@ def create_account(
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO data_rooms (id, kind, name, created_by)
|
||||
VALUES (%s, 'personal', 'Persönlicher Raum', %s)
|
||||
INSERT INTO data_rooms (id, kind, name, created_by, owner_id)
|
||||
VALUES (%s, 'personal', 'Persönlicher Raum', %s, %s)
|
||||
""",
|
||||
(account_id, account_id),
|
||||
(account_id, account_id, account_id),
|
||||
)
|
||||
return conn.execute(
|
||||
"""
|
||||
@@ -141,6 +141,12 @@ def set_status(conn, actor: UUID, account_id: UUID, status: str) -> None:
|
||||
if status == "disabled":
|
||||
if account_id == actor:
|
||||
raise unprocessable("Du kannst dein eigenes Konto nicht deaktivieren.")
|
||||
owned = _owned_shared_rooms(conn, account_id)
|
||||
if owned:
|
||||
raise unprocessable(
|
||||
f"Das Konto ist Eigentümer von {owned} Datenraum(en) mit weiteren Mitgliedern. "
|
||||
"Der Eigentümer überträgt sie zuerst, oder der Betreiber setzt einen neuen Eigentümer ein."
|
||||
)
|
||||
if account["is_superadmin"] and operators <= 1:
|
||||
raise unprocessable("Der letzte Betreiber lässt sich nicht deaktivieren.")
|
||||
conn.execute(
|
||||
@@ -237,6 +243,21 @@ def clear_login_attempts(conn, bucket: str) -> None:
|
||||
conn.execute("DELETE FROM auth_login_attempts WHERE bucket = %s", (bucket,))
|
||||
|
||||
|
||||
def _owned_shared_rooms(conn, account_id: UUID) -> int:
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT count(*) AS n FROM data_rooms r
|
||||
WHERE r.kind = 'organization' AND r.status = 'active' AND r.owner_id = %s
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM room_memberships m
|
||||
WHERE m.room_id = r.id AND m.status = 'active' AND m.subject_id <> %s
|
||||
)
|
||||
""",
|
||||
(account_id, account_id),
|
||||
).fetchone()
|
||||
return int(row["n"])
|
||||
|
||||
|
||||
def list_rooms(conn, account_id: UUID) -> list[dict]:
|
||||
personal = conn.execute(
|
||||
"SELECT id, kind, name FROM data_rooms WHERE id = %s AND kind = 'personal'",
|
||||
@@ -247,7 +268,8 @@ def list_rooms(conn, account_id: UUID) -> list[dict]:
|
||||
rooms.append({"id": str(personal["id"]), "kind": personal["kind"], "name": personal["name"], "role": "owner"})
|
||||
shared = conn.execute(
|
||||
"""
|
||||
SELECT r.id, r.kind, r.name, m.role
|
||||
SELECT r.id, r.kind, r.name, m.role, r.owner_id, r.default_visibility,
|
||||
(SELECT count(*) FROM room_memberships x WHERE x.room_id = r.id AND x.status = 'active') AS members
|
||||
FROM room_memberships m
|
||||
JOIN data_rooms r ON r.id = m.room_id
|
||||
WHERE m.subject_id = %s AND m.status = 'active' AND r.status = 'active' AND r.kind = 'organization'
|
||||
@@ -256,5 +278,15 @@ def list_rooms(conn, account_id: UUID) -> list[dict]:
|
||||
(account_id,),
|
||||
).fetchall()
|
||||
for room in shared:
|
||||
rooms.append({"id": str(room["id"]), "kind": room["kind"], "name": room["name"], "role": room["role"]})
|
||||
rooms.append(
|
||||
{
|
||||
"id": str(room["id"]),
|
||||
"kind": room["kind"],
|
||||
"name": room["name"],
|
||||
"role": room["role"],
|
||||
"is_owner": room["owner_id"] == account_id,
|
||||
"private": room["members"] <= 1,
|
||||
"default_visibility": room["default_visibility"],
|
||||
}
|
||||
)
|
||||
return rooms
|
||||
@@ -13,6 +13,7 @@ from app.api.accounts import router as accounts_router
|
||||
from app.api.correspondence import router as correspondence_router
|
||||
from app.api.export import router as export_router
|
||||
from app.api.rooms import router as rooms_router
|
||||
from app.api.roomshare import router as roomshare_router
|
||||
from app.api.routes import router
|
||||
from app.api.sources import router as sources_router
|
||||
from app.core.errors import Problem, problem_response
|
||||
@@ -120,6 +121,7 @@ def create_app() -> FastAPI:
|
||||
app.include_router(router)
|
||||
app.include_router(accounts_router)
|
||||
app.include_router(rooms_router)
|
||||
app.include_router(roomshare_router)
|
||||
app.include_router(sources_router)
|
||||
app.include_router(export_router)
|
||||
app.include_router(correspondence_router)
|
||||
|
||||
+96
-15
@@ -74,6 +74,81 @@ def _placement(context: Ctx, placement: Ctx | None) -> tuple[Ctx, Ctx]:
|
||||
return target, context if context.kind == "case" or placement is not None else None
|
||||
|
||||
|
||||
def default_visibility(conn, scope_id: UUID) -> str:
|
||||
"""New cases and matters follow the room preset. The personal room is always private."""
|
||||
room = conn.execute("SELECT kind, default_visibility FROM data_rooms WHERE id = %s", (scope_id,)).fetchone()
|
||||
if room is not None and room["kind"] == "organization" and room["default_visibility"] == "room":
|
||||
return "room"
|
||||
return "private"
|
||||
|
||||
|
||||
def _shareable(conn, actor: UUID, kind: str, resource_id: UUID) -> dict:
|
||||
"""The own case or matter of an organization room. Only the owner changes its room share."""
|
||||
if kind == "case":
|
||||
resource = get_case(conn, resource_id)
|
||||
elif kind == "matter":
|
||||
resource = get_matter(conn, resource_id)
|
||||
else:
|
||||
raise unprocessable("Die Raumfreigabe gilt für Akten und Vorgänge.")
|
||||
if resource is None or resource["owner_subject_id"] != actor:
|
||||
raise not_found()
|
||||
room = conn.execute("SELECT kind FROM data_rooms WHERE id = %s", (resource["home_scope_id"],)).fetchone()
|
||||
if room is None or room["kind"] != "organization":
|
||||
raise unprocessable("Im persönlichen Datenraum gibt es keine Raumfreigabe.")
|
||||
return resource
|
||||
|
||||
|
||||
def set_visibility(conn, actor: UUID, kind: str, resource_id: UUID, value: str) -> dict:
|
||||
if value not in {"private", "room"}:
|
||||
raise unprocessable("Die Sichtbarkeit ist ungültig.")
|
||||
resource = _shareable(conn, actor, kind, resource_id)
|
||||
table = "cases" if kind == "case" else "matters"
|
||||
if resource["visibility"] != value:
|
||||
conn.execute(f"UPDATE {table} SET visibility = %s WHERE id = %s", (value, resource_id))
|
||||
audit(conn, actor, resource["home_scope_id"], "visibility_changed", kind, resource_id, detail={"visibility": value})
|
||||
return {"id": str(resource_id), "visibility": value}
|
||||
|
||||
|
||||
def set_person_exclusion(conn, actor: UUID, kind: str, resource_id: UUID, subject_id: UUID, excluded: bool) -> None:
|
||||
"""Take one room member out of the room share of an own case or matter."""
|
||||
resource = _shareable(conn, actor, kind, resource_id)
|
||||
if subject_id == actor:
|
||||
raise unprocessable("Du kannst dich nicht selbst ausnehmen.")
|
||||
member = conn.execute(
|
||||
"SELECT 1 FROM room_memberships WHERE room_id = %s AND subject_id = %s AND status = 'active'",
|
||||
(resource["home_scope_id"], subject_id),
|
||||
).fetchone()
|
||||
if member is None:
|
||||
raise not_found()
|
||||
conn.execute(
|
||||
"""
|
||||
DELETE FROM access_grants
|
||||
WHERE is_exception AND grantee_subject_id = %s AND resource_kind = %s AND resource_id = %s
|
||||
AND context_kind IS NULL
|
||||
""",
|
||||
(subject_id, kind, resource_id),
|
||||
)
|
||||
if excluded:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO access_grants (
|
||||
grantee_subject_id, resource_kind, resource_id,
|
||||
can_read, can_edit, can_share, can_delete, is_exception, created_by
|
||||
)
|
||||
VALUES (%s, %s, %s, true, true, true, true, true, %s)
|
||||
""",
|
||||
(subject_id, kind, resource_id, actor),
|
||||
)
|
||||
audit(
|
||||
conn,
|
||||
actor,
|
||||
resource["home_scope_id"],
|
||||
"person_excluded" if excluded else "person_included",
|
||||
kind,
|
||||
resource_id,
|
||||
)
|
||||
|
||||
|
||||
def create_case(conn, actor: UUID, scope_id: UUID, title: str, parent_case_id: UUID | None, rights_mode: str) -> dict:
|
||||
ensure_can_create(conn, actor, scope_id)
|
||||
title = title.strip()
|
||||
@@ -82,12 +157,12 @@ def create_case(conn, actor: UUID, scope_id: UUID, title: str, parent_case_id: U
|
||||
"""
|
||||
INSERT INTO cases (
|
||||
home_scope_id, owner_subject_id, created_by, title, rights_mode,
|
||||
content_order, content_order_dir
|
||||
content_order, content_order_dir, visibility
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, 'explicit', 'date', 'asc')
|
||||
VALUES (%s, %s, %s, %s, 'explicit', 'date', 'asc', %s)
|
||||
RETURNING *
|
||||
""",
|
||||
(scope_id, actor, actor, title),
|
||||
(scope_id, actor, actor, title, default_visibility(conn, scope_id)),
|
||||
).fetchone()
|
||||
else:
|
||||
parent, _rights = require_case(conn, actor, parent_case_id, "edit")
|
||||
@@ -99,12 +174,15 @@ def create_case(conn, actor: UUID, scope_id: UUID, title: str, parent_case_id: U
|
||||
"""
|
||||
INSERT INTO cases (
|
||||
home_scope_id, owner_subject_id, created_by, title, parent_case_id, rights_mode,
|
||||
content_order, content_order_dir
|
||||
content_order, content_order_dir, visibility
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, 'date', 'asc')
|
||||
VALUES (%s, %s, %s, %s, %s, %s, 'date', 'asc', %s)
|
||||
RETURNING *
|
||||
""",
|
||||
(parent["home_scope_id"], actor, actor, title, parent["id"], rights_mode),
|
||||
(
|
||||
parent["home_scope_id"], actor, actor, title, parent["id"], rights_mode,
|
||||
default_visibility(conn, parent["home_scope_id"]),
|
||||
),
|
||||
).fetchone()
|
||||
audit(conn, actor, row["home_scope_id"], "case_created", "case", row["id"])
|
||||
return row
|
||||
@@ -347,11 +425,11 @@ def create_matter(conn, actor: UUID, scope_id: UUID, title: str, parent_matter_i
|
||||
row = conn.execute(
|
||||
"""
|
||||
INSERT INTO matters (
|
||||
home_scope_id, owner_subject_id, created_by, title, content_order, content_order_dir
|
||||
home_scope_id, owner_subject_id, created_by, title, content_order, content_order_dir, visibility
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, 'date', 'asc') RETURNING *
|
||||
VALUES (%s, %s, %s, %s, 'date', 'asc', %s) RETURNING *
|
||||
""",
|
||||
(scope_id, actor, actor, title),
|
||||
(scope_id, actor, actor, title, default_visibility(conn, scope_id)),
|
||||
).fetchone()
|
||||
else:
|
||||
parent, _rights = require_matter(conn, actor, parent_matter_id, context, "edit")
|
||||
@@ -361,11 +439,11 @@ def create_matter(conn, actor: UUID, scope_id: UUID, title: str, parent_matter_i
|
||||
"""
|
||||
INSERT INTO matters (
|
||||
home_scope_id, owner_subject_id, created_by, title, parent_matter_id,
|
||||
content_order, content_order_dir
|
||||
content_order, content_order_dir, visibility
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, %s, 'date', 'asc') RETURNING *
|
||||
VALUES (%s, %s, %s, %s, %s, 'date', 'asc', %s) RETURNING *
|
||||
""",
|
||||
(parent["home_scope_id"], actor, actor, title, parent["id"]),
|
||||
(parent["home_scope_id"], actor, actor, title, parent["id"], default_visibility(conn, parent["home_scope_id"])),
|
||||
).fetchone()
|
||||
audit(conn, actor, row["home_scope_id"], "matter_created", "matter", row["id"], context)
|
||||
return row
|
||||
@@ -520,8 +598,9 @@ def move_bindings_chapter(
|
||||
|
||||
|
||||
def delete_case(conn, actor: UUID, case_id: UUID, expected: int) -> None:
|
||||
case, rights = require_case(conn, actor, case_id, "edit")
|
||||
if not rights.edit:
|
||||
case, rights = require_case(conn, actor, case_id, "read")
|
||||
# Deleting the container is the owner's right. Editing, also through the room share, is not enough.
|
||||
if case["owner_subject_id"] != actor and not rights.delete:
|
||||
raise forbidden()
|
||||
children = conn.execute(
|
||||
"SELECT id FROM cases WHERE parent_case_id = %s AND deleted_at IS NULL",
|
||||
@@ -536,7 +615,9 @@ def delete_case(conn, actor: UUID, case_id: UUID, expected: int) -> None:
|
||||
|
||||
|
||||
def delete_matter(conn, actor: UUID, matter_id: UUID, expected: int, context: Ctx | None) -> None:
|
||||
matter, _rights = require_matter(conn, actor, matter_id, context, "edit")
|
||||
matter, rights = require_matter(conn, actor, matter_id, context, "read")
|
||||
if matter["owner_subject_id"] != actor and not rights.delete:
|
||||
raise forbidden()
|
||||
children = conn.execute(
|
||||
"SELECT id FROM matters WHERE parent_matter_id = %s AND deleted_at IS NULL",
|
||||
(matter_id,),
|
||||
|
||||
@@ -634,6 +634,7 @@ def case_summary(case: dict, rights) -> dict:
|
||||
"rights_mode": case["rights_mode"],
|
||||
"parent_case_id": str(case["parent_case_id"]) if case["parent_case_id"] else None,
|
||||
"home_scope_id": str(case["home_scope_id"]),
|
||||
"visibility": case.get("visibility", "private"),
|
||||
"rights": rights.as_dict(),
|
||||
}
|
||||
|
||||
@@ -779,6 +780,7 @@ def matter_detail(conn, actor: UUID, matter: dict, rights, context: Ctx | None)
|
||||
"parent_title": matter_title_if_readable(conn, actor, matter["parent_matter_id"]),
|
||||
"home_scope_id": str(matter["home_scope_id"]),
|
||||
"home_scope_name": room_name(conn, matter["home_scope_id"]),
|
||||
"visibility": matter.get("visibility", "private"),
|
||||
"is_owner": matter["owner_subject_id"] == actor,
|
||||
"content_order": matter.get("content_order") or "position",
|
||||
"content_order_dir": matter.get("content_order_dir") or "asc",
|
||||
@@ -1052,4 +1054,9 @@ LABELS = {
|
||||
"invitation_created": "Einladung erstellt",
|
||||
"invitation_revoked": "Einladung widerrufen",
|
||||
"invitation_accepted": "Einladung angenommen",
|
||||
"visibility_changed": "Raumfreigabe geändert",
|
||||
"person_excluded": "Person von der Raumfreigabe ausgenommen",
|
||||
"person_included": "Person wieder in die Raumfreigabe aufgenommen",
|
||||
"room_owner_changed": "Eigentümer des Datenraums gewechselt",
|
||||
"room_default_changed": "Voreinstellung des Datenraums geändert",
|
||||
}
|
||||
+3
-3
@@ -87,11 +87,11 @@ def seed_demo(settings: Settings | None = None) -> bool:
|
||||
ben = create_account(conn, BEN_EMAIL, "Ben Beispiel", BEN_PASSWORD, origin="fixture")
|
||||
room = conn.execute(
|
||||
"""
|
||||
INSERT INTO data_rooms (id, kind, name, created_by)
|
||||
VALUES (gen_random_uuid(), 'organization', 'Haushalt Musterstraße', %s)
|
||||
INSERT INTO data_rooms (id, kind, name, created_by, owner_id)
|
||||
VALUES (gen_random_uuid(), 'organization', 'Haushalt Musterstraße', %s, %s)
|
||||
RETURNING id
|
||||
""",
|
||||
(ada["id"],),
|
||||
(ada["id"], ada["id"]),
|
||||
).fetchone()
|
||||
conn.execute(
|
||||
"INSERT INTO room_memberships (room_id, subject_id, role) VALUES (%s, %s, 'manager'), (%s, %s, 'member')",
|
||||
|
||||
Reference in new issue
Block a user