diff --git a/docs/development/V1-2_ANATOMICAL_SHEETS.md b/docs/development/V1-2_ANATOMICAL_SHEETS.md index a905fe6b..e274d23b 100644 --- a/docs/development/V1-2_ANATOMICAL_SHEETS.md +++ b/docs/development/V1-2_ANATOMICAL_SHEETS.md @@ -1519,7 +1519,8 @@ A drawing never blocks authoring: only the round waits for its verdict. - `get_sheet_version(version_id)` returns the sheet id, version id and number, status, species, document, `revision`, the fingerprint (published - only) and the drawings (asset id, sha256, size). Published and deprecated + and deprecated: owner decision 2026-11-02, BDR-098, M7; a draft has none) + and the drawings (asset id, sha256, size). Published and deprecated versions answer under the reader predicate (§5.3), drafts under a capability with `aal2`; unknown and forbidden versions get one answer. - The page downloads each drawing under RLS and builds a payload. A new core @@ -1751,7 +1752,7 @@ of the document's JSON text in one form, the same for every layer: | M4 `sheet_access_hardening` | V1-2.1 | `anatomical_catalog_grants` and its RPCs; `has_mfa`, `grant_active`, `caller_has`; `can_read_official_catalog` and the reader row predicate; `can_edit_sheet` redefined (F14); AR-1 B on the platform role RPCs and the private write functions (F19, §5.7), F17, F18; F1, F2, F3 (fork closed), F5, F6, F8, F9, F12 (every catalogue table), F13, F15; asset-row immutability trigger (F16); historical-sheet protection trigger (§14.2); reader column grants narrowed; personal creation and visibility changes refused | no row changes | | M5 `sheet_review_workflow` | V1-2.1 | version columns `revision`, `authors`, `source_version_id`, `published_review_id`, `document_fingerprint`; sheet column `working_copy_source_version_id`; revision, author and freeze triggers, the historical-sheet write rule; extended immutability guard (§6.8); rounds, comments, acknowledgments; `create_official_sheet`, `create_sheet_draft` adapted, `save_sheet_draft`, submit, withdraw, comment, acknowledge, decide, publish; `create_sheet_working_copy`; species RPCs; catalogue and version read RPCs; upload: `register_sheet_asset` extended, `confirm_sheet_asset_upload` with the storage check (F7), INSERT policy, bucket limit 2 MiB; mirror removal and status check (§12.2); superseded signatures dropped; the content-size CHECK replaced by the text measure (`octet_length(content::text)`, §12.2) | new columns take their defaults on the equine v1 row (`revision` 0, empty author set); its document, status, number and assets unchanged; the new size CHECK is validated against the existing rows when M5 is applied (the equine document measures 184 105 bytes as `content::text`, measured on the sandbox); bucket limit lowered; no round, no grant | | M6 `sheet_verification_jobs` | V1-2.3 | worker RPCs (claim, input, complete with the verdict structure check, fail), service role only, with the retry delay (hidden `not_before` column on the round, `private.sheet_verification_retry_delay`: owner decision 2026-11-01, replaces « without backoff ») and the retry by cause (definitive codes close at the first occurrence, `private.sheet_worker_final_codes`); written as `20261101100000_sheet_verification_jobs.sql`, pgTAP `021`, rehearsed on the sandbox from the state before M4 (M4 to M6 in one `migration up`) and from the state before M6 | none | -| M7 `sheet_admin_reads` | V1-2.5 | read RPCs of the admin space (owner decision 2026-11-02, BDR-097): the official sheet list with its badges (historical, working copy), statuses and round counts, and one sheet's overview (versions, rounds); capability holders at `aal2` only (`caller_can_see_official_sheet`); no write path, no grant, no policy or rule change; pgTAP allowed and denied cases | none | +| M7 `sheet_admin_reads` | V1-2.5 | read RPCs of the admin space (owner decision 2026-11-02, BDR-097): the official sheet list with its badges (historical, working copy), statuses and round counts, and one sheet's overview (versions, rounds); `get_sheet_version` redefined to return the fingerprint of deprecated versions too (BDR-098); the admin reads for capability holders at `aal2` only (`caller_can_see_official_sheet`); no write path, no grant, no policy or rule change; pgTAP allowed and denied cases | none | Each migration ships its pgTAP file (allowed and denied cases), regenerated `database.types.ts`, a `db:sandbox up upgrade` run from the DEV state (with a @@ -2575,6 +2576,7 @@ VERIFIED (BDR-092), V1-2.4 approved (§17.9); V1-2.3 VERIFIED as well (BDR-093). | Admin reads | the admin list needs the working-copy badge and round counts, which no table grant gives (§5.3) and no RPC returned: one read migration M7, capability holders at `aal2`, no write path | §12.3, §16.6 | | Working copy action | « Créer une copie de travail » built in V1-2.5 (board 10), landing on the copy's overview; the editor link arrives with V1-2.6 | §11.1, §16.6 | | EN term | « Habilitation » reads « Permission » in the English interface; `capability` stays the technical term | §3 | +| Deprecated fingerprint | found while building the catalogue: `PreviewSheet` checks the fingerprint of published and deprecated versions, `get_sheet_version` returned it for published only, so no withdrawn version could open; `get_sheet_version` now returns it for both (redefined in M7, BDR-098) | §10, §12.3 | --- diff --git a/supabase/migrations/20261102100000_sheet_admin_reads.sql b/supabase/migrations/20261102100000_sheet_admin_reads.sql index 81a65588..ad99bf13 100644 --- a/supabase/migrations/20261102100000_sheet_admin_reads.sql +++ b/supabase/migrations/20261102100000_sheet_admin_reads.sql @@ -13,7 +13,8 @@ -- rounds, with the names of the people who acted. -- Capability holders at aal2 only (`caller_is_catalog_staff`, the predicate -- of the draft reads); an unknown or forbidden sheet gets one answer. No --- write, no grant, no policy and no rule changes here. +-- write, no grant, no policy and no rule changes here. Also: `get_sheet_version` +-- returns the fingerprint of a deprecated version (§10, BDR-098). -- =========================================================================== -- 1. Helpers @@ -173,13 +174,68 @@ begin end; $$; +-- =========================================================================== +-- 3. get_sheet_version: the fingerprint of a deprecated version too +-- =========================================================================== +-- M5 returned the fingerprint for published versions only, while the core's +-- PreviewSheet checks it for published and deprecated versions alike (§10: a +-- deprecated version previews like a published one), so no withdrawn version +-- could open in the catalogue. A deprecated version keeps the fingerprint it +-- was published with, already public then; a draft still gets none. Owner +-- decision 2026-11-02 (BDR-098). Body otherwise identical to M5. +create or replace function public.get_sheet_version(version_id uuid) +returns jsonb +language plpgsql +stable +security definer +set search_path = '' +as $$ +#variable_conflict use_column +declare + v_uid uuid := auth.uid(); + v_version public.anatomical_sheet_versions%rowtype; + v_sheet public.anatomical_sheets%rowtype; + v_allowed boolean; +begin + if v_uid is null then + raise exception 'not_found' using errcode = '42501'; + end if; + select v.* into v_version from public.anatomical_sheet_versions v where v.id = get_sheet_version.version_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_version.sheet_id; + v_allowed := private.caller_can_see_official_sheet(v_sheet.id) + or (v_version.status in ('published', 'deprecated') and private.can_read_catalog_sheet(v_uid, v_sheet.id)); + if not v_allowed then + raise exception 'not_found' using errcode = '42501'; + end if; + return jsonb_build_object( + 'sheet_id', v_sheet.id, + 'version_id', v_version.id, + 'version_number', v_version.version_number, + 'status', v_version.status, + 'species_id', v_sheet.species_id, + 'document', v_version.content, + 'revision', v_version.revision, + 'fingerprint', case when v_version.status in ('published', 'deprecated') then v_version.document_fingerprint end, + 'drawings', coalesce(( + select jsonb_agg(jsonb_build_object('asset_id', a.id, 'sha256', a.sha256, 'byte_size', a.byte_size) order by a.id) + from public.anatomical_sheet_assets a + where a.sheet_id = v_sheet.id and a.uploaded_at is not null + and a.id::text = any (private.sheet_document_asset_ids(v_version.content)) + ), '[]'::jsonb) + ); +end; +$$; + comment on function public.list_official_sheets_admin() is 'Capability holders, live aal2 session: every official sheet that is not deleted, with its historical and working-copy badges, version and round states (V1-2 contract §11.1, M7).'; comment on function public.get_official_sheet_admin(uuid) is 'Capability holders, live aal2 session: one official sheet''s summary, versions (authors named) and rounds (actors named); unknown and forbidden ids answer not_found (V1-2 contract §11.1, M7).'; -- =========================================================================== --- 3. Privileges +-- 4. Privileges -- =========================================================================== revoke all on function private.sheet_admin_person(uuid) from public, anon, authenticated, service_role; revoke all on function private.official_sheet_admin_summary(uuid) from public, anon, authenticated, service_role; diff --git a/supabase/tests/022_sheet_admin_reads.sql b/supabase/tests/022_sheet_admin_reads.sql index 82ae7ef7..3395e509 100644 --- a/supabase/tests/022_sheet_admin_reads.sql +++ b/supabase/tests/022_sheet_admin_reads.sql @@ -4,7 +4,7 @@ -- unknown, deleted, personal or forbidden sheet gets one answer. Fixtures are -- scoped to the ids created here. begin; -select plan(40); +select plan(43); -- Accounts. O owner, A admin, E admin + editor, V reviewer, P admin + -- publisher, R reader, N no right. Every one has a verified factor and a live @@ -40,7 +40,8 @@ create table pg_temp.k (key text primary key, id uuid not null); insert into pg_temp.k values ('SP', '22200000-0000-0000-0000-000000000001'), ('SA', '22200000-0000-0000-0000-000000000002'), ('SD', '22200000-0000-0000-0000-000000000003'), ('PS', '22200000-0000-0000-0000-000000000004'), - ('WC', '22200000-0000-0000-0000-000000000005'), + ('WC', '22200000-0000-0000-0000-000000000005'), ('SX', '22200000-0000-0000-0000-000000000006'), + ('SX.v1', '22300000-0000-0000-0000-000000000061'), ('SX.v2', '22300000-0000-0000-0000-000000000062'), ('SP.v1', '22300000-0000-0000-0000-000000000011'), ('SP.v2', '22300000-0000-0000-0000-000000000012'), ('SA.v1', '22300000-0000-0000-0000-000000000021'), ('WC.v1', '22300000-0000-0000-0000-000000000051'), ('SP.r', '22500000-0000-0000-0000-000000000001'), @@ -64,13 +65,19 @@ insert into public.anatomical_sheets (id, species_id, name, slug, is_official, v (pg_temp.id('SA'), 'equine', 'SA', 'm22-sa', true, 'private', 'archived', now(), null, null, null), (pg_temp.id('SD'), 'equine', 'SD', 'm22-sd', true, 'private', 'deleted', null, now(), null, null), (pg_temp.id('PS'), 'equine', 'PS', 'm22-ps', false, 'private', 'active', null, null, '22000000-0000-0000-0000-00000000000e', null), - (pg_temp.id('WC'), 'equine', 'WC', 'm22-wc', true, 'private', 'active', null, null, null, pg_temp.id('h.v')); + (pg_temp.id('WC'), 'equine', 'WC', 'm22-wc', true, 'private', 'active', null, null, null, pg_temp.id('h.v')), + (pg_temp.id('SX'), 'equine', 'SX', 'm22-sx', true, 'private', 'active', null, null, null, null); insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, published_at, document_fingerprint) values (pg_temp.id('SP.v1'), pg_temp.id('SP'), 1, 'published', 2, pg_temp.doc('Publiée'), now(), repeat('0', 64)), (pg_temp.id('SP.v2'), pg_temp.id('SP'), 2, 'draft', 2, pg_temp.doc('Brouillon'), null, null), (pg_temp.id('SA.v1'), pg_temp.id('SA'), 1, 'published', 2, pg_temp.doc('Archivée'), now(), repeat('0', 64)), - (pg_temp.id('WC.v1'), pg_temp.id('WC'), 1, 'draft', 2, pg_temp.doc('Copie'), null, null); + (pg_temp.id('WC.v1'), pg_temp.id('WC'), 1, 'draft', 2, pg_temp.doc('Copie'), null, null), + (pg_temp.id('SX.v2'), pg_temp.id('SX'), 2, 'published', 2, pg_temp.doc('Courante'), now(), repeat('e', 64)); +insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, + published_at, deprecated_at, document_fingerprint) + values (pg_temp.id('SX.v1'), pg_temp.id('SX'), 1, 'deprecated', 2, pg_temp.doc('Retirée'), now(), now(), repeat('d', 64)); +update public.anatomical_sheets set current_version_id = pg_temp.id('SX.v2'), last_version_number = 2 where id = pg_temp.id('SX'); update public.anatomical_sheet_versions set authors = array['22000000-0000-0000-0000-00000000000e'::uuid] where id = pg_temp.id('SP.v2'); update public.anatomical_sheets set current_version_id = pg_temp.id('SP.v1'), last_version_number = 2 where id = pg_temp.id('SP'); @@ -133,11 +140,11 @@ select is(pg_temp.listed(), '42501 rights.mfa', 'editor at aal1: list refused'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 rights.mfa', 'editor at aal1: sheet refused'); select pg_temp.as_user('E', 'aal2'); -select is(pg_temp.listed(), 'SA,SP,WC,h', 'editor at aal2: every official sheet but the deleted one'); +select is(pg_temp.listed(), 'SA,SP,SX,WC,h', 'editor at aal2: every official sheet but the deleted one'); select pg_temp.as_user('V', 'aal2'); -select is(pg_temp.listed(), 'SA,SP,WC,h', 'reviewer at aal2: the same list'); +select is(pg_temp.listed(), 'SA,SP,SX,WC,h', 'reviewer at aal2: the same list'); select pg_temp.as_user('P', 'aal2'); -select is(pg_temp.listed(), 'SA,SP,WC,h', 'publisher at aal2: the same list'); +select is(pg_temp.listed(), 'SA,SP,SX,WC,h', 'publisher at aal2: the same list'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SD')) ->> 'slug' $$), '42501 not_found', 'deleted sheet: not_found'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('PS')) ->> 'slug' $$), '42501 not_found', 'personal sheet: not_found'); select is(pg_temp.val($$ select public.get_official_sheet_admin('22200000-0000-0000-0000-0000000000ff') ->> 'slug' $$), '42501 not_found', 'unknown id: not_found'); @@ -167,6 +174,13 @@ select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP') select is(pg_temp.val($$ select (public.get_official_sheet_admin(pg_temp.id('SA')) ->> 'status') $$), 'archived', 'overview of an archived sheet'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('WC')) #>> '{working_copy_source,version_id}' $$), pg_temp.id('h.v')::text, 'overview: the working copy names its source'); +-- ============ get_sheet_version: the fingerprint of a deprecated version (BDR-098) ============ +select pg_temp.as_user('R', 'aal1'); +select is(pg_temp.val($$ select public.get_sheet_version(pg_temp.id('SX.v1')) ->> 'fingerprint' $$), repeat('d', 64), 'reader: a deprecated version carries its publication fingerprint'); +select is(pg_temp.val($$ select public.get_sheet_version(pg_temp.id('SX.v2')) ->> 'fingerprint' $$), repeat('e', 64), 'reader: a published version carries its fingerprint'); +select pg_temp.as_user('E', 'aal2'); +select is(pg_temp.val($$ select public.get_sheet_version(pg_temp.id('SP.v2')) ->> 'fingerprint' $$), 'ok', 'editor: a draft has no fingerprint'); + -- ============ Privileges and nature ============ reset role; select ok(not has_function_privilege('anon', 'public.list_official_sheets_admin()', 'execute')