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
+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")
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user