fix(app): grant expiry ends the chosen day in Paris; species order 0..9999 (V1-2.5 L2)
Owner checkpoint 2026-11-02: expiry 23:59:59.999 Europe/Paris (picker, validation, display), tests under non-Paris zones and DST days; M7 redefines create_species and update_inactive_species with the range check; pgTAP 015 and 022; « Relecteur ou relectrice »; object_conflict EN; setup key selectable, no Copy.
This commit is contained in:
1 parent
4111efa1d1
commit
51225cacf6
17 files changed
+437
-61
No files matched your search
@@ -13,8 +13,10 @@
|
||||
-- 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. Also: `get_sheet_version`
|
||||
-- returns the fingerprint of a deprecated version (§10, BDR-098).
|
||||
-- table, grant, policy or rule changes here. Also: `get_sheet_version`
|
||||
-- returns the fingerprint of a deprecated version (§10, BDR-098); and
|
||||
-- `create_species` / `update_inactive_species` refuse a sort order outside
|
||||
-- 0..9 999 (owner 2026-11-02), their privileges restated.
|
||||
|
||||
-- ===========================================================================
|
||||
-- 1. Helpers
|
||||
@@ -247,3 +249,68 @@ grant execute on function public.get_official_sheet_admin(uuid) to authenticated
|
||||
-- Kept by `create or replace`, stated again so this file stands alone.
|
||||
revoke all on function public.get_sheet_version(uuid) from public, anon, service_role;
|
||||
grant execute on function public.get_sheet_version(uuid) to authenticated;
|
||||
|
||||
-- ===========================================================================
|
||||
-- 5. Species order range (owner 2026-11-02)
|
||||
-- ===========================================================================
|
||||
-- The M5 bodies, with the sort order bounded 0..9 999 (null keeps today's
|
||||
-- meaning: the default 100 on creation, the stored value on update).
|
||||
create or replace function public.create_species(id text, name_fr text, name_en text, sort_order integer default 100)
|
||||
returns text
|
||||
language plpgsql
|
||||
volatile
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_capability('sheet_editor');
|
||||
begin
|
||||
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
|
||||
or (create_species.sort_order is not null and create_species.sort_order not between 0 and 9999) then
|
||||
raise exception 'invalid_input' using errcode = 'P0001';
|
||||
end if;
|
||||
if exists (select 1 from public.species s where s.id = create_species.id) then
|
||||
raise exception 'species.exists' using errcode = 'P0001';
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
insert into public.species (id, name_fr, name_en, sort_order, active)
|
||||
values (create_species.id, name_fr, name_en, coalesce(sort_order, 100), false);
|
||||
perform private.log_event(v_actor, 'species.created', 'species', null, null, null,
|
||||
jsonb_build_object('id', create_species.id));
|
||||
return create_species.id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace function public.update_inactive_species(id text, name_fr text, name_en text, sort_order integer)
|
||||
returns text
|
||||
language plpgsql
|
||||
volatile
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_column
|
||||
declare
|
||||
v_actor uuid := private.require_capability('sheet_editor');
|
||||
begin
|
||||
if char_length(name_fr) not between 1 and 80 or char_length(name_en) not between 1 and 80
|
||||
or (update_inactive_species.sort_order is not null and update_inactive_species.sort_order not between 0 and 9999) then
|
||||
raise exception 'invalid_input' using errcode = 'P0001';
|
||||
end if;
|
||||
perform 1 from public.species s where s.id = update_inactive_species.id and not s.active for update;
|
||||
if not found then
|
||||
raise exception 'species.not_inactive' using errcode = 'P0001';
|
||||
end if;
|
||||
perform private.consume_catalog_write_budget(v_actor);
|
||||
update public.species s set name_fr = update_inactive_species.name_fr, name_en = update_inactive_species.name_en,
|
||||
sort_order = coalesce(update_inactive_species.sort_order, s.sort_order) where s.id = update_inactive_species.id;
|
||||
perform private.log_event(v_actor, 'species.updated', 'species', null, null, null, jsonb_build_object('id', update_inactive_species.id));
|
||||
return update_inactive_species.id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
revoke all on function public.create_species(text, text, text, integer) from public, anon, service_role;
|
||||
revoke all on function public.update_inactive_species(text, text, text, integer) from public, anon, service_role;
|
||||
grant execute on function public.create_species(text, text, text, integer) to authenticated;
|
||||
grant execute on function public.update_inactive_species(text, text, text, integer) to authenticated;
|
||||
@@ -5,7 +5,7 @@
|
||||
-- proven in packages/supabase/tests/sandbox/sheets.sandbox.ts, the seed owner's
|
||||
-- factors on an upgraded stack in seed-owner-mfa.sandbox.ts.
|
||||
begin;
|
||||
select plan(37);
|
||||
select plan(44);
|
||||
|
||||
insert into auth.users (id, email, email_confirmed_at) values
|
||||
('d0000000-0000-0000-0000-000000000001', 'own@t.local', now()), -- owner
|
||||
@@ -190,6 +190,57 @@ select ok(not private.grant_active('d0000000-0000-0000-0000-00000000000c','catal
|
||||
set local role authenticated;
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
|
||||
|
||||
-- Expiry instants (owner 2026-11-02, Europe/Paris end of day): the stored
|
||||
-- instant is the same whatever the session TimeZone; the DST days are future
|
||||
-- ones the RPC accepts. One fresh account per grant (one active row per kind).
|
||||
reset role;
|
||||
insert into auth.users (id, email, email_confirmed_at) values
|
||||
('d0000000-0000-0000-0000-0000000000b1', 'tz1@t.local', now()),
|
||||
('d0000000-0000-0000-0000-0000000000b2', 'tz2@t.local', now()),
|
||||
('d0000000-0000-0000-0000-0000000000b3', 'tz3@t.local', now()),
|
||||
('d0000000-0000-0000-0000-0000000000b4', 'tz4@t.local', now()),
|
||||
('d0000000-0000-0000-0000-0000000000b5', 'st1@t.local', now()),
|
||||
('d0000000-0000-0000-0000-0000000000b6', 'st2@t.local', now()),
|
||||
('d0000000-0000-0000-0000-0000000000b7', 'st3@t.local', now());
|
||||
set local role authenticated;
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
|
||||
set local timezone to 'UTC';
|
||||
select public.grant_catalog_right('d0000000-0000-0000-0000-0000000000b1', 'catalog_reader', 'tz', null, '2027-03-28T23:59:59.999+02:00');
|
||||
select public.grant_catalog_right('d0000000-0000-0000-0000-0000000000b2', 'catalog_reader', 'tz', null, '2027-10-31T23:59:59.999+01:00');
|
||||
set local timezone to 'America/New_York';
|
||||
select public.grant_catalog_right('d0000000-0000-0000-0000-0000000000b3', 'catalog_reader', 'tz', null, '2027-03-28T23:59:59.999+02:00');
|
||||
select public.grant_catalog_right('d0000000-0000-0000-0000-0000000000b4', 'catalog_reader', 'tz', null, '2027-10-31T23:59:59.999+01:00');
|
||||
reset role;
|
||||
select is((select string_agg(to_char(expires_at at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS'), ',' order by user_id)
|
||||
from public.anatomical_catalog_grants where user_id in (
|
||||
'd0000000-0000-0000-0000-0000000000b1', 'd0000000-0000-0000-0000-0000000000b2')),
|
||||
'2027-03-28T21:59:59.999,2027-10-31T22:59:59.999', 'the 2027 DST days store the same instants under UTC');
|
||||
select is((select string_agg(to_char(expires_at at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS'), ',' order by user_id)
|
||||
from public.anatomical_catalog_grants where user_id in (
|
||||
'd0000000-0000-0000-0000-0000000000b3', 'd0000000-0000-0000-0000-0000000000b4')),
|
||||
'2027-03-28T21:59:59.999,2027-10-31T22:59:59.999', 'the 2027 DST days store the same instants under America/New_York');
|
||||
select is((select count(*)::int from public.anatomical_catalog_grants where user_id in (
|
||||
'd0000000-0000-0000-0000-0000000000b1', 'd0000000-0000-0000-0000-0000000000b2')
|
||||
and expires_at in ('2027-03-28T21:59:59.999Z'::timestamptz, '2027-10-31T22:59:59.999Z'::timestamptz)), 2,
|
||||
'the stored instants equal the Paris end of day (summer +02:00, winter +01:00)');
|
||||
set local timezone to 'UTC';
|
||||
-- The list state at each boundary (direct inserts with an earlier granted_at).
|
||||
insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, granted_by, granted_at, expires_at) values
|
||||
('d0000000-0000-0000-0000-0000000000b5', 'catalog_reader', 'x', 'd0000000-0000-0000-0000-000000000001', now() - interval '2 days', now() - interval '1 minute'),
|
||||
('d0000000-0000-0000-0000-0000000000b6', 'catalog_reader', 'x', 'd0000000-0000-0000-0000-000000000001', now() - interval '2 days', now() + interval '1 minute'),
|
||||
('d0000000-0000-0000-0000-0000000000b7', 'catalog_reader', 'x', 'd0000000-0000-0000-0000-000000000001', now() - interval '2 days', now() + interval '30 days 1 minute');
|
||||
set local role authenticated;
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
|
||||
create function pg_temp.grant_state(p_user uuid) returns text language sql stable as $$
|
||||
select (e ->> 'state') || '/' || (e ->> 'effective')
|
||||
from jsonb_array_elements(public.list_catalog_grants()) e where (e ->> 'user_id')::uuid = p_user;
|
||||
$$;
|
||||
grant execute on function pg_temp.grant_state(uuid) to authenticated;
|
||||
select is(pg_temp.grant_state('d0000000-0000-0000-0000-0000000000b5'), 'expired/false', 'expiry 1 minute ago: expired, not effective');
|
||||
select is(pg_temp.grant_state('d0000000-0000-0000-0000-0000000000b6'), 'expiring/true', 'expiry in 1 minute: expiring, effective');
|
||||
select is(pg_temp.grant_state('d0000000-0000-0000-0000-0000000000b7'), 'active/true', 'expiry in 30 days and 1 minute: active, effective');
|
||||
select is(pg_temp.grant_state('d0000000-0000-0000-0000-0000000000b3') like 'expiring/%' or pg_temp.grant_state('d0000000-0000-0000-0000-0000000000b3') like 'active/%', true, 'a future DST-day grant is listed live');
|
||||
|
||||
-- only the owner (aal2) lists grants
|
||||
select ok((select jsonb_typeof(public.list_catalog_grants()) = 'array'), 'owner lists grants');
|
||||
select pg_temp.act('d0000000-0000-0000-0000-00000000000a', 'd1000000-0000-0000-0000-00000000000a', 'aal2');
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
-- unknown, deleted, personal or forbidden sheet gets one answer. Fixtures are
|
||||
-- scoped to the ids created here.
|
||||
begin;
|
||||
select plan(43);
|
||||
select plan(60);
|
||||
|
||||
-- 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
|
||||
@@ -193,5 +193,37 @@ select is((select string_agg(p.provolatile::text, ',' order by p.proname) from p
|
||||
where n.nspname = 'public' and p.proname in ('list_official_sheets_admin', 'get_official_sheet_admin')), 's,s',
|
||||
'both reads are stable: no write path');
|
||||
|
||||
-- ============ Species order range (owner 2026-11-02) ============
|
||||
-- The stored order of an inactive species, read with the owner's rights (RLS hides inactive rows).
|
||||
create function pg_temp.stored_order(p_id text) returns text language plpgsql security definer as $$
|
||||
begin
|
||||
return (select sort_order::text from public.species where id = p_id);
|
||||
end $$;
|
||||
grant execute on function pg_temp.stored_order(text) to authenticated;
|
||||
select pg_temp.as_user('E', 'aal2');
|
||||
select is(pg_temp.val($$ select public.create_species('m22-neg', 'Négatif', 'Negative', -1) $$), 'P0001 invalid_input', 'create_species: -1 refused');
|
||||
select is(pg_temp.val($$ select public.create_species('m22-big', 'Grand', 'Big', 10000) $$), 'P0001 invalid_input', 'create_species: 10 000 refused');
|
||||
select is(pg_temp.val($$ select public.create_species('m22-zero', 'Zéro', 'Zero', 0) $$), 'm22-zero', 'create_species: 0 accepted');
|
||||
select is(pg_temp.val($$ select public.create_species('m22-max', 'Max', 'Max', 9999) $$), 'm22-max', 'create_species: 9 999 accepted');
|
||||
select is(pg_temp.val($$ select public.create_species('m22-null', 'Nul', 'Null', null) $$), 'm22-null', 'create_species: null accepted');
|
||||
select is(coalesce(pg_temp.stored_order('m22-neg'), 'none') || '/' || coalesce(pg_temp.stored_order('m22-big'), 'none'), 'none/none', 'create_species: refused orders store nothing');
|
||||
select is(pg_temp.stored_order('m22-zero') || ',' || pg_temp.stored_order('m22-max') || ',' || pg_temp.stored_order('m22-null'),
|
||||
'0,9999,100', 'create_species: stored orders, null keeps the default 100');
|
||||
select is(pg_temp.val($$ select public.update_inactive_species('m22-zero', 'Zéro', 'Zero', -1) $$), 'P0001 invalid_input', 'update_inactive_species: -1 refused');
|
||||
select is(pg_temp.val($$ select public.update_inactive_species('m22-zero', 'Zéro', 'Zero', 10000) $$), 'P0001 invalid_input', 'update_inactive_species: 10 000 refused');
|
||||
select is(pg_temp.val($$ select public.update_inactive_species('m22-zero', 'Zéro', 'Zero', 9999) $$), 'm22-zero', 'update_inactive_species: 9 999 accepted');
|
||||
select is(pg_temp.stored_order('m22-zero'), '9999', 'update_inactive_species: 9 999 stored');
|
||||
select is(pg_temp.val($$ select public.update_inactive_species('m22-zero', 'Zéro', 'Zero', 0) $$), 'm22-zero', 'update_inactive_species: 0 accepted');
|
||||
select is(pg_temp.stored_order('m22-zero'), '0', 'update_inactive_species: 0 stored');
|
||||
select is(pg_temp.val($$ select public.update_inactive_species('m22-max', 'Max', 'Max', null) $$), 'm22-max', 'update_inactive_species: null accepted');
|
||||
select is(pg_temp.stored_order('m22-max'), '9999', 'update_inactive_species: null keeps the stored order');
|
||||
select pg_temp.as_user('V', 'aal2');
|
||||
select is(pg_temp.val($$ select public.create_species('m22-rev', 'Rev', 'Rev', 5) $$), '42501 rights.capability', 'create_species: a reviewer is still refused');
|
||||
reset role;
|
||||
select is((select string_agg(p.prosecdef::text || '/' || (p.proconfig = array['search_path=""'])::text, ',' order by p.proname) from pg_proc p
|
||||
join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname in ('create_species', 'update_inactive_species')),
|
||||
'true/true,true/true', 'both species RPCs: security definer, empty search_path');
|
||||
|
||||
select * from finish();
|
||||
rollback;
|
||||
Reference in new issue
Block a user