901 lines
37 KiB
Python
901 lines
37 KiB
Python
"""Use-cases for organisation, content and tracking. One transaction is the caller's."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
from pathlib import Path
|
|
from uuid import UUID, uuid4
|
|
|
|
from psycopg.types.json import Json
|
|
|
|
from app.access.policy import (
|
|
Ctx,
|
|
Rights,
|
|
audit,
|
|
block_unsharable_matter_elements,
|
|
bump_version,
|
|
can_change_status,
|
|
case_rights,
|
|
chapter_for,
|
|
deadline_rights,
|
|
display_name,
|
|
element_rights,
|
|
get_case,
|
|
get_deadline,
|
|
get_element,
|
|
get_matter,
|
|
get_task,
|
|
insert_binding,
|
|
matter_rights,
|
|
require_case,
|
|
require_element,
|
|
require_matter,
|
|
task_rights,
|
|
)
|
|
from app.content.documents import clean_document
|
|
from app.core.errors import forbidden, not_found, unprocessable
|
|
from app.core.timeutil import utcnow
|
|
from app.media.storage import asset_path, display_filename, sha256, sniff, write_bytes
|
|
|
|
|
|
def _ctx(value) -> Ctx | None:
|
|
if value is None:
|
|
return None
|
|
if isinstance(value, Ctx):
|
|
return value
|
|
return Ctx(value.kind, value.id)
|
|
|
|
|
|
def require_container_edit(conn, actor: UUID, target: Ctx, rights_context: Ctx | None) -> None:
|
|
if target.kind == "case":
|
|
if rights_context is not None and (rights_context.kind != "case" or rights_context.id != target.id):
|
|
raise unprocessable("Der Handlungskontext passt nicht zur Akte.")
|
|
require_case(conn, actor, target.id, "edit")
|
|
return
|
|
if rights_context is None or (rights_context.kind == "matter" and rights_context.id == target.id):
|
|
require_matter(conn, actor, target.id, None, "edit")
|
|
return
|
|
require_matter(conn, actor, target.id, rights_context, "edit")
|
|
|
|
|
|
def _placement(context: Ctx, placement: Ctx | None) -> tuple[Ctx, Ctx]:
|
|
target = placement or context
|
|
rights_context = None if placement is None and context.kind == "matter" else context
|
|
if placement is None and context.kind == "matter":
|
|
return target, None
|
|
if placement is not None and placement.kind == "matter" and context.kind == "case":
|
|
return placement, context
|
|
if placement is not None and placement == context:
|
|
return target, None if context.kind == "matter" else context
|
|
return target, context if context.kind == "case" or placement is not None else None
|
|
|
|
|
|
def create_case(conn, actor: UUID, scope_id: UUID, title: str, parent_case_id: UUID | None, rights_mode: str) -> dict:
|
|
title = title.strip()
|
|
if parent_case_id is None:
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO cases (
|
|
home_scope_id, owner_subject_id, created_by, title, rights_mode,
|
|
content_order, content_order_dir
|
|
)
|
|
VALUES (%s, %s, %s, %s, 'explicit', 'date', 'asc')
|
|
RETURNING *
|
|
""",
|
|
(scope_id, actor, actor, title),
|
|
).fetchone()
|
|
else:
|
|
parent, _rights = require_case(conn, actor, parent_case_id, "edit")
|
|
if parent["parent_case_id"] is not None:
|
|
raise unprocessable("Im ersten Schnitt hat eine Unterakte keine weitere Unterakte.")
|
|
if rights_mode not in {"inherit_parent", "explicit"}:
|
|
raise unprocessable("Die Rechteart der Unterakte ist ungültig.")
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO cases (
|
|
home_scope_id, owner_subject_id, created_by, title, parent_case_id, rights_mode,
|
|
content_order, content_order_dir
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, %s, 'date', 'asc')
|
|
RETURNING *
|
|
""",
|
|
(parent["home_scope_id"], actor, actor, title, parent["id"], rights_mode),
|
|
).fetchone()
|
|
audit(conn, actor, row["home_scope_id"], "case_created", "case", row["id"])
|
|
return row
|
|
|
|
|
|
def patch_case(conn, actor: UUID, case_id: UUID, expected: int, title: str | None, status: str | None) -> dict:
|
|
case, rights = require_case(conn, actor, case_id, "edit")
|
|
if title is None and status is None:
|
|
raise unprocessable("Es fehlt eine Änderung.")
|
|
if not rights.edit:
|
|
raise forbidden()
|
|
version = bump_version(conn, "cases", case_id, expected)
|
|
if title is not None:
|
|
conn.execute("UPDATE cases SET title = %s WHERE id = %s", (title.strip(), case_id))
|
|
if status is not None:
|
|
conn.execute("UPDATE cases SET record_status = %s WHERE id = %s", (status, case_id))
|
|
updated = get_case(conn, case_id)
|
|
updated["version"] = version
|
|
return updated
|
|
|
|
|
|
def create_chapter(conn, actor: UUID, kind: str, container_id: UUID, title: str, position: int | None, context: Ctx | None) -> dict:
|
|
if kind == "case":
|
|
container, _rights = require_case(conn, actor, container_id, "edit")
|
|
else:
|
|
container, _rights = require_matter(conn, actor, container_id, context, "edit")
|
|
if position is None:
|
|
column = "case_id" if kind == "case" else "matter_id"
|
|
current = conn.execute(
|
|
f"SELECT COALESCE(MAX(position), -1) + 1 AS pos FROM chapters WHERE {column} = %s",
|
|
(container_id,),
|
|
).fetchone()
|
|
position = int(current["pos"])
|
|
if kind == "case":
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO chapters (home_scope_id, case_id, title, position)
|
|
VALUES (%s, %s, %s, %s) RETURNING *
|
|
""",
|
|
(container["home_scope_id"], container_id, title.strip(), position),
|
|
).fetchone()
|
|
else:
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO chapters (home_scope_id, matter_id, title, position)
|
|
VALUES (%s, %s, %s, %s) RETURNING *
|
|
""",
|
|
(container["home_scope_id"], container_id, title.strip(), position),
|
|
).fetchone()
|
|
audit(conn, actor, container["home_scope_id"], "chapter_created", kind, container_id, context)
|
|
return row
|
|
|
|
|
|
def create_matter(conn, actor: UUID, scope_id: UUID, title: str, parent_matter_id: UUID | None, context: Ctx | None) -> dict:
|
|
title = title.strip()
|
|
if parent_matter_id is None:
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO matters (
|
|
home_scope_id, owner_subject_id, created_by, title, content_order, content_order_dir
|
|
)
|
|
VALUES (%s, %s, %s, %s, 'date', 'asc') RETURNING *
|
|
""",
|
|
(scope_id, actor, actor, title),
|
|
).fetchone()
|
|
else:
|
|
parent, _rights = require_matter(conn, actor, parent_matter_id, context, "edit")
|
|
if parent["parent_matter_id"] is not None:
|
|
raise unprocessable("Im ersten Schnitt hat ein Teilvorgang keinen weiteren Teilvorgang.")
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO matters (
|
|
home_scope_id, owner_subject_id, created_by, title, parent_matter_id,
|
|
content_order, content_order_dir
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, 'date', 'asc') RETURNING *
|
|
""",
|
|
(parent["home_scope_id"], actor, actor, title, parent["id"]),
|
|
).fetchone()
|
|
audit(conn, actor, row["home_scope_id"], "matter_created", "matter", row["id"], context)
|
|
return row
|
|
|
|
|
|
def patch_matter(conn, actor: UUID, matter_id: UUID, expected: int, title: str | None, status: str | None, context: Ctx | None) -> dict:
|
|
if title is None and status is None:
|
|
raise unprocessable("Es fehlt eine Änderung.")
|
|
_matter, _rights = require_matter(conn, actor, matter_id, context, "edit")
|
|
bump_version(conn, "matters", matter_id, expected)
|
|
if title is not None:
|
|
conn.execute("UPDATE matters SET title = %s WHERE id = %s", (title.strip(), matter_id))
|
|
if status is not None:
|
|
conn.execute("UPDATE matters SET record_status = %s WHERE id = %s", (status, matter_id))
|
|
return get_matter(conn, matter_id)
|
|
|
|
|
|
def link_matter_to_case(conn, actor: UUID, matter_id: UUID, case_id: UUID, matter_context: Ctx | None) -> dict:
|
|
matter, matter_right = require_matter(conn, actor, matter_id, matter_context, "share")
|
|
case, _case_right = require_case(conn, actor, case_id, "edit")
|
|
if not matter_right.share:
|
|
raise forbidden()
|
|
try:
|
|
link = conn.execute(
|
|
"""
|
|
INSERT INTO matter_case_links (matter_id, case_id, created_by)
|
|
VALUES (%s, %s, %s) RETURNING *
|
|
""",
|
|
(matter["id"], case["id"], actor),
|
|
).fetchone()
|
|
except Exception as exc:
|
|
if exc.__class__.__name__ == "UniqueViolation":
|
|
raise unprocessable("Der Vorgang ist dieser Akte bereits zugeordnet.") from exc
|
|
raise
|
|
block_unsharable_matter_elements(conn, actor, link["id"], matter)
|
|
audit(conn, actor, case["home_scope_id"], "matter_linked", "matter", matter["id"], Ctx("case", case["id"]))
|
|
return link
|
|
|
|
|
|
def unlink_matter_case(conn, actor: UUID, link_id: UUID) -> None:
|
|
link = conn.execute("SELECT * FROM matter_case_links WHERE id = %s", (link_id,)).fetchone()
|
|
if link is None:
|
|
raise not_found()
|
|
matter_direct = matter_rights(conn, actor, get_matter(conn, link["matter_id"]), None)
|
|
case_right = case_rights(conn, actor, get_case(conn, link["case_id"]))
|
|
if not case_right.read and not matter_direct.read:
|
|
raise not_found()
|
|
if not case_right.edit and not matter_direct.share:
|
|
raise forbidden()
|
|
conn.execute("DELETE FROM matter_case_links WHERE id = %s", (link_id,))
|
|
audit(conn, actor, None, "matter_unlinked", "matter", link["matter_id"], Ctx("case", link["case_id"]))
|
|
|
|
|
|
def create_cross_link(conn, actor: UUID, source_id: UUID, target_id: UUID, source_context: Ctx | None, target_context: Ctx) -> dict:
|
|
if source_id == target_id:
|
|
raise unprocessable("Ein Vorgang kann nicht auf sich selbst verweisen.")
|
|
source, _rights = require_matter(conn, actor, source_id, source_context, "edit")
|
|
target, target_rights = require_matter(conn, actor, target_id, target_context, "read")
|
|
if not target_rights.read:
|
|
raise not_found()
|
|
try:
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO matter_cross_links (from_matter_id, to_matter_id, created_by)
|
|
VALUES (%s, %s, %s) RETURNING *
|
|
""",
|
|
(source["id"], target["id"], actor),
|
|
).fetchone()
|
|
except Exception as exc:
|
|
if exc.__class__.__name__ == "UniqueViolation":
|
|
raise unprocessable("Dieser Querverweis besteht bereits.") from exc
|
|
raise
|
|
audit(conn, actor, source["home_scope_id"], "cross_linked", "matter", source["id"], source_context)
|
|
return row
|
|
|
|
|
|
def delete_cross_link(conn, actor: UUID, link_id: UUID, source_context: Ctx | None) -> None:
|
|
link = conn.execute("SELECT * FROM matter_cross_links WHERE id = %s", (link_id,)).fetchone()
|
|
if link is None:
|
|
raise not_found()
|
|
require_matter(conn, actor, link["from_matter_id"], source_context, "edit")
|
|
conn.execute("DELETE FROM matter_cross_links WHERE id = %s", (link_id,))
|
|
|
|
|
|
def _actor_rights_for(conn, actor: UUID, kind: str, resource_id: UUID, context: Ctx | None) -> Rights:
|
|
if kind == "case":
|
|
_row, rights = require_case(conn, actor, resource_id, "read")
|
|
return rights
|
|
if kind == "matter":
|
|
_row, rights = require_matter(conn, actor, resource_id, context, "read")
|
|
return rights
|
|
if kind == "element":
|
|
_row, rights = require_element(conn, actor, resource_id, context, "read")
|
|
return rights
|
|
if kind == "task":
|
|
task = get_task(conn, resource_id)
|
|
if task is None:
|
|
raise not_found()
|
|
rights = task_rights(conn, actor, task, context)
|
|
if not rights.read:
|
|
raise not_found()
|
|
return rights
|
|
deadline = get_deadline(conn, resource_id)
|
|
if deadline is None:
|
|
raise not_found()
|
|
rights = deadline_rights(conn, actor, deadline, context)
|
|
if not rights.read:
|
|
raise not_found()
|
|
return rights
|
|
|
|
|
|
def grant_access(conn, actor: UUID, kind: str, resource_id: UUID, grantee_id: UUID, can_read: bool, can_edit: bool, can_share: bool, context: Ctx | None) -> dict:
|
|
if grantee_id == actor:
|
|
raise unprocessable("Die eigene Berechtigung wird nicht als Freigabe gespeichert.")
|
|
if not can_read:
|
|
raise unprocessable("Eine Freigabe braucht mindestens Lesen.")
|
|
if kind == "case":
|
|
resource = get_case(conn, resource_id)
|
|
rights = case_rights(conn, actor, resource) if resource else Rights()
|
|
elif kind == "matter":
|
|
resource = get_matter(conn, resource_id)
|
|
rights = matter_rights(conn, actor, resource, context) if resource else Rights()
|
|
else:
|
|
raise unprocessable("Freigaben werden an der Akte oder am Vorgang erteilt.")
|
|
if resource is None or not rights.read:
|
|
raise not_found()
|
|
owner = resource["owner_subject_id"] == actor
|
|
if not owner and not rights.share:
|
|
raise forbidden()
|
|
if can_edit and not (owner or rights.edit):
|
|
raise forbidden()
|
|
if can_share and not (owner or rights.share):
|
|
raise forbidden()
|
|
account = conn.execute(
|
|
"SELECT id FROM auth_accounts WHERE id = %s AND status = 'active'",
|
|
(grantee_id,),
|
|
).fetchone()
|
|
if account is None:
|
|
raise not_found()
|
|
existing = conn.execute(
|
|
"""
|
|
SELECT id FROM access_grants
|
|
WHERE NOT is_exception AND grantee_subject_id = %s
|
|
AND resource_kind = %s AND resource_id = %s AND context_kind IS NULL
|
|
""",
|
|
(grantee_id, kind, resource_id),
|
|
).fetchone()
|
|
if existing is None:
|
|
row = 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, %s, %s, %s, false, false, %s)
|
|
RETURNING *
|
|
""",
|
|
(grantee_id, kind, resource_id, can_read, can_edit, can_share, actor),
|
|
).fetchone()
|
|
else:
|
|
row = conn.execute(
|
|
"""
|
|
UPDATE access_grants
|
|
SET can_read = %s, can_edit = %s, can_share = %s
|
|
WHERE id = %s
|
|
RETURNING *
|
|
""",
|
|
(can_read, can_edit, can_share, existing["id"]),
|
|
).fetchone()
|
|
audit(conn, actor, resource["home_scope_id"], "grant_created", kind, resource_id, context)
|
|
return row
|
|
|
|
|
|
def revoke_grant(conn, actor: UUID, grant_id: UUID) -> None:
|
|
grant = conn.execute("SELECT * FROM access_grants WHERE id = %s AND NOT is_exception", (grant_id,)).fetchone()
|
|
if grant is None:
|
|
raise not_found()
|
|
kind = grant["resource_kind"]
|
|
if kind == "case":
|
|
resource = get_case(conn, grant["resource_id"])
|
|
rights = case_rights(conn, actor, resource) if resource else Rights()
|
|
elif kind == "matter":
|
|
resource = get_matter(conn, grant["resource_id"])
|
|
rights = matter_rights(conn, actor, resource, None) if resource else Rights()
|
|
elif kind == "task":
|
|
resource = get_task(conn, grant["resource_id"])
|
|
rights = task_rights(conn, actor, resource, None) if resource else Rights()
|
|
elif kind == "deadline":
|
|
resource = get_deadline(conn, grant["resource_id"])
|
|
rights = deadline_rights(conn, actor, resource, None) if resource else Rights()
|
|
else:
|
|
resource = get_element(conn, grant["resource_id"])
|
|
rights = element_rights(conn, actor, resource, None) if resource else Rights()
|
|
if resource is None or not rights.read:
|
|
raise not_found()
|
|
if resource["owner_subject_id"] != actor and not rights.share:
|
|
raise forbidden()
|
|
conn.execute("DELETE FROM access_grants WHERE id = %s", (grant_id,))
|
|
audit(conn, actor, resource["home_scope_id"], "grant_revoked", kind, grant["resource_id"])
|
|
|
|
|
|
def set_exception(conn, actor: UUID, kind: str, resource_id: UUID, context: Ctx, remove_read: bool, remove_edit: bool, remove_share: bool) -> None:
|
|
if kind == "element":
|
|
resource = get_element(conn, resource_id)
|
|
elif kind == "matter":
|
|
resource = get_matter(conn, resource_id)
|
|
elif kind == "task":
|
|
resource = get_task(conn, resource_id)
|
|
else:
|
|
resource = get_deadline(conn, resource_id)
|
|
if resource is None:
|
|
raise not_found()
|
|
if resource["owner_subject_id"] != actor:
|
|
raise forbidden()
|
|
conn.execute(
|
|
"""
|
|
DELETE FROM access_grants
|
|
WHERE is_exception AND grantee_subject_id IS NULL
|
|
AND resource_kind = %s AND resource_id = %s
|
|
AND context_kind = %s AND context_id = %s
|
|
""",
|
|
(kind, resource_id, context.kind, context.id),
|
|
)
|
|
if remove_read or remove_edit or remove_share:
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO access_grants (
|
|
grantee_subject_id, resource_kind, resource_id, context_kind, context_id,
|
|
can_read, can_edit, can_share, can_delete, is_exception, created_by
|
|
)
|
|
VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, false, true, %s)
|
|
""",
|
|
(kind, resource_id, context.kind, context.id, remove_read, remove_edit, remove_share, actor),
|
|
)
|
|
|
|
|
|
def _new_element(conn, actor: UUID, scope_id: UUID, title: str, kind: str) -> dict:
|
|
return conn.execute(
|
|
"""
|
|
INSERT INTO elements (home_scope_id, owner_subject_id, created_by, title, kind)
|
|
VALUES (%s, %s, %s, %s, %s) RETURNING *
|
|
""",
|
|
(scope_id, actor, actor, title.strip(), kind),
|
|
).fetchone()
|
|
|
|
|
|
def record_dates(conn, actor: UUID, scope_id: UUID, element_id: UUID, context: Ctx, document_on: date | None, sent_on: date | None, received_on: date | None) -> None:
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO correspondence_dates (element_id, document_on, sent_on, received_on)
|
|
VALUES (%s, %s, %s, %s)
|
|
ON CONFLICT (element_id) DO UPDATE
|
|
SET document_on = EXCLUDED.document_on,
|
|
sent_on = EXCLUDED.sent_on,
|
|
received_on = EXCLUDED.received_on
|
|
""",
|
|
(element_id, document_on, sent_on, received_on),
|
|
)
|
|
if document_on is not None:
|
|
audit(conn, actor, scope_id, "correspondence_document", "element", element_id, context, document_on)
|
|
if sent_on is not None:
|
|
audit(conn, actor, scope_id, "correspondence_sent", "element", element_id, context, sent_on)
|
|
if received_on is not None:
|
|
audit(conn, actor, scope_id, "correspondence_received", "element", element_id, context, received_on)
|
|
audit(conn, actor, scope_id, "correspondence_recorded", "element", element_id, context)
|
|
|
|
|
|
def create_note(
|
|
conn,
|
|
actor: UUID,
|
|
scope_id: UUID,
|
|
title: str,
|
|
document: dict,
|
|
context: Ctx,
|
|
placement: Ctx | None,
|
|
chapter_id: UUID | None,
|
|
document_on: date | None = None,
|
|
) -> dict:
|
|
document = clean_document(document)
|
|
target, rights_context = _placement(context, placement)
|
|
require_container_edit(conn, actor, target, rights_context)
|
|
element = _new_element(conn, actor, scope_id, title, "note")
|
|
conn.execute(
|
|
"INSERT INTO note_bodies (element_id, document_json, version) VALUES (%s, %s, 1)",
|
|
(element["id"], Json(document)),
|
|
)
|
|
binding = insert_binding(conn, element["id"], target.kind, target.id, chapter_id, actor)
|
|
audit(conn, actor, scope_id, "note_created", "element", element["id"], rights_context or target)
|
|
if document_on is not None:
|
|
record_dates(conn, actor, scope_id, element["id"], rights_context or target, document_on, None, None)
|
|
return {"element": element, "binding": binding, "document": document}
|
|
|
|
|
|
def update_note(conn, actor: UUID, element_id: UUID, expected: int, document: dict, context: Ctx | None) -> dict:
|
|
document = clean_document(document)
|
|
element, rights = require_element(conn, actor, element_id, context, "edit")
|
|
if element["kind"] != "note":
|
|
raise not_found()
|
|
if not rights.edit:
|
|
raise forbidden()
|
|
version = bump_version(conn, "elements", element_id, expected)
|
|
conn.execute(
|
|
"UPDATE note_bodies SET document_json = %s, version = %s, updated_at = now() WHERE element_id = %s",
|
|
(Json(document), version, element_id),
|
|
)
|
|
audit(conn, actor, element["home_scope_id"], "note_updated", "element", element_id, context)
|
|
element = get_element(conn, element_id)
|
|
return {"element": element, "document": document}
|
|
|
|
|
|
def bind_element(conn, actor: UUID, element_id: UUID, source: Ctx, target: Ctx, target_context: Ctx | None, chapter_id: UUID | None) -> dict:
|
|
_element, source_rights = require_element(conn, actor, element_id, source, "share")
|
|
if not source_rights.share:
|
|
raise forbidden()
|
|
require_container_edit(conn, actor, target, target_context)
|
|
binding = insert_binding(conn, element_id, target.kind, target.id, chapter_id, actor)
|
|
audit(conn, actor, None, "binding_created", "element", element_id, target_context or target)
|
|
return binding
|
|
|
|
|
|
def detach_binding(conn, actor: UUID, binding_id: UUID, context: Ctx | None) -> None:
|
|
binding = conn.execute("SELECT * FROM bindings WHERE id = %s", (binding_id,)).fetchone()
|
|
if binding is None:
|
|
raise not_found()
|
|
element = get_element(conn, binding["element_id"])
|
|
if element is None:
|
|
raise not_found()
|
|
action_context = context or Ctx(binding["context_kind"], binding["context_id"])
|
|
_element, rights = require_element(conn, actor, element["id"], action_context, "edit")
|
|
if binding["context_kind"] != action_context.kind and not (
|
|
action_context.kind == "case" and binding["context_kind"] == "matter"
|
|
):
|
|
raise not_found()
|
|
if not rights.edit:
|
|
raise forbidden()
|
|
conn.execute("DELETE FROM bindings WHERE id = %s", (binding_id,))
|
|
audit(conn, actor, element["home_scope_id"], "binding_removed", "element", element["id"], action_context)
|
|
|
|
|
|
def add_annotation(conn, actor: UUID, binding_id: UUID, body: str, context: Ctx | None) -> dict:
|
|
binding = conn.execute("SELECT * FROM bindings WHERE id = %s", (binding_id,)).fetchone()
|
|
if binding is None:
|
|
raise not_found()
|
|
element = get_element(conn, binding["element_id"])
|
|
if element is None:
|
|
raise not_found()
|
|
action_context = context or Ctx(binding["context_kind"], binding["context_id"])
|
|
_element, rights = require_element(conn, actor, element["id"], action_context, "edit")
|
|
if not rights.edit:
|
|
raise forbidden()
|
|
if binding["context_id"] != action_context.id and not (
|
|
action_context.kind == "case" and binding["context_kind"] == "matter"
|
|
):
|
|
raise not_found()
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO context_annotations (binding_id, body, author_subject_id)
|
|
VALUES (%s, %s, %s) RETURNING *
|
|
""",
|
|
(binding_id, body.strip(), actor),
|
|
).fetchone()
|
|
audit(conn, actor, element["home_scope_id"], "annotation_created", "element", element["id"], action_context)
|
|
return row
|
|
|
|
|
|
def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str, external_id: str, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, detail: dict | None = None) -> dict:
|
|
target, rights_context = _placement(context, placement)
|
|
require_container_edit(conn, actor, target, rights_context)
|
|
element = _new_element(conn, actor, scope_id, title, "external_ref")
|
|
conn.execute(
|
|
"INSERT INTO external_references (element_id, source, external_id, detail) VALUES (%s, %s, %s, %s)",
|
|
(element["id"], source, external_id.strip()[:200], Json(detail or {})),
|
|
)
|
|
binding = insert_binding(conn, element["id"], target.kind, target.id, chapter_id, actor)
|
|
record_dates(conn, actor, scope_id, element["id"], rights_context or target, document_on, sent_on, received_on)
|
|
audit(conn, actor, scope_id, "external_ref_added", "element", element["id"], rights_context or target)
|
|
return element | {"binding_id": binding["id"]}
|
|
|
|
|
|
def create_file(conn, actor: UUID, scope_id: UUID, title: str, filename: str, data: bytes, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, media_root: str, max_bytes: int, rollback_files: list[Path]) -> dict:
|
|
if not data or len(data) > max_bytes:
|
|
raise unprocessable("Die Datei ist leer oder zu groß.")
|
|
content_type = sniff(data)
|
|
target, rights_context = _placement(context, placement)
|
|
require_container_edit(conn, actor, target, rights_context)
|
|
element = _new_element(conn, actor, scope_id, title, "file")
|
|
asset_id = uuid4()
|
|
path = asset_path(media_root, asset_id)
|
|
write_bytes(path, data)
|
|
rollback_files.append(path)
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO media_assets (
|
|
id, element_id, owner_subject_id, home_scope_id, original_filename,
|
|
content_type, byte_size, sha256, status
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, 'active')
|
|
""",
|
|
(
|
|
asset_id,
|
|
element["id"],
|
|
actor,
|
|
scope_id,
|
|
display_filename(filename),
|
|
content_type,
|
|
len(data),
|
|
sha256(data),
|
|
),
|
|
)
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO media_operations (element_id, asset_id, phase, status)
|
|
VALUES (%s, %s, 'active', 'committed')
|
|
""",
|
|
(element["id"], asset_id),
|
|
)
|
|
binding = insert_binding(conn, element["id"], target.kind, target.id, chapter_id, actor)
|
|
record_dates(conn, actor, scope_id, element["id"], rights_context or target, document_on, sent_on, received_on)
|
|
audit(conn, actor, scope_id, "file_added", "element", element["id"], rights_context or target)
|
|
return element | {"binding_id": binding["id"], "asset_id": asset_id}
|
|
|
|
|
|
def delete_element(conn, actor: UUID, element_id: UUID, expected: int, media_root: str, delete_after: list[Path]) -> None:
|
|
element = get_element(conn, element_id)
|
|
if element is None or element["owner_subject_id"] != actor:
|
|
raise not_found()
|
|
bump_version(conn, "elements", element_id, expected)
|
|
asset = conn.execute("SELECT * FROM media_assets WHERE element_id = %s", (element_id,)).fetchone()
|
|
conn.execute("DELETE FROM bindings WHERE element_id = %s", (element_id,))
|
|
conn.execute(
|
|
"UPDATE elements SET deleted_at = now(), updated_at = now() WHERE id = %s",
|
|
(element_id,),
|
|
)
|
|
if asset is not None and asset["status"] == "active":
|
|
conn.execute("UPDATE media_assets SET status = 'purging' WHERE id = %s", (asset["id"],))
|
|
delete_after.append(asset_path(media_root, asset["id"]))
|
|
conn.execute("UPDATE media_assets SET status = 'purged' WHERE id = %s", (asset["id"],))
|
|
audit(conn, actor, element["home_scope_id"], "element_deleted", "element", element_id)
|
|
|
|
|
|
def create_task(conn, actor: UUID, scope_id: UUID, title: str, due_on: date | None, matter_id: UUID | None, context: Ctx | None) -> dict:
|
|
if matter_id is not None:
|
|
require_matter(conn, actor, matter_id, context, "edit")
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO tasks (
|
|
home_scope_id, owner_subject_id, created_by, title, matter_id, due_on
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, %s) RETURNING *
|
|
""",
|
|
(scope_id, actor, actor, title.strip(), matter_id, due_on),
|
|
).fetchone()
|
|
audit(conn, actor, scope_id, "task_created", "task", row["id"], context)
|
|
return row
|
|
|
|
|
|
def patch_task(conn, actor: UUID, task_id: UUID, expected: int, title: str | None, due_on: date | None, clear_due: bool, status: str | None, context: Ctx | None) -> dict:
|
|
task = get_task(conn, task_id)
|
|
if task is None:
|
|
raise not_found()
|
|
rights = task_rights(conn, actor, task, context)
|
|
if not rights.read:
|
|
raise not_found()
|
|
structural = title is not None or due_on is not None or clear_due
|
|
if structural and not rights.edit:
|
|
raise forbidden()
|
|
if status is not None and not can_change_status(actor, task, rights):
|
|
raise forbidden()
|
|
if title is None and due_on is None and not clear_due and status is None:
|
|
raise unprocessable("Es fehlt eine Änderung.")
|
|
bump_version(conn, "tasks", task_id, expected)
|
|
if title is not None:
|
|
conn.execute("UPDATE tasks SET title = %s WHERE id = %s", (title.strip(), task_id))
|
|
if clear_due:
|
|
conn.execute("UPDATE tasks SET due_on = NULL WHERE id = %s", (task_id,))
|
|
elif due_on is not None:
|
|
conn.execute("UPDATE tasks SET due_on = %s WHERE id = %s", (due_on, task_id))
|
|
if status is not None:
|
|
conn.execute("UPDATE tasks SET task_status = %s WHERE id = %s", (status, task_id))
|
|
audit(
|
|
conn,
|
|
actor,
|
|
task["home_scope_id"],
|
|
"task_done" if status == "done" else "task_reopened",
|
|
"task",
|
|
task_id,
|
|
context,
|
|
)
|
|
return get_task(conn, task_id)
|
|
|
|
|
|
def assign_task(conn, actor: UUID, task_id: UUID, assignee_id: UUID | None, context: Ctx | None) -> dict:
|
|
task = get_task(conn, task_id)
|
|
if task is None:
|
|
raise not_found()
|
|
rights = task_rights(conn, actor, task, context)
|
|
if not rights.read:
|
|
raise not_found()
|
|
if task["owner_subject_id"] != actor and not rights.share:
|
|
raise forbidden()
|
|
if assignee_id is not None:
|
|
account = conn.execute(
|
|
"SELECT id FROM auth_accounts WHERE id = %s AND status = 'active'",
|
|
(assignee_id,),
|
|
).fetchone()
|
|
if account is None:
|
|
raise not_found()
|
|
existing = conn.execute(
|
|
"""
|
|
SELECT * FROM access_grants
|
|
WHERE NOT is_exception AND grantee_subject_id = %s
|
|
AND resource_kind = 'task' AND resource_id = %s AND context_kind IS NULL
|
|
""",
|
|
(assignee_id, task_id),
|
|
).fetchone()
|
|
if existing is None:
|
|
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, 'task', %s, true, false, false, false, false, %s)
|
|
""",
|
|
(assignee_id, task_id, actor),
|
|
)
|
|
elif not existing["can_read"]:
|
|
conn.execute("UPDATE access_grants SET can_read = true WHERE id = %s", (existing["id"],))
|
|
elif task["assignee_subject_id"] is not None:
|
|
conn.execute(
|
|
"""
|
|
DELETE FROM access_grants
|
|
WHERE NOT is_exception AND grantee_subject_id = %s
|
|
AND resource_kind = 'task' AND resource_id = %s
|
|
AND context_kind IS NULL AND can_read AND NOT can_edit AND NOT can_share AND NOT can_delete
|
|
""",
|
|
(task["assignee_subject_id"], task_id),
|
|
)
|
|
conn.execute(
|
|
"UPDATE tasks SET assignee_subject_id = %s, updated_at = now() WHERE id = %s",
|
|
(assignee_id, task_id),
|
|
)
|
|
return get_task(conn, task_id)
|
|
|
|
|
|
def create_deadline(conn, actor: UUID, scope_id: UUID, title: str, due_on: date, matter_id: UUID | None, context: Ctx | None) -> dict:
|
|
if matter_id is not None:
|
|
require_matter(conn, actor, matter_id, context, "edit")
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO deadlines (
|
|
home_scope_id, owner_subject_id, created_by, title, matter_id, due_on
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, %s) RETURNING *
|
|
""",
|
|
(scope_id, actor, actor, title.strip(), matter_id, due_on),
|
|
).fetchone()
|
|
audit(conn, actor, scope_id, "deadline_created", "deadline", row["id"], context)
|
|
return row
|
|
|
|
|
|
def patch_deadline(conn, actor: UUID, deadline_id: UUID, expected: int, title: str | None, due_on: date | None, context: Ctx | None) -> dict:
|
|
deadline = get_deadline(conn, deadline_id)
|
|
if deadline is None:
|
|
raise not_found()
|
|
rights = deadline_rights(conn, actor, deadline, context)
|
|
if not rights.read:
|
|
raise not_found()
|
|
if not rights.edit:
|
|
raise forbidden()
|
|
if title is None and due_on is None:
|
|
raise unprocessable("Es fehlt eine Änderung.")
|
|
bump_version(conn, "deadlines", deadline_id, expected)
|
|
if title is not None:
|
|
conn.execute("UPDATE deadlines SET title = %s WHERE id = %s", (title.strip(), deadline_id))
|
|
if due_on is not None:
|
|
conn.execute("UPDATE deadlines SET due_on = %s WHERE id = %s", (due_on, deadline_id))
|
|
return get_deadline(conn, deadline_id)
|
|
|
|
|
|
def fulfill_deadline(conn, actor: UUID, deadline_id: UUID, expected: int, context: Ctx | None) -> dict:
|
|
deadline = get_deadline(conn, deadline_id)
|
|
if deadline is None:
|
|
raise not_found()
|
|
rights = deadline_rights(conn, actor, deadline, context)
|
|
if not rights.read:
|
|
raise not_found()
|
|
if not can_change_status(actor, deadline, rights):
|
|
raise forbidden()
|
|
bump_version(conn, "deadlines", deadline_id, expected)
|
|
conn.execute(
|
|
"""
|
|
UPDATE deadlines
|
|
SET deadline_status = 'fulfilled', fulfilled_at = %s, updated_at = now()
|
|
WHERE id = %s
|
|
""",
|
|
(utcnow(), deadline_id),
|
|
)
|
|
audit(conn, actor, deadline["home_scope_id"], "deadline_fulfilled", "deadline", deadline_id, context)
|
|
return get_deadline(conn, deadline_id)
|
|
|
|
|
|
def link_task_deadline(conn, actor: UUID, task_id: UUID, deadline_id: UUID, context: Ctx | None) -> None:
|
|
task = get_task(conn, task_id)
|
|
deadline = get_deadline(conn, deadline_id)
|
|
if task is None or deadline is None:
|
|
raise not_found()
|
|
task_right = task_rights(conn, actor, task, context)
|
|
deadline_right = deadline_rights(conn, actor, deadline, context)
|
|
if not task_right.read or not deadline_right.read:
|
|
raise not_found()
|
|
if not task_right.edit:
|
|
raise forbidden()
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO task_deadline_links (task_id, deadline_id, created_by)
|
|
VALUES (%s, %s, %s)
|
|
ON CONFLICT DO NOTHING
|
|
""",
|
|
(task_id, deadline_id, actor),
|
|
)
|
|
|
|
|
|
def set_content_order(
|
|
conn,
|
|
actor: UUID,
|
|
kind: str,
|
|
container_id: UUID,
|
|
expected: int,
|
|
mode: str,
|
|
direction: str,
|
|
context: Ctx | None,
|
|
) -> int:
|
|
if kind == "case":
|
|
container, rights = require_case(conn, actor, container_id, "edit")
|
|
table = "cases"
|
|
else:
|
|
container, rights = require_matter(conn, actor, container_id, context, "edit")
|
|
table = "matters"
|
|
if not rights.edit:
|
|
raise forbidden()
|
|
version = bump_version(conn, table, container_id, expected)
|
|
conn.execute(
|
|
f"UPDATE {table} SET content_order = %s, content_order_dir = %s WHERE id = %s",
|
|
(mode, direction, container_id),
|
|
)
|
|
audit(conn, actor, container["home_scope_id"], "content_order", kind, container_id, context)
|
|
return version
|
|
|
|
|
|
def reorder_bindings(
|
|
conn,
|
|
actor: UUID,
|
|
kind: str,
|
|
container_id: UUID,
|
|
expected: int,
|
|
binding_ids: list[UUID],
|
|
context: Ctx | None,
|
|
) -> int:
|
|
if kind == "case":
|
|
container, rights = require_case(conn, actor, container_id, "edit")
|
|
table = "cases"
|
|
page = Ctx("case", container_id)
|
|
else:
|
|
container, rights = require_matter(conn, actor, container_id, context, "edit")
|
|
table = "matters"
|
|
page = context or Ctx("matter", container_id)
|
|
if not rights.edit:
|
|
raise forbidden()
|
|
version = bump_version(conn, table, container_id, expected)
|
|
rows = conn.execute(
|
|
"""
|
|
SELECT * FROM bindings
|
|
WHERE context_kind = %s AND context_id = %s
|
|
ORDER BY position, id
|
|
""",
|
|
(kind, container_id),
|
|
).fetchall()
|
|
visible_ids = []
|
|
for row in rows:
|
|
element = get_element(conn, row["element_id"])
|
|
if element is not None and element_rights(conn, actor, element, page).read:
|
|
visible_ids.append(row["id"])
|
|
submitted = list(binding_ids)
|
|
if len(submitted) != len(set(submitted)):
|
|
raise unprocessable("Dieselbe Einbindung steht mehrfach in der Ordnung.")
|
|
if any(item not in set(visible_ids) for item in submitted):
|
|
raise not_found()
|
|
if set(submitted) != set(visible_ids):
|
|
raise unprocessable("Die Ordnung ist unvollständig. Bitte neu laden.")
|
|
visible_set = set(visible_ids)
|
|
coming = iter(submitted)
|
|
sequence = []
|
|
for row in rows:
|
|
if row["id"] in visible_set:
|
|
sequence.append(next(coming))
|
|
else:
|
|
sequence.append(row["id"])
|
|
for index, binding_id in enumerate(sequence):
|
|
conn.execute("UPDATE bindings SET position = %s WHERE id = %s", (index, binding_id))
|
|
audit(conn, actor, container["home_scope_id"], "content_reordered", kind, container_id, context)
|
|
return version
|
|
|
|
|
|
def update_dates(
|
|
conn,
|
|
actor: UUID,
|
|
element_id: UUID,
|
|
expected: int,
|
|
document_on: date | None,
|
|
sent_on: date | None,
|
|
received_on: date | None,
|
|
context: Ctx | None,
|
|
) -> dict:
|
|
element, rights = require_element(conn, actor, element_id, context, "edit")
|
|
if not rights.edit:
|
|
raise forbidden()
|
|
bump_version(conn, "elements", element_id, expected)
|
|
record_dates(conn, actor, element["home_scope_id"], element_id, context or Ctx("matter", element_id), document_on, sent_on, received_on)
|
|
return get_element(conn, element_id)
|