fix(supabase): V1-2.1 audit M1 M2 M3 and LOW fixes in M5
Approval needs an active sheet (lock sheet, version, round); a base only from a still-published round version; rights.mfa before rights.capability; withdrawal of a published round refused; approver separation at publish; deprecate and working copy serialised; closed codes; append-only acks; upload policy bound to the registering editor. pgTAP 017-019 added.
This commit is contained in:
1 parent
3914eaa866
commit
d05f75da84
10 files changed
+1477
-127
No files matched your search
@@ -307,9 +307,11 @@ $$;
|
||||
-- ===========================================================================
|
||||
-- 4. Authoring helpers and RPCs
|
||||
-- ===========================================================================
|
||||
-- Require the sheet_editor capability at aal2 on an official, non-deleted
|
||||
-- sheet other than the historical one (writes), returning the actor.
|
||||
create or replace function private.require_sheet_author(p_sheet_id uuid)
|
||||
-- The calling session: an active account with a confirmed email, at aal2
|
||||
-- (contract §5.6, §6.7). Every catalogue RPC except the readers calls it
|
||||
-- before any lookup or capability check, so an aal1 session gets one answer
|
||||
-- (`rights.mfa`) whatever it asks for and whatever it holds.
|
||||
create or replace function private.require_catalog_actor()
|
||||
returns uuid
|
||||
language plpgsql
|
||||
stable
|
||||
@@ -319,12 +321,50 @@ as $$
|
||||
declare
|
||||
v_actor uuid := private.require_actor();
|
||||
begin
|
||||
if p_sheet_id = private.historical_sheet_id() then
|
||||
raise exception 'sheet.historical_read_only' using errcode = '42501';
|
||||
if not private.has_mfa() then
|
||||
raise exception 'rights.mfa' using errcode = '42501';
|
||||
end if;
|
||||
return v_actor;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- The calling session holds one capability at aal2, returning the actor.
|
||||
create or replace function private.require_capability(p_kind public.catalog_grant_kind)
|
||||
returns uuid
|
||||
language plpgsql
|
||||
stable
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
declare
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
begin
|
||||
if not private.grant_active(v_actor, p_kind) then
|
||||
raise exception 'rights.capability' using errcode = '42501';
|
||||
end if;
|
||||
return v_actor;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Require the sheet_editor capability at aal2 on an official, non-deleted
|
||||
-- sheet other than the historical one (writes), returning the actor. The
|
||||
-- capability is checked first, so only an editor learns a sheet is historical.
|
||||
create or replace function private.require_sheet_author(p_sheet_id uuid)
|
||||
returns uuid
|
||||
language plpgsql
|
||||
stable
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
declare
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
begin
|
||||
if not private.can_edit_sheet(v_actor, p_sheet_id) then
|
||||
raise exception 'rights.capability' using errcode = '42501';
|
||||
end if;
|
||||
if p_sheet_id = private.historical_sheet_id() then
|
||||
raise exception 'sheet.historical_read_only' using errcode = '42501';
|
||||
end if;
|
||||
return v_actor;
|
||||
end;
|
||||
$$;
|
||||
@@ -338,9 +378,9 @@ security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
declare
|
||||
v_actor uuid := private.require_actor();
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
begin
|
||||
if not (private.caller_has('sheet_publisher') and exists (
|
||||
if not (private.grant_active(v_actor, 'sheet_publisher') and exists (
|
||||
select 1 from public.anatomical_sheets s
|
||||
where s.id = p_sheet_id and s.is_official and s.status <> 'deleted'
|
||||
)) then
|
||||
@@ -350,6 +390,65 @@ begin
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- The structural guards a stored document passes at creation, save and
|
||||
-- working copy (contract §12.2): an object whose id-bearing collections are
|
||||
-- arrays, schema version 2, at most 2 MiB serialised. The table's own size
|
||||
-- CHECK (`pg_column_size(content)`, base migration) measures the binary jsonb,
|
||||
-- which a document of many small objects makes larger than its text: both
|
||||
-- measures are checked here, so either limit answers the closed code rather
|
||||
-- than a raw constraint error.
|
||||
create or replace function private.check_document_structure(p_doc jsonb)
|
||||
returns void
|
||||
language plpgsql
|
||||
immutable
|
||||
set search_path = ''
|
||||
as $$
|
||||
begin
|
||||
if not private.sheet_document_has_shape(p_doc) then
|
||||
raise exception 'document.shape' using errcode = '23514';
|
||||
end if;
|
||||
if (p_doc -> 'schema_version') is distinct from '2'::jsonb then
|
||||
raise exception 'document.schema_version' using errcode = '23514';
|
||||
end if;
|
||||
if octet_length(p_doc::text) > 2097152 or pg_column_size(p_doc) > 2097152 then
|
||||
raise exception 'document.too_large' using errcode = '23514';
|
||||
end if;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- A new official sheet row (creation and working copy). A slug outside the
|
||||
-- table's format is invalid input; one already used by a non-deleted official
|
||||
-- sheet answers `sheet.slug_taken`, the race included (unique index).
|
||||
create or replace function private.insert_official_sheet(
|
||||
p_species text,
|
||||
p_name text,
|
||||
p_slug text,
|
||||
p_description text,
|
||||
p_source uuid
|
||||
)
|
||||
returns uuid
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
declare
|
||||
v_sheet uuid;
|
||||
begin
|
||||
if p_slug is null or p_slug !~ '^[a-z0-9][a-z0-9-]{1,79}$' then
|
||||
raise exception 'invalid_input' using errcode = 'P0001';
|
||||
end if;
|
||||
begin
|
||||
insert into public.anatomical_sheets (species_id, name, slug, description, creator_user_id, is_official,
|
||||
visibility, working_copy_source_version_id)
|
||||
values (p_species, p_name, p_slug, p_description, null, true, 'private', p_source)
|
||||
returning id into v_sheet;
|
||||
exception when unique_violation then
|
||||
raise exception 'sheet.slug_taken' using errcode = 'P0001';
|
||||
end;
|
||||
return v_sheet;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Create an official sheet and its draft v1 from a core-built document.
|
||||
create or replace function public.create_official_sheet(
|
||||
slug text,
|
||||
@@ -363,18 +462,13 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_actor();
|
||||
v_actor uuid := private.require_capability('sheet_editor');
|
||||
v_species text := document ->> 'species_id';
|
||||
v_name text;
|
||||
v_description text;
|
||||
v_sheet uuid;
|
||||
begin
|
||||
if not private.caller_has('sheet_editor') then
|
||||
raise exception 'rights.capability' using errcode = '42501';
|
||||
end if;
|
||||
if not private.sheet_document_has_shape(document) then
|
||||
raise exception 'document.shape' using errcode = '23514';
|
||||
end if;
|
||||
perform private.check_document_structure(document);
|
||||
v_name := left(coalesce(document -> 'name' ->> 'fr', document -> 'name' ->> 'en', 'Sans nom'), 120);
|
||||
v_description := left(coalesce(document -> 'description' ->> 'fr', ''), 2000);
|
||||
-- An official sheet accepts an active or inactive species, never `other` (§7.2).
|
||||
@@ -384,9 +478,7 @@ begin
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
|
||||
insert into public.anatomical_sheets (species_id, name, slug, description, creator_user_id, is_official, visibility)
|
||||
values (v_species, v_name, slug, v_description, null, true, 'private')
|
||||
returning id into v_sheet;
|
||||
v_sheet := private.insert_official_sheet(v_species, v_name, create_official_sheet.slug, v_description, null);
|
||||
|
||||
perform private.log_event(
|
||||
v_actor, 'anatomical_sheet.created', 'anatomical_sheet', v_sheet, null, null,
|
||||
@@ -427,10 +519,17 @@ begin
|
||||
if v_source.id is null then
|
||||
raise exception 'sheet.no_source' using errcode = 'P0002';
|
||||
end if;
|
||||
if exists (select 1 from public.anatomical_sheet_versions v
|
||||
where v.sheet_id = create_sheet_draft.sheet_id and v.status = 'draft') then
|
||||
raise exception 'draft.exists' using errcode = 'P0001';
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
|
||||
-- Base only from a version published through a V1-2 round (§6.2).
|
||||
v_base := case when v_source.published_review_id is not null then v_source.id else null end;
|
||||
-- Base only from a version still `published` that was published through a
|
||||
-- V1-2 round (§6.2): a deprecated source gives no base, so every group of
|
||||
-- the new draft is to cover again.
|
||||
v_base := case when v_source.status = 'published' and v_source.published_review_id is not null
|
||||
then v_source.id end;
|
||||
v_draft := private.insert_sheet_draft(v_actor, sheet_id, v_source.content, change_note);
|
||||
update public.anatomical_sheet_versions set source_version_id = v_base where id = v_draft;
|
||||
return v_draft;
|
||||
@@ -452,14 +551,14 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
v_row public.anatomical_sheet_versions%rowtype;
|
||||
v_actor uuid;
|
||||
begin
|
||||
select v.* into v_row from public.anatomical_sheet_versions v where v.id = save_sheet_draft.version_id for update;
|
||||
if v_row.id is null then
|
||||
raise exception 'not_found' using errcode = '42501';
|
||||
end if;
|
||||
v_actor := private.require_sheet_author(v_row.sheet_id);
|
||||
perform private.require_sheet_author(v_row.sheet_id);
|
||||
if v_row.status <> 'draft' then
|
||||
raise exception 'draft.not_a_draft' using errcode = '55000';
|
||||
end if;
|
||||
@@ -474,15 +573,10 @@ begin
|
||||
if save_sheet_draft.expected_revision is distinct from v_row.revision then
|
||||
raise exception 'draft_conflict' using errcode = 'P0001';
|
||||
end if;
|
||||
if not private.sheet_document_has_shape(document) then
|
||||
raise exception 'document.shape' using errcode = '23514';
|
||||
end if;
|
||||
perform private.check_document_structure(document);
|
||||
if (document ->> 'species_id') is distinct from (select species_id from public.anatomical_sheets where id = v_row.sheet_id) then
|
||||
raise exception 'document.species' using errcode = '23514';
|
||||
end if;
|
||||
if octet_length(document::text) > 2097152 then
|
||||
raise exception 'document.too_large' using errcode = '23514';
|
||||
end if;
|
||||
if document = v_row.content then
|
||||
raise exception 'draft.unchanged' using errcode = 'P0001';
|
||||
end if;
|
||||
@@ -516,8 +610,8 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
v_row public.anatomical_sheet_versions%rowtype;
|
||||
v_actor uuid;
|
||||
v_round integer;
|
||||
v_id uuid;
|
||||
begin
|
||||
@@ -525,7 +619,7 @@ begin
|
||||
if v_row.id is null then
|
||||
raise exception 'not_found' using errcode = '42501';
|
||||
end if;
|
||||
v_actor := private.require_sheet_author(v_row.sheet_id);
|
||||
perform private.require_sheet_author(v_row.sheet_id);
|
||||
if v_row.status <> 'draft' then
|
||||
raise exception 'draft.not_a_draft' using errcode = '55000';
|
||||
end if;
|
||||
@@ -542,6 +636,11 @@ begin
|
||||
if char_length(note) > 1000 then
|
||||
raise exception 'invalid_input' using errcode = 'P0001';
|
||||
end if;
|
||||
-- Every drawing id of the document is registered for the sheet (§12.2,
|
||||
-- kept at submission and publication).
|
||||
if cardinality(private.sheet_document_unknown_assets(v_row.sheet_id, v_row.content)) > 0 then
|
||||
raise exception 'view.asset.unknown' using errcode = 'P0001';
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
|
||||
select coalesce(max(round), 0) + 1 into v_round
|
||||
@@ -561,7 +660,9 @@ begin
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Withdraw a round in verifying, open or approved; the draft unfreezes.
|
||||
-- Withdraw a round of a draft in verifying, open or approved; the draft
|
||||
-- unfreezes. The round of a published version stays approved (§6.1): it is
|
||||
-- the version's review link and never withdrawn.
|
||||
create or replace function public.withdraw_sheet_review(review_id uuid, note text default null)
|
||||
returns uuid
|
||||
language plpgsql
|
||||
@@ -571,22 +672,33 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
v_review public.anatomical_sheet_reviews%rowtype;
|
||||
v_version public.anatomical_sheet_versions%rowtype;
|
||||
v_sheet uuid;
|
||||
v_actor uuid;
|
||||
begin
|
||||
select r.* into v_review from public.anatomical_sheet_reviews r where r.id = withdraw_sheet_review.review_id for update;
|
||||
if v_review.id is null then
|
||||
select r.version_id into v_review.version_id from public.anatomical_sheet_reviews r
|
||||
where r.id = withdraw_sheet_review.review_id;
|
||||
if v_review.version_id is null then
|
||||
raise exception 'not_found' using errcode = '42501';
|
||||
end if;
|
||||
select sheet_id into v_sheet from public.anatomical_sheet_versions where id = v_review.version_id;
|
||||
v_actor := private.require_sheet_author(v_sheet);
|
||||
-- Lock order version, then round (§6.1): a publication holding the version
|
||||
-- either completes first (the version is then no draft) or sees the round
|
||||
-- withdrawn.
|
||||
select v.* into v_version from public.anatomical_sheet_versions v where v.id = v_review.version_id for share;
|
||||
v_sheet := v_version.sheet_id;
|
||||
perform private.require_sheet_author(v_sheet);
|
||||
select r.* into v_review from public.anatomical_sheet_reviews r where r.id = withdraw_sheet_review.review_id for update;
|
||||
if v_version.status <> 'draft' then
|
||||
raise exception 'draft.not_a_draft' using errcode = '55000';
|
||||
end if;
|
||||
if v_review.status not in ('verifying', 'open', 'approved') then
|
||||
raise exception 'round.terminal' using errcode = '55000';
|
||||
end if;
|
||||
if char_length(note) > 1000 then
|
||||
raise exception 'invalid_input' using errcode = 'P0001';
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
|
||||
update public.anatomical_sheet_reviews
|
||||
set status = 'withdrawn', withdrawn_by = v_actor, withdrawn_at = now(), withdrawal_note = note
|
||||
@@ -613,7 +725,7 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_actor();
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
v_review public.anatomical_sheet_reviews%rowtype;
|
||||
v_sheet uuid;
|
||||
v_id uuid;
|
||||
@@ -671,15 +783,12 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_actor();
|
||||
v_actor uuid := private.require_capability('sheet_reviewer');
|
||||
v_review public.anatomical_sheet_reviews%rowtype;
|
||||
v_sheet uuid;
|
||||
v_group jsonb;
|
||||
v_id uuid;
|
||||
begin
|
||||
if not private.caller_has('sheet_reviewer') then
|
||||
raise exception 'rights.capability' using errcode = '42501';
|
||||
end if;
|
||||
select r.* into v_review from public.anatomical_sheet_reviews r where r.id = acknowledge_sheet_review_group.review_id for update;
|
||||
if v_review.id is null then
|
||||
raise exception 'not_found' using errcode = '42501';
|
||||
@@ -705,10 +814,18 @@ begin
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
|
||||
-- Append-only (§6.3): acknowledging a group twice in the same round keeps the
|
||||
-- first row (the verdict, hence the digest, is fixed while the round is open).
|
||||
insert into public.anatomical_sheet_review_acknowledgments (review_id, group_key, group_digest, reviewer)
|
||||
values (acknowledge_sheet_review_group.review_id, group_key, group_digest, v_actor)
|
||||
on conflict (review_id, group_key, reviewer) do update set group_digest = excluded.group_digest, created_at = now()
|
||||
on conflict (review_id, group_key, reviewer) do nothing
|
||||
returning id into v_id;
|
||||
if v_id is null then
|
||||
select a.id into v_id from public.anatomical_sheet_review_acknowledgments a
|
||||
where a.review_id = acknowledge_sheet_review_group.review_id
|
||||
and a.group_key = acknowledge_sheet_review_group.group_key and a.reviewer = v_actor;
|
||||
return v_id;
|
||||
end if;
|
||||
perform private.log_event(
|
||||
v_actor, 'anatomical_sheet_review.acknowledged', 'anatomical_sheet_review', v_review.id, null, null,
|
||||
jsonb_build_object('sheet_id', v_sheet, 'group_key', group_key)
|
||||
@@ -785,23 +902,27 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_actor();
|
||||
v_actor uuid := private.require_capability('sheet_reviewer');
|
||||
v_review public.anatomical_sheet_reviews%rowtype;
|
||||
v_version public.anatomical_sheet_versions%rowtype;
|
||||
v_sheet public.anatomical_sheets%rowtype;
|
||||
v_uncovered text[];
|
||||
begin
|
||||
if not private.caller_has('sheet_reviewer') then
|
||||
raise exception 'rights.capability' using errcode = '42501';
|
||||
end if;
|
||||
if char_length(note) not between 1 and 4000 then
|
||||
raise exception 'invalid_input' using errcode = 'P0001';
|
||||
end if;
|
||||
-- Lock order version then round (contract §6.1), matching publish, so the
|
||||
-- two never deadlock.
|
||||
select version_id into v_version.id from public.anatomical_sheet_reviews where id = decide_sheet_review.review_id;
|
||||
-- Lock order sheet, then version, then round (contract §6.1), as publish
|
||||
-- does. The sheet is share-locked: an archive (which updates the row) waits
|
||||
-- for the decision or is seen by it, so no approval lands on a sheet archived
|
||||
-- in the meantime (§6.7, M1).
|
||||
select v.sheet_id, v.id into v_sheet.id, v_version.id
|
||||
from public.anatomical_sheet_reviews r
|
||||
join public.anatomical_sheet_versions v on v.id = r.version_id
|
||||
where r.id = decide_sheet_review.review_id;
|
||||
if v_version.id is null then
|
||||
raise exception 'not_found' using errcode = '42501';
|
||||
end if;
|
||||
select s.* into v_sheet from public.anatomical_sheets s where s.id = v_sheet.id for share;
|
||||
select v.* into v_version from public.anatomical_sheet_versions v where v.id = v_version.id for update;
|
||||
select r.* into v_review from public.anatomical_sheet_reviews r where r.id = decide_sheet_review.review_id for update;
|
||||
if v_review.status <> 'open' then
|
||||
@@ -813,9 +934,19 @@ begin
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
|
||||
if approve then
|
||||
-- Sheet active, official, other than the historical sheet (§6.7).
|
||||
if v_sheet.status <> 'active' then
|
||||
raise exception 'sheet.inactive' using errcode = '55000';
|
||||
end if;
|
||||
if v_sheet.id = private.historical_sheet_id() then
|
||||
raise exception 'sheet.historical_read_only' using errcode = '42501';
|
||||
end if;
|
||||
-- Publishable content: verdict present on the current revision, no
|
||||
-- to_confirm / inherited element, groups covered.
|
||||
if v_review.verdict is null or v_review.revision <> v_version.revision then
|
||||
if v_review.verdict is null then
|
||||
raise exception 'verdict.missing' using errcode = 'P0001';
|
||||
end if;
|
||||
if v_review.revision <> v_version.revision then
|
||||
raise exception 'verdict.stale' using errcode = 'P0001';
|
||||
end if;
|
||||
if private.sheet_has_to_confirm(v_version.content) then
|
||||
@@ -871,6 +1002,7 @@ declare
|
||||
v_name text;
|
||||
v_description text;
|
||||
begin
|
||||
perform private.require_catalog_actor();
|
||||
-- Lock order sheet, then version, then round (contract §6.1).
|
||||
select v.sheet_id, v.id into v_sheet.id, v_version.id
|
||||
from public.anatomical_sheet_versions v
|
||||
@@ -899,9 +1031,11 @@ begin
|
||||
if v_review.verdict is null then
|
||||
raise exception 'verdict.missing' using errcode = 'P0001';
|
||||
end if;
|
||||
-- The approving reviewer's grant must still be active at publication (§5.2,
|
||||
-- §6.7): a revoked reviewer's approval does not carry a publication.
|
||||
if not private.grant_active(v_review.decided_by, 'sheet_reviewer') then
|
||||
-- The approver is outside the round's authors and its reviewer grant is
|
||||
-- still active at publication (§5.2, §6.7): a revoked reviewer's approval
|
||||
-- does not carry a publication.
|
||||
if v_review.decided_by is null or v_review.authors @> array[v_review.decided_by]
|
||||
or not private.grant_active(v_review.decided_by, 'sheet_reviewer') then
|
||||
raise exception 'publish.approval.invalid' using errcode = 'P0001';
|
||||
end if;
|
||||
-- Species active and not `other` (§6.7).
|
||||
@@ -925,7 +1059,11 @@ begin
|
||||
if coalesce(array_length(private.uncovered_review_groups(v_review.id), 1), 0) > 0 then
|
||||
raise exception 'review.groups.uncovered' using errcode = 'P0001';
|
||||
end if;
|
||||
-- Each referenced drawing confirmed, size and sha256 equal to the verdict's.
|
||||
-- Every drawing id registered for the sheet (§12.2), each referenced
|
||||
-- drawing confirmed, size and sha256 equal to the verdict's.
|
||||
if cardinality(private.sheet_document_unknown_assets(v_version.sheet_id, v_version.content)) > 0 then
|
||||
raise exception 'view.asset.unknown' using errcode = 'P0001';
|
||||
end if;
|
||||
if exists (
|
||||
select 1 from unnest(private.sheet_document_asset_ids(v_version.content)) as ref
|
||||
where not exists (
|
||||
@@ -950,6 +1088,7 @@ begin
|
||||
end if;
|
||||
v_name := left(coalesce(v_version.content -> 'name' ->> 'fr', v_sheet.name), 120);
|
||||
v_description := left(coalesce(v_version.content -> 'description' ->> 'fr', ''), 2000);
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
|
||||
update public.anatomical_sheet_versions
|
||||
set status = 'published', published_at = now(), published_by = v_actor,
|
||||
@@ -980,21 +1119,27 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
v_version public.anatomical_sheet_versions%rowtype;
|
||||
v_actor uuid;
|
||||
v_next uuid;
|
||||
begin
|
||||
if char_length(reason) > 500 then
|
||||
raise exception 'invalid_input' using errcode = 'P0001';
|
||||
end if;
|
||||
select v.* into v_version from public.anatomical_sheet_versions v where v.id = deprecate_sheet_version.version_id for update;
|
||||
if v_version.id is null then
|
||||
select v.sheet_id into v_version.sheet_id from public.anatomical_sheet_versions v where v.id = deprecate_sheet_version.version_id;
|
||||
if v_version.sheet_id is null then
|
||||
raise exception 'not_found' using errcode = '42501';
|
||||
end if;
|
||||
v_actor := private.require_sheet_publisher(v_version.sheet_id);
|
||||
perform private.require_sheet_publisher(v_version.sheet_id);
|
||||
-- Lock order sheet, then version (§6.1). Two deprecations of the same sheet
|
||||
-- are serialised on the sheet row, so each picks the next current version
|
||||
-- from the other's committed state and never names a deprecated one.
|
||||
perform 1 from public.anatomical_sheets s where s.id = v_version.sheet_id for update;
|
||||
select v.* into v_version from public.anatomical_sheet_versions v where v.id = deprecate_sheet_version.version_id for update;
|
||||
if v_version.status <> 'published' then
|
||||
raise exception 'version.not_published' using errcode = '55000';
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
|
||||
update public.anatomical_sheet_versions
|
||||
set status = 'deprecated', deprecated_at = now(), deprecated_by = v_actor
|
||||
@@ -1038,6 +1183,7 @@ begin
|
||||
if v_status <> p_from then
|
||||
raise exception 'sheet.state' using errcode = '55000';
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(p_actor);
|
||||
update public.anatomical_sheets
|
||||
set status = p_to,
|
||||
archived_at = case when p_to = 'archived' then now() when p_to = 'active' then null else archived_at end,
|
||||
@@ -1062,6 +1208,9 @@ begin
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Restoring a working copy of the historical sheet keeps the single active
|
||||
-- slot (§14.3): refused while another copy is active. The historical sheet
|
||||
-- row is locked as in create_sheet_working_copy, so the two serialise.
|
||||
create or replace function public.restore_anatomical_sheet(sheet_id uuid)
|
||||
returns uuid
|
||||
language plpgsql
|
||||
@@ -1069,10 +1218,17 @@ volatile
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_sheet_publisher(sheet_id);
|
||||
begin
|
||||
return private.set_sheet_status(v_actor, sheet_id, 'archived', 'active', 'anatomical_sheet.restored');
|
||||
if private.is_historical_working_copy(restore_anatomical_sheet.sheet_id) then
|
||||
perform 1 from public.anatomical_sheets s where s.id = private.historical_sheet_id() for no key update;
|
||||
if private.historical_working_copy_active(restore_anatomical_sheet.sheet_id) then
|
||||
raise exception 'working_copy.exists' using errcode = 'P0001';
|
||||
end if;
|
||||
end if;
|
||||
return private.set_sheet_status(v_actor, restore_anatomical_sheet.sheet_id, 'archived', 'active', 'anatomical_sheet.restored');
|
||||
end;
|
||||
$$;
|
||||
|
||||
@@ -1114,17 +1270,18 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
v_version public.anatomical_sheet_versions%rowtype;
|
||||
v_actor uuid;
|
||||
begin
|
||||
select v.* into v_version from public.anatomical_sheet_versions v where v.id = delete_sheet_version.version_id;
|
||||
select v.* into v_version from public.anatomical_sheet_versions v where v.id = delete_sheet_version.version_id for update;
|
||||
if v_version.id is null then
|
||||
raise exception 'not_found' using errcode = '42501';
|
||||
end if;
|
||||
v_actor := private.require_sheet_author(v_version.sheet_id);
|
||||
perform private.require_sheet_author(v_version.sheet_id);
|
||||
if exists (select 1 from public.anatomical_sheet_reviews r where r.version_id = v_version.id) then
|
||||
raise exception 'version.has_rounds' using errcode = '55000';
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
delete from public.anatomical_sheet_versions where id = v_version.id;
|
||||
perform private.log_event(
|
||||
v_actor, 'anatomical_sheet_version.deleted', 'anatomical_sheet_version', v_version.id, null, null,
|
||||
@@ -1137,6 +1294,36 @@ $$;
|
||||
-- ===========================================================================
|
||||
-- 8. Equine working copy (contract §14.3)
|
||||
-- ===========================================================================
|
||||
-- A sheet created as a working copy of a version of the historical sheet.
|
||||
create or replace function private.is_historical_working_copy(p_sheet_id uuid)
|
||||
returns boolean
|
||||
language sql
|
||||
stable
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
select exists (
|
||||
select 1 from public.anatomical_sheets s
|
||||
join public.anatomical_sheet_versions v on v.id = s.working_copy_source_version_id
|
||||
where s.id = p_sheet_id and v.sheet_id = private.historical_sheet_id()
|
||||
);
|
||||
$$;
|
||||
|
||||
-- An active working copy of the historical sheet other than p_except.
|
||||
create or replace function private.historical_working_copy_active(p_except uuid)
|
||||
returns boolean
|
||||
language sql
|
||||
stable
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
select exists (
|
||||
select 1 from public.anatomical_sheets s
|
||||
where s.status = 'active' and s.id is distinct from p_except
|
||||
and private.is_historical_working_copy(s.id)
|
||||
);
|
||||
$$;
|
||||
|
||||
create or replace function public.create_sheet_working_copy(source_version_id uuid, slug text)
|
||||
returns uuid
|
||||
language plpgsql
|
||||
@@ -1146,7 +1333,7 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_actor();
|
||||
v_actor uuid := private.require_capability('sheet_editor');
|
||||
v_source public.anatomical_sheet_versions%rowtype;
|
||||
v_sheet uuid;
|
||||
v_draft uuid;
|
||||
@@ -1154,22 +1341,16 @@ declare
|
||||
v_new uuid;
|
||||
r record;
|
||||
begin
|
||||
if not private.caller_has('sheet_editor') then
|
||||
raise exception 'rights.capability' using errcode = '42501';
|
||||
end if;
|
||||
select v.* into v_source from public.anatomical_sheet_versions v where v.id = create_sheet_working_copy.source_version_id;
|
||||
-- Source restricted to the historical sheet, and no other (§14.3).
|
||||
if v_source.id is null or v_source.sheet_id <> private.historical_sheet_id() then
|
||||
raise exception 'working_copy.source_invalid' using errcode = 'P0001';
|
||||
end if;
|
||||
-- At most one active working copy of the historical sheet.
|
||||
if exists (
|
||||
select 1 from public.anatomical_sheets s
|
||||
where s.working_copy_source_version_id is not null and s.status = 'active'
|
||||
and s.id <> private.historical_sheet_id()
|
||||
and exists (select 1 from public.anatomical_sheet_versions vv
|
||||
where vv.id = s.working_copy_source_version_id and vv.sheet_id = private.historical_sheet_id())
|
||||
) then
|
||||
-- At most one active working copy of the historical sheet. Two concurrent
|
||||
-- creations (or a creation and a restore) serialise on the historical sheet
|
||||
-- row; the lock writes nothing, so its protection trigger never fires.
|
||||
perform 1 from public.anatomical_sheets s where s.id = private.historical_sheet_id() for no key update;
|
||||
if private.historical_working_copy_active(null) then
|
||||
raise exception 'working_copy.exists' using errcode = 'P0001';
|
||||
end if;
|
||||
-- Every referenced drawing uploaded, with an object of the recorded size.
|
||||
@@ -1187,11 +1368,10 @@ begin
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
|
||||
insert into public.anatomical_sheets (species_id, name, slug, description, creator_user_id, is_official, visibility, working_copy_source_version_id)
|
||||
select species_id, left(coalesce(v_source.content -> 'name' ->> 'fr', name), 120), create_sheet_working_copy.slug,
|
||||
description, null, true, 'private', v_source.id
|
||||
from public.anatomical_sheets where id = v_source.sheet_id
|
||||
returning id into v_sheet;
|
||||
select private.insert_official_sheet(s.species_id, left(coalesce(v_source.content -> 'name' ->> 'fr', s.name), 120),
|
||||
create_sheet_working_copy.slug, s.description, v_source.id)
|
||||
into v_sheet
|
||||
from public.anatomical_sheets s where s.id = v_source.sheet_id;
|
||||
|
||||
-- Register the source drawings for the new sheet (same bytes, shared object).
|
||||
for r in
|
||||
@@ -1206,9 +1386,7 @@ begin
|
||||
end loop;
|
||||
|
||||
-- Size checked as in §6.2: the remapped document must fit the 2 MiB cap.
|
||||
if octet_length(private.remap_document_assets(v_source.content, v_map)::text) > 2097152 then
|
||||
raise exception 'document.too_large' using errcode = '23514';
|
||||
end if;
|
||||
perform private.check_document_structure(private.remap_document_assets(v_source.content, v_map));
|
||||
v_draft := private.insert_sheet_draft(
|
||||
v_actor, v_sheet, private.remap_document_assets(v_source.content, v_map),
|
||||
'Copie de travail de la fiche historique (version ' || v_source.version_number || ', révision ' || v_source.revision || ')'
|
||||
@@ -1234,11 +1412,8 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_actor();
|
||||
v_actor uuid := private.require_capability('sheet_editor');
|
||||
begin
|
||||
if not private.caller_has('sheet_editor') then
|
||||
raise exception 'rights.capability' using errcode = '42501';
|
||||
end if;
|
||||
if create_species.id is null or create_species.id !~ '^[a-z0-9][a-z0-9_-]{0,63}$' or create_species.id = 'other'
|
||||
or char_length(name_fr) not between 1 and 80 or char_length(name_en) not between 1 and 80 then
|
||||
raise exception 'invalid_input' using errcode = 'P0001';
|
||||
@@ -1264,11 +1439,8 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_actor();
|
||||
v_actor uuid := private.require_capability('sheet_editor');
|
||||
begin
|
||||
if not private.caller_has('sheet_editor') then
|
||||
raise exception 'rights.capability' using errcode = '42501';
|
||||
end if;
|
||||
if char_length(name_fr) not between 1 and 80 or char_length(name_en) not between 1 and 80 then
|
||||
raise exception 'invalid_input' using errcode = 'P0001';
|
||||
end if;
|
||||
@@ -1293,15 +1465,16 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_actor();
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
begin
|
||||
if not (private.has_mfa() and private.is_platform_owner(v_actor)) then
|
||||
if not private.is_platform_owner(v_actor) then
|
||||
raise exception 'rights.owner' using errcode = '42501';
|
||||
end if;
|
||||
perform 1 from public.species s where s.id = activate_species.id for update;
|
||||
if not found then
|
||||
raise exception 'not_found' using errcode = '42501';
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
update public.species s set active = true where s.id = activate_species.id and not s.active;
|
||||
perform private.log_event(v_actor, 'species.activated', 'species', null, null, null, jsonb_build_object('id', activate_species.id));
|
||||
return activate_species.id;
|
||||
@@ -1317,9 +1490,9 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_actor();
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
begin
|
||||
if not (private.has_mfa() and private.is_platform_owner(v_actor)) then
|
||||
if not private.is_platform_owner(v_actor) then
|
||||
raise exception 'rights.owner' using errcode = '42501';
|
||||
end if;
|
||||
if char_length(name_fr) not between 1 and 80 or char_length(name_en) not between 1 and 80 then
|
||||
@@ -1329,6 +1502,7 @@ begin
|
||||
if not found then
|
||||
raise exception 'species.not_active' using errcode = 'P0001';
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
update public.species s set name_fr = rename_species.name_fr, name_en = rename_species.name_en where s.id = rename_species.id;
|
||||
perform private.log_event(v_actor, 'species.renamed', 'species', null, null, null, jsonb_build_object('id', rename_species.id));
|
||||
return rename_species.id;
|
||||
@@ -1344,7 +1518,8 @@ security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
begin
|
||||
if not (private.caller_is_catalog_staff() or (private.has_mfa() and private.is_platform_owner((select auth.uid())))) then
|
||||
perform private.require_catalog_actor();
|
||||
if not (private.caller_is_catalog_staff() or private.is_platform_owner((select auth.uid()))) then
|
||||
raise exception 'rights.capability' using errcode = '42501';
|
||||
end if;
|
||||
return coalesce((
|
||||
@@ -1494,17 +1669,21 @@ set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid;
|
||||
v_row public.anatomical_sheet_assets%rowtype;
|
||||
v_object storage.objects%rowtype;
|
||||
begin
|
||||
select a.* into v_row from public.anatomical_sheet_assets a where a.id = confirm_sheet_asset_upload.asset_id;
|
||||
if auth.uid() is not null then
|
||||
v_actor := private.require_catalog_actor();
|
||||
elsif auth.role() is distinct from 'service_role' then
|
||||
raise exception 'rights.capability' using errcode = '42501';
|
||||
end if;
|
||||
select a.* into v_row from public.anatomical_sheet_assets a where a.id = confirm_sheet_asset_upload.asset_id for update;
|
||||
if v_row.id is null then
|
||||
raise exception 'not_found' using errcode = '42501';
|
||||
end if;
|
||||
if auth.uid() is not null then
|
||||
if v_actor is not null then
|
||||
perform private.require_sheet_author(v_row.sheet_id);
|
||||
elsif auth.role() is distinct from 'service_role' then
|
||||
raise exception 'rights.capability' using errcode = '42501';
|
||||
end if;
|
||||
select o.* into v_object from storage.objects o
|
||||
where o.bucket_id = 'anatomical-sheet-assets' and o.name = v_row.sha256 || '.svg';
|
||||
@@ -1515,6 +1694,10 @@ begin
|
||||
raise exception 'size_mismatch' using errcode = 'P0001';
|
||||
end if;
|
||||
if v_row.uploaded_at is null then
|
||||
-- The operator tool (service role, no actor) has no budget row.
|
||||
if v_actor is not null then
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
end if;
|
||||
update public.anatomical_sheet_assets set uploaded_at = now() where id = v_row.id;
|
||||
perform private.log_event(auth.uid(), 'anatomical_sheet_asset.uploaded', 'anatomical_sheet_asset', v_row.id);
|
||||
end if;
|
||||
@@ -1548,10 +1731,11 @@ begin
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- An editor uploads the pending key of their own sheet (contract §9.3 step 2).
|
||||
-- Routed through a single granted predicate, as the avatar bucket is, so that
|
||||
-- evaluating this policy against another bucket's insert needs no EXECUTE on
|
||||
-- the internal helpers.
|
||||
-- The editor who registered a pending row uploads its key (contract §9.3
|
||||
-- step 2: "exactly that key for the caller of a pending row"). Routed through
|
||||
-- a single granted predicate, as the avatar bucket is, so that evaluating this
|
||||
-- policy against another bucket's insert needs no EXECUTE on the internal
|
||||
-- helpers.
|
||||
create or replace function private.can_upload_sheet_asset_object(p_user_id uuid, p_object_name text)
|
||||
returns boolean
|
||||
language sql
|
||||
@@ -1566,12 +1750,48 @@ as $$
|
||||
where a.sha256 = left(p_object_name, 64)
|
||||
and p_object_name = a.sha256 || '.svg'
|
||||
and a.uploaded_at is null
|
||||
and a.created_by = p_user_id
|
||||
and private.caller_can_see_official_sheet(a.sheet_id)
|
||||
);
|
||||
$$;
|
||||
|
||||
comment on function private.can_upload_sheet_asset_object(uuid, text) is
|
||||
'Storage INSERT predicate for bucket anatomical-sheet-assets: the caller holds sheet_editor at aal2 and the pending key <sha256>.svg belongs to a non-deleted official sheet (contract §9.3).';
|
||||
'Storage INSERT predicate for bucket anatomical-sheet-assets: the caller holds sheet_editor at aal2 and registered a pending row of a non-deleted official sheet whose key is exactly <sha256>.svg (contract §9.3).';
|
||||
|
||||
-- Delete an unreferenced asset row (editor, aal2), replacing the base
|
||||
-- version: closed codes, the historical sheet refused, the write budget. The
|
||||
-- object stays in Storage (objects are never deleted, §9.5).
|
||||
create or replace function public.delete_sheet_asset(asset_id uuid)
|
||||
returns uuid
|
||||
language plpgsql
|
||||
volatile
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
declare
|
||||
v_actor uuid := private.require_catalog_actor();
|
||||
v_row public.anatomical_sheet_assets%rowtype;
|
||||
begin
|
||||
select a.* into v_row from public.anatomical_sheet_assets a where a.id = delete_sheet_asset.asset_id for update;
|
||||
if v_row.id is null then
|
||||
raise exception 'not_found' using errcode = '42501';
|
||||
end if;
|
||||
perform private.require_sheet_author(v_row.sheet_id);
|
||||
if exists (
|
||||
select 1 from public.anatomical_sheet_versions v
|
||||
where v.sheet_id = v_row.sheet_id and v_row.id::text = any (private.sheet_document_asset_ids(v.content))
|
||||
) then
|
||||
raise exception 'asset.referenced' using errcode = '55000';
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
delete from public.anatomical_sheet_assets where id = v_row.id;
|
||||
perform private.log_event(
|
||||
v_actor, 'anatomical_sheet_asset.deleted', 'anatomical_sheet_asset', v_row.id, null, null,
|
||||
jsonb_build_object('sheet_id', v_row.sheet_id)
|
||||
);
|
||||
return v_row.id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
revoke all on function private.can_upload_sheet_asset_object(uuid, text) from public, anon, authenticated, service_role;
|
||||
grant execute on function private.can_upload_sheet_asset_object(uuid, text) to authenticated;
|
||||
@@ -1688,6 +1908,12 @@ revoke all on function private.sheet_has_to_confirm(jsonb) from public, anon, au
|
||||
revoke all on function private.sheet_disclaimer_ok(jsonb) from public, anon, authenticated, service_role;
|
||||
revoke all on function private.on_sheet_version_content() from public, anon, authenticated, service_role;
|
||||
revoke all on function private.guard_sheet_version_update() from public, anon, authenticated, service_role;
|
||||
revoke all on function private.require_catalog_actor() from public, anon, authenticated, service_role;
|
||||
revoke all on function private.require_capability(public.catalog_grant_kind) from public, anon, authenticated, service_role;
|
||||
revoke all on function private.check_document_structure(jsonb) from public, anon, authenticated, service_role;
|
||||
revoke all on function private.insert_official_sheet(text, text, text, text, uuid) from public, anon, authenticated, service_role;
|
||||
revoke all on function private.is_historical_working_copy(uuid) from public, anon, authenticated, service_role;
|
||||
revoke all on function private.historical_working_copy_active(uuid) from public, anon, authenticated, service_role;
|
||||
revoke all on function private.require_sheet_author(uuid) from public, anon, authenticated, service_role;
|
||||
revoke all on function private.require_sheet_publisher(uuid) from public, anon, authenticated, service_role;
|
||||
revoke all on function private.verdict_group_digest(jsonb, text) from public, anon, authenticated, service_role;
|
||||
|
||||
Reference in new issue
Block a user