61 lines
2.4 KiB
PL/PgSQL
61 lines
2.4 KiB
PL/PgSQL
-- Storage for the SVG assets of anatomical sheets (business architecture
|
|
-- §15, ANATOMICAL_SHEET_MODEL.md §3 "assets").
|
|
--
|
|
-- Bucket anatomical-sheet-assets, private. Objects are content-addressed:
|
|
-- key = <sha256>.svg, so two sheets with the same drawing share one object
|
|
-- and a copy of a sheet never copies bytes. Reading an object is authorised
|
|
-- THROUGH a sheet (an asset row the caller may use or edit), never through
|
|
-- the hash alone; drafts stay visible to editors only, as the sheet rows do.
|
|
-- No API role writes objects: uploads are an operator / server task
|
|
-- (import tool with the service key) until the editor step adds a signed
|
|
-- upload path.
|
|
|
|
insert into storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
|
|
values ('anatomical-sheet-assets', 'anatomical-sheet-assets', false, 20971520, array['image/svg+xml'])
|
|
on conflict (id) do nothing;
|
|
|
|
-- An object is readable when a usable sheet publishes it (a non-draft
|
|
-- version references the asset) or when the caller edits the sheet.
|
|
create or replace function private.can_read_sheet_asset_object(p_user_id uuid, p_object_name text)
|
|
returns boolean
|
|
language sql
|
|
stable
|
|
security definer
|
|
set search_path = ''
|
|
as $$
|
|
select exists (
|
|
select 1
|
|
from public.anatomical_sheet_assets a
|
|
where a.sha256 || '.svg' = p_object_name
|
|
and (
|
|
private.can_edit_sheet(p_user_id, a.sheet_id)
|
|
or (
|
|
private.can_use_sheet(p_user_id, a.sheet_id)
|
|
and exists (
|
|
select 1 from public.anatomical_sheet_versions v
|
|
where v.sheet_id = a.sheet_id
|
|
and v.status <> 'draft'
|
|
and a.id::text = any (private.sheet_document_asset_ids(v.content))
|
|
)
|
|
)
|
|
)
|
|
);
|
|
$$;
|
|
|
|
comment on function private.can_read_sheet_asset_object(uuid, text) is
|
|
'Storage read predicate for bucket anatomical-sheet-assets: object <sha256>.svg referenced by a published version of a usable sheet, or by any version of a sheet the caller edits.';
|
|
|
|
revoke all on function private.can_read_sheet_asset_object(uuid, text) from public;
|
|
grant execute on function private.can_read_sheet_asset_object(uuid, text) to authenticated;
|
|
|
|
create policy anatomical_sheet_assets_read
|
|
on storage.objects
|
|
for select
|
|
to authenticated
|
|
using (
|
|
bucket_id = 'anatomical-sheet-assets'
|
|
and private.can_read_sheet_asset_object((select auth.uid()), name)
|
|
);
|
|
|
|
-- No INSERT / UPDATE / DELETE policy for API roles on this bucket.
|