Mache Datenräume zu Teams, damit Akten und Vorgänge mit beliebigen Räumen geteilt werden können.
Die Raumfreigabe hängt nicht mehr am Heimatraum. Der Owner teilt eine Akte, Unterakte oder einen Vorgang mit jedem Raum, in dem er schreiben darf; die Mitglieder erhalten Rechte nach ihrer Rolle, begrenzbar auf nur lesen, mit Personenausnahme. Ein einzelner Vorgang lässt sich teilen, ohne die Akte oder die anderen Vorgänge offenzulegen. Wer ein Team verlässt oder Leser wird, nimmt seine Freigaben mit; wer entfernt wird, arbeitet danach im persönlichen Raum und sieht weiter alles, was ihm gehört. Der Heimatraum ist nur noch Ablage. Die Migration übernimmt die bisherigen Raumfreigaben in die neue Tabelle room_shares und wurde mit Bestandsdaten geprüft. Die Sitzung meldet einen Raumwechsel jetzt mit eigenem Fehlercode, die Oberfläche lädt sie dann neu. Die lokale Suite läuft mit 132 Tests grün, der Frontend-Build 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
1c2f045e7d
commit
21c2d05fb8
19 files changed
+634
-297
No files matched your search
@@ -240,27 +240,33 @@ 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.
|
||||
def room_default(conn, actor: UUID, kind: str, resource_id: UUID) -> Rights:
|
||||
"""Rights of the current members of the rooms a case or matter is shared with.
|
||||
|
||||
Only an active room, an active membership and an active account count. The personal
|
||||
room is never shared. Anything unclear gives no rights.
|
||||
A room is a team. The rights of one share are the role of the person in that room,
|
||||
capped by the share (`read` or `edit`). Several shares add up. Only an active room,
|
||||
an active membership and an active account count. Anything unclear gives no rights.
|
||||
"""
|
||||
if resource.get("visibility") != "room":
|
||||
return Rights()
|
||||
row = conn.execute(
|
||||
rows = 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'
|
||||
SELECT m.role, s.access
|
||||
FROM room_shares s
|
||||
JOIN room_memberships m ON m.room_id = s.room_id AND m.subject_id = %s AND m.status = 'active'
|
||||
JOIN data_rooms r ON r.id = s.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'
|
||||
WHERE s.resource_kind = %s AND s.resource_id = %s
|
||||
""",
|
||||
(resource["home_scope_id"], actor),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
return Rights()
|
||||
return ROOM_RIGHTS.get(row["role"], Rights())
|
||||
(actor, kind, resource_id),
|
||||
).fetchall()
|
||||
result = Rights()
|
||||
for row in rows:
|
||||
rights = ROOM_RIGHTS.get(row["role"], Rights())
|
||||
if row["access"] == "read":
|
||||
rights = Rights(read=rights.read)
|
||||
elif row["access"] != "edit":
|
||||
rights = Rights()
|
||||
result = result.union(rights)
|
||||
return result
|
||||
|
||||
|
||||
def case_rights(conn, actor: UUID, case: dict) -> Rights:
|
||||
@@ -270,7 +276,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"]).union(room_default(conn, actor, case))
|
||||
base = _direct_grant(conn, actor, "case", case["id"]).union(room_default(conn, actor, "case", case["id"]))
|
||||
return base.without(_exceptions(conn, actor, "case", case["id"], None))
|
||||
|
||||
|
||||
@@ -287,7 +293,7 @@ def matter_rights(conn, actor: UUID, matter: dict, context: Ctx | None) -> Right
|
||||
if matter["owner_subject_id"] == actor:
|
||||
base = Rights.full()
|
||||
else:
|
||||
base = _direct_grant(conn, actor, "matter", matter["id"]).union(room_default(conn, actor, matter))
|
||||
base = _direct_grant(conn, actor, "matter", matter["id"]).union(room_default(conn, actor, "matter", matter["id"]))
|
||||
return base.without(_exceptions(conn, actor, "matter", matter["id"], None))
|
||||
|
||||
if context.kind == "case":
|
||||
@@ -614,10 +620,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 (EXISTS (
|
||||
SELECT 1 FROM room_shares sh
|
||||
JOIN room_memberships sm ON sm.room_id = sh.room_id AND sm.subject_id = %(actor)s AND sm.status = 'active'
|
||||
JOIN data_rooms sr ON sr.id = sh.room_id AND sr.kind = 'organization' AND sr.status = 'active'
|
||||
WHERE sh.resource_kind = 'case' AND sh.resource_id = c.id
|
||||
))
|
||||
OR (
|
||||
c.rights_mode = 'inherit_parent' AND c.parent_case_id IS NOT NULL AND EXISTS (
|
||||
@@ -631,10 +638,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'
|
||||
OR (EXISTS (
|
||||
SELECT 1 FROM room_shares sh
|
||||
JOIN room_memberships sm ON sm.room_id = sh.room_id AND sm.subject_id = %(actor)s AND sm.status = 'active'
|
||||
JOIN data_rooms sr ON sr.id = sh.room_id AND sr.kind = 'organization' AND sr.status = 'active'
|
||||
WHERE sh.resource_kind = 'case' AND sh.resource_id = p.id
|
||||
))
|
||||
)
|
||||
)
|
||||
@@ -666,10 +674,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 room_shares sh
|
||||
JOIN room_memberships sm ON sm.room_id = sh.room_id AND sm.subject_id = %(actor)s AND sm.status = 'active'
|
||||
JOIN data_rooms sr ON sr.id = sh.room_id AND sr.kind = 'organization' AND sr.status = 'active'
|
||||
WHERE sh.resource_kind = 'matter' AND sh.resource_id = m.id
|
||||
))
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM matter_case_links l
|
||||
@@ -683,10 +692,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'
|
||||
OR (EXISTS (
|
||||
SELECT 1 FROM room_shares sh
|
||||
JOIN room_memberships sm ON sm.room_id = sh.room_id AND sm.subject_id = %(actor)s AND sm.status = 'active'
|
||||
JOIN data_rooms sr ON sr.id = sh.room_id AND sr.kind = 'organization' AND sr.status = 'active'
|
||||
WHERE sh.resource_kind = 'case' AND sh.resource_id = c.id
|
||||
))
|
||||
)
|
||||
)
|
||||
|
||||
@@ -21,7 +21,7 @@ from psycopg import Connection
|
||||
|
||||
from app.access.policy import ensure_member_or_personal
|
||||
from app.auth.security import hash_secret, read_session_token
|
||||
from app.core.errors import forbidden, password_change_required, unauthorized
|
||||
from app.core.errors import forbidden, password_change_required, scope_changed, unauthorized
|
||||
from app.core.timeutil import utcnow
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@ def get_principal(request: Request, conn: Connection = Depends(get_conn)) -> Pri
|
||||
except ValueError as exc:
|
||||
raise forbidden("Der Datenraum ist nicht gewählt.") from exc
|
||||
if kind != row["scope_kind"] or scope_id != row["scope_id"]:
|
||||
raise forbidden("Der gewählte Datenraum entspricht nicht mehr der Sitzung.")
|
||||
raise scope_changed()
|
||||
room = ensure_member_or_personal(conn, row["account_id"], kind, scope_id)
|
||||
return Principal(
|
||||
account_id=row["account_id"],
|
||||
|
||||
+71
-23
@@ -95,6 +95,21 @@ def _drop_edit_rights_in_room(conn, subject_id: UUID, room_id: UUID) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _drop_shares_of(conn, room_id: UUID, subject_id: UUID) -> None:
|
||||
"""Whoever leaves a team, or may no longer write in it, takes the shares of their own entries with them."""
|
||||
conn.execute(
|
||||
"""
|
||||
DELETE FROM room_shares s
|
||||
WHERE s.room_id = %s
|
||||
AND (
|
||||
(s.resource_kind = 'case' AND s.resource_id IN (SELECT id FROM cases WHERE owner_subject_id = %s))
|
||||
OR (s.resource_kind = 'matter' AND s.resource_id IN (SELECT id FROM matters WHERE owner_subject_id = %s))
|
||||
)
|
||||
""",
|
||||
(room_id, subject_id, subject_id),
|
||||
)
|
||||
|
||||
|
||||
def _set_role(conn, actor: UUID, room_id: UUID, subject_id: UUID, role: str, *, added: bool) -> None:
|
||||
if role not in ROOM_ROLES:
|
||||
raise unprocessable("Diese Raumrolle gibt es nicht.")
|
||||
@@ -108,6 +123,7 @@ def _set_role(conn, actor: UUID, room_id: UUID, subject_id: UUID, role: str, *,
|
||||
)
|
||||
if role == "reader":
|
||||
_drop_edit_rights_in_room(conn, subject_id, room_id)
|
||||
_drop_shares_of(conn, room_id, subject_id)
|
||||
_audit_membership(conn, actor, room_id, subject_id, "membership_added" if added else "membership_role_changed", role)
|
||||
|
||||
|
||||
@@ -270,23 +286,27 @@ def transfer_owner(
|
||||
|
||||
@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."""
|
||||
"""Own cases and matters: shared with this room (`room`) or filed here but not shared (`private`)."""
|
||||
_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(
|
||||
for table, kind in (("cases", "case"), ("matters", "matter")):
|
||||
row = 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
|
||||
SELECT
|
||||
count(*) FILTER (WHERE EXISTS (
|
||||
SELECT 1 FROM room_shares s
|
||||
WHERE s.resource_kind = %s AND s.resource_id = t.id AND s.room_id = %s)) AS shared,
|
||||
count(*) FILTER (WHERE t.home_scope_id = %s AND NOT EXISTS (
|
||||
SELECT 1 FROM room_shares s
|
||||
WHERE s.resource_kind = %s AND s.resource_id = t.id AND s.room_id = %s)) AS filed
|
||||
FROM {table} t
|
||||
WHERE t.owner_subject_id = %s AND t.deleted_at IS NULL
|
||||
""",
|
||||
(room_id, principal.account_id),
|
||||
).fetchall()
|
||||
counts = {"private": 0, "room": 0}
|
||||
counts.update({row["visibility"]: row["n"] for row in rows})
|
||||
result[key] = counts
|
||||
(kind, room_id, room_id, kind, room_id, principal.account_id),
|
||||
).fetchone()
|
||||
result[table] = {"room": row["shared"], "private": row["filed"]}
|
||||
return JSONResponse(result, headers=_NO_STORE)
|
||||
|
||||
|
||||
@@ -297,21 +317,39 @@ def set_my_content(
|
||||
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`: share every own entry filed in this room with it. `private`: end all own shares to it."""
|
||||
_room(conn, room_id)
|
||||
if room_role(conn, principal.account_id, room_id) is None:
|
||||
role = room_role(conn, principal.account_id, room_id)
|
||||
if role 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 body.visibility == "room":
|
||||
if role not in {"member", "manager"}:
|
||||
raise forbidden("Als Leser kannst du nichts mit dem Datenraum teilen.")
|
||||
for table, kind in (("cases", "case"), ("matters", "matter")):
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
INSERT INTO room_shares (resource_kind, resource_id, room_id, access, created_by)
|
||||
SELECT %s, t.id, %s, 'edit', %s FROM {table} t
|
||||
WHERE t.home_scope_id = %s AND t.owner_subject_id = %s AND t.deleted_at IS NULL
|
||||
ON CONFLICT DO NOTHING
|
||||
RETURNING id
|
||||
""",
|
||||
(kind, room_id, principal.account_id, room_id, principal.account_id),
|
||||
).fetchall()
|
||||
changed += len(rows)
|
||||
else:
|
||||
for table, kind in (("cases", "case"), ("matters", "matter")):
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
DELETE FROM room_shares s
|
||||
WHERE s.room_id = %s AND s.resource_kind = %s
|
||||
AND s.resource_id IN (SELECT id FROM {table} WHERE owner_subject_id = %s)
|
||||
RETURNING s.id
|
||||
""",
|
||||
(room_id, kind, principal.account_id),
|
||||
).fetchall()
|
||||
changed += len(rows)
|
||||
if changed:
|
||||
audit(
|
||||
conn,
|
||||
@@ -417,6 +455,16 @@ def remove_member(
|
||||
"UPDATE room_memberships SET status = 'removed' WHERE room_id = %s AND subject_id = %s",
|
||||
(room_id, subject_id),
|
||||
)
|
||||
_drop_shares_of(conn, room_id, subject_id)
|
||||
# The removed person works in the personal room again; own entries stay visible there.
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE auth_sessions
|
||||
SET scope_kind = 'personal', scope_id = account_id, epoch = epoch + 1
|
||||
WHERE account_id = %s AND scope_id = %s AND revoked_at IS NULL
|
||||
""",
|
||||
(subject_id, room_id),
|
||||
)
|
||||
_audit_membership(conn, principal.account_id, room_id, subject_id, "membership_removed")
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@@ -1,55 +1,94 @@
|
||||
"""Room share of a case or matter: shared with the room members by role, or private."""
|
||||
"""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
|
||||
from fastapi import APIRouter, Depends, Response
|
||||
from fastapi.responses import JSONResponse
|
||||
from psycopg import Connection
|
||||
|
||||
from app.access.policy import ROOM_RIGHTS
|
||||
from app.access.policy import ROOM_RIGHTS, 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
|
||||
from app.api.schemas import ExclusionIn, RoomShareIn
|
||||
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:
|
||||
resource = _shareable(conn, actor, kind, resource_id)
|
||||
members = conn.execute(
|
||||
_shareable(conn, actor, kind, resource_id)
|
||||
shares = 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
|
||||
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, resource["home_scope_id"], actor),
|
||||
(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),
|
||||
"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
|
||||
"shares": result,
|
||||
"available": [
|
||||
{"id": str(row["id"]), "name": row["name"]} for row in candidates if str(row["id"]) not in shared_ids
|
||||
],
|
||||
}
|
||||
|
||||
@@ -61,14 +100,25 @@ def _register(kind: str, plural: str) -> None:
|
||||
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")
|
||||
@router.put(base + "/rooms/{room_id}", name=f"put_{kind}_room_share")
|
||||
def put_share(
|
||||
resource_id: UUID,
|
||||
body: VisibilityIn,
|
||||
room_id: UUID,
|
||||
body: RoomShareIn,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
return set_visibility(conn, principal.account_id, kind, resource_id, body.visibility)
|
||||
return share_with_room(conn, principal.account_id, kind, resource_id, room_id, body.access)
|
||||
|
||||
@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(
|
||||
@@ -84,5 +134,3 @@ def _register(kind: str, plural: str) -> None:
|
||||
|
||||
_register("case", "cases")
|
||||
_register("matter", "matters")
|
||||
|
||||
|
||||
@@ -72,6 +72,10 @@ class VisibilityIn(APIModel):
|
||||
visibility: Literal["private", "room"]
|
||||
|
||||
|
||||
class RoomShareIn(APIModel):
|
||||
access: Literal["read", "edit"] = "edit"
|
||||
|
||||
|
||||
class ExclusionIn(APIModel):
|
||||
excluded: bool
|
||||
|
||||
|
||||
@@ -54,6 +54,15 @@ def unsupported(detail: str) -> Problem:
|
||||
return Problem(422, "Nicht unterstützt", detail, "unsupported")
|
||||
|
||||
|
||||
def scope_changed() -> Problem:
|
||||
return Problem(
|
||||
403,
|
||||
"Datenraum gewechselt",
|
||||
"Der gewählte Datenraum entspricht nicht mehr der Sitzung. Die Ansicht wird neu geladen.",
|
||||
"scope_changed",
|
||||
)
|
||||
|
||||
|
||||
def password_change_required() -> Problem:
|
||||
return Problem(
|
||||
403,
|
||||
|
||||
+83
-32
@@ -8,7 +8,7 @@ from uuid import UUID, uuid4
|
||||
|
||||
from psycopg.types.json import Json
|
||||
|
||||
from app.access.roles import ensure_can_create
|
||||
from app.access.roles import ensure_can_create, room_role
|
||||
from app.access.policy import (
|
||||
Ctx,
|
||||
Rights,
|
||||
@@ -75,15 +75,27 @@ def _placement(context: Ctx, placement: Ctx | None) -> tuple[Ctx, Ctx]:
|
||||
|
||||
|
||||
def default_visibility(conn, scope_id: UUID) -> str:
|
||||
"""New cases and matters follow the room preset. The personal room is always private."""
|
||||
"""Whether new cases and matters are shared with their room at once. The personal room never is."""
|
||||
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 _auto_share(conn, actor: UUID, kind: str, resource: dict) -> None:
|
||||
if default_visibility(conn, resource["home_scope_id"]) == "room":
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO room_shares (resource_kind, resource_id, room_id, access, created_by)
|
||||
VALUES (%s, %s, %s, 'edit', %s)
|
||||
ON CONFLICT DO NOTHING
|
||||
""",
|
||||
(kind, resource["id"], resource["home_scope_id"], actor),
|
||||
)
|
||||
|
||||
|
||||
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."""
|
||||
"""The own case or matter. Only the owner decides which rooms it is shared with."""
|
||||
if kind == "case":
|
||||
resource = get_case(conn, resource_id)
|
||||
elif kind == "matter":
|
||||
@@ -92,31 +104,71 @@ def _shareable(conn, actor: UUID, kind: str, resource_id: UUID) -> dict:
|
||||
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.")
|
||||
def _target_room(conn, actor: UUID, room_id: UUID) -> dict:
|
||||
room = conn.execute("SELECT id, kind, status FROM data_rooms WHERE id = %s", (room_id,)).fetchone()
|
||||
if room is None or room["kind"] != "organization" or room["status"] != "active":
|
||||
raise not_found()
|
||||
if room_role(conn, actor, room_id) not in {"member", "manager"}:
|
||||
raise forbidden("Du kannst nur mit Datenräumen teilen, in denen du Mitglied oder Verwalter bist.")
|
||||
return room
|
||||
|
||||
|
||||
def share_with_room(conn, actor: UUID, kind: str, resource_id: UUID, room_id: UUID, access: str) -> dict:
|
||||
if access not in {"read", "edit"}:
|
||||
raise unprocessable("Die Freigabeart 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}
|
||||
_target_room(conn, actor, room_id)
|
||||
existing = conn.execute(
|
||||
"SELECT access FROM room_shares WHERE resource_kind = %s AND resource_id = %s AND room_id = %s",
|
||||
(kind, resource_id, room_id),
|
||||
).fetchone()
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO room_shares (resource_kind, resource_id, room_id, access, created_by)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
ON CONFLICT (resource_kind, resource_id, room_id) DO UPDATE SET access = EXCLUDED.access
|
||||
""",
|
||||
(kind, resource_id, room_id, access, actor),
|
||||
)
|
||||
if existing is None or existing["access"] != access:
|
||||
audit(
|
||||
conn,
|
||||
actor,
|
||||
resource["home_scope_id"],
|
||||
"room_shared" if existing is None else "room_share_changed",
|
||||
kind,
|
||||
resource_id,
|
||||
detail={"room_id": str(room_id), "access": access},
|
||||
)
|
||||
return {"room_id": str(room_id), "access": access}
|
||||
|
||||
|
||||
def unshare_from_room(conn, actor: UUID, kind: str, resource_id: UUID, room_id: UUID) -> None:
|
||||
resource = _shareable(conn, actor, kind, resource_id)
|
||||
row = conn.execute(
|
||||
"DELETE FROM room_shares WHERE resource_kind = %s AND resource_id = %s AND room_id = %s RETURNING id",
|
||||
(kind, resource_id, room_id),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise not_found()
|
||||
audit(conn, actor, resource["home_scope_id"], "room_unshared", kind, resource_id, detail={"room_id": str(room_id)})
|
||||
|
||||
|
||||
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."""
|
||||
"""Take one person out of the room shares 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),
|
||||
"""
|
||||
SELECT 1 FROM room_shares s
|
||||
JOIN room_memberships m ON m.room_id = s.room_id AND m.subject_id = %s AND m.status = 'active'
|
||||
WHERE s.resource_kind = %s AND s.resource_id = %s
|
||||
""",
|
||||
(subject_id, kind, resource_id),
|
||||
).fetchone()
|
||||
if member is None:
|
||||
raise not_found()
|
||||
@@ -157,12 +209,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, visibility
|
||||
content_order, content_order_dir
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, 'explicit', 'date', 'asc', %s)
|
||||
VALUES (%s, %s, %s, %s, 'explicit', 'date', 'asc')
|
||||
RETURNING *
|
||||
""",
|
||||
(scope_id, actor, actor, title, default_visibility(conn, scope_id)),
|
||||
(scope_id, actor, actor, title),
|
||||
).fetchone()
|
||||
else:
|
||||
parent, _rights = require_case(conn, actor, parent_case_id, "edit")
|
||||
@@ -174,16 +226,14 @@ 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, visibility
|
||||
content_order, content_order_dir
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, 'date', 'asc', %s)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, 'date', 'asc')
|
||||
RETURNING *
|
||||
""",
|
||||
(
|
||||
parent["home_scope_id"], actor, actor, title, parent["id"], rights_mode,
|
||||
default_visibility(conn, parent["home_scope_id"]),
|
||||
),
|
||||
(parent["home_scope_id"], actor, actor, title, parent["id"], rights_mode),
|
||||
).fetchone()
|
||||
_auto_share(conn, actor, "case", row)
|
||||
audit(conn, actor, row["home_scope_id"], "case_created", "case", row["id"])
|
||||
return row
|
||||
|
||||
@@ -425,11 +475,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, visibility
|
||||
home_scope_id, owner_subject_id, created_by, title, content_order, content_order_dir
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, 'date', 'asc', %s) RETURNING *
|
||||
VALUES (%s, %s, %s, %s, 'date', 'asc') RETURNING *
|
||||
""",
|
||||
(scope_id, actor, actor, title, default_visibility(conn, scope_id)),
|
||||
(scope_id, actor, actor, title),
|
||||
).fetchone()
|
||||
else:
|
||||
parent, _rights = require_matter(conn, actor, parent_matter_id, context, "edit")
|
||||
@@ -439,12 +489,13 @@ 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, visibility
|
||||
content_order, content_order_dir
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, %s, 'date', 'asc', %s) RETURNING *
|
||||
VALUES (%s, %s, %s, %s, %s, 'date', 'asc') RETURNING *
|
||||
""",
|
||||
(parent["home_scope_id"], actor, actor, title, parent["id"], default_visibility(conn, parent["home_scope_id"])),
|
||||
(parent["home_scope_id"], actor, actor, title, parent["id"]),
|
||||
).fetchone()
|
||||
_auto_share(conn, actor, "matter", row)
|
||||
audit(conn, actor, row["home_scope_id"], "matter_created", "matter", row["id"], context)
|
||||
return row
|
||||
|
||||
|
||||
@@ -634,7 +634,6 @@ 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(),
|
||||
}
|
||||
|
||||
@@ -780,7 +779,6 @@ 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",
|
||||
@@ -1055,6 +1053,9 @@ LABELS = {
|
||||
"invitation_revoked": "Einladung widerrufen",
|
||||
"invitation_accepted": "Einladung angenommen",
|
||||
"visibility_changed": "Raumfreigabe geändert",
|
||||
"room_shared": "Mit Datenraum geteilt",
|
||||
"room_share_changed": "Freigabe an Datenraum geändert",
|
||||
"room_unshared": "Freigabe an Datenraum beendet",
|
||||
"person_excluded": "Person von der Raumfreigabe ausgenommen",
|
||||
"person_included": "Person wieder in die Raumfreigabe aufgenommen",
|
||||
"room_owner_changed": "Eigentümer des Datenraums gewechselt",
|
||||
|
||||
Reference in new issue
Block a user