Files
Convivo/supabase/migrations/20261021180000_material_time_lock.sql
a8f3f5b8e1 feat(sessions): material downloads unlock when the session ends
Private materials bucket + RLS read gate (admins/own speakers anytime, all
attendees after the scheduled slot end, event-local Amsterdam time); always
visible uploaded-state, locked download panel, authenticated client downloads,
admin signed URLs. 12 new assertions in material_time_lock_tests.sql.

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
2026-10-21 18:57:15 +02:00

67 lines
3.2 KiB
PL/PgSQL

-- =============================================================================
-- Session materials unlock at the scheduled session end.
--
-- Product rule: once a speaker uploads material, attendees must ALWAYS see
-- that material exists — but the download stays locked until the session's
-- slot has ended, so slides are not a distraction mid-session. After the
-- scheduled end everyone (any signed-in attendee, attendance not required —
-- purely time-based) can download. The session's own speakers and admins keep
-- access at any time.
--
-- Enforcement is at the STORAGE layer, not the UI: the `materials` bucket
-- becomes private (the /object/public/ endpoint stops serving files) and the
-- read policy only releases an object to admins, the owning speakers, or after
-- the slot end. Knowing the exact path/URL no longer yields the bytes before
-- the session ends.
-- =============================================================================
-- Unlock = the session's slot has ended, on event-local wall time
-- (APPE events run in Amsterdam; slot_date + end_time are event-local).
create or replace function public.session_material_unlocked(p_session_id uuid)
returns boolean
language sql stable security definer set search_path = '' as $$
select exists (
select 1
from public.sessions s
join public.slots sl on sl.id = s.slot_id
where s.id = p_session_id
and (sl.slot_date + sl.end_time) <= (timezone('Europe/Amsterdam', now()))
);
$$;
-- Storage read gate for the private `materials` bucket. Object paths are
-- `sessions/<session-uuid>/<file>` (see uploadSessionMaterial); anything else
-- in that bucket, and every other bucket, is denied here (other buckets have
-- their own policies).
create or replace function public.can_read_session_material(p_bucket text, p_name text)
returns boolean
language plpgsql stable security definer set search_path = '' as $$
declare
v_session uuid;
begin
if p_bucket is distinct from 'materials' then return false; end if;
if public.is_admin() then return true; end if;
if p_name ~ '^sessions/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/[^/]+$' then
v_session := split_part(p_name, '/', 2)::uuid;
if public.owns_speaker_session(v_session) then return true; end if;
return public.session_material_unlocked(v_session);
end if;
return false;
end;
$$;
grant execute on function public.session_material_unlocked(uuid) to authenticated;
grant execute on function public.can_read_session_material(text, text) to authenticated;
-- Private bucket: kills every public (unauthenticated) material URL.
update storage.buckets set public = false where id = 'materials';
-- Replace the blanket read policy with the time/ownership gate above. The
-- Storage API evaluates this as the requesting user, so it applies both to
-- /object/materials/... fetches and to createSignedUrl issuance.
drop policy if exists "materials_read" on storage.objects;
create policy "materials_read" on storage.objects for select to authenticated
using (public.can_read_session_material(bucket_id, name));
comment on column public.sessions.material_path is
'Storage object path in the private materials bucket; downloads unlock at the scheduled session end (storage RLS), speakers/admins bypass';