create or replace function public.owns_speaker_session(p_session_id uuid) returns boolean language sql stable security definer set search_path = '' as $$ select public.current_email() is not null and exists ( select 1 from public.session_speakers assignment join public.sessions talk on talk.id = assignment.session_id join public.slots slot on slot.id = talk.slot_id and slot.event_id = talk.event_id join public.registrations registration on registration.event_id = talk.event_id and registration.contact_email = assignment.contact_email where assignment.session_id = p_session_id and assignment.contact_email = public.current_email() and registration.type = 'speaker' and slot.type = 'session' ); $$; create or replace function public.can_write_session_material(p_path text) returns boolean language plpgsql stable security definer set search_path = '' as $$ begin if public.current_email() is null then return false; end if; if public.is_admin() then return true; end if; if p_path is null or p_path !~ '^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 return false; end if; return public.owns_speaker_session(split_part(p_path, '/', 2)::uuid); end; $$; create or replace function public.get_my_speaker_sessions() returns table ( session_id uuid, title text, description text, room text, material_path text, event_id uuid, event_name text, event_start_date date, event_end_date date, is_active boolean, slot_date date, start_time time, end_time time ) language sql stable security definer set search_path = '' as $$ select talk.id, talk.title, talk.description, talk.room, talk.material_path, event.id, event.name, event.start_date, event.end_date, event.is_active, slot.slot_date, slot.start_time, slot.end_time from public.sessions talk join public.events event on event.id = talk.event_id join public.slots slot on slot.id = talk.slot_id where public.owns_speaker_session(talk.id) order by slot.slot_date, slot.start_time, event.name, talk.title, talk.id; $$; create or replace function public.set_session_material(p_session_id uuid, p_material_path text) returns public.sessions language plpgsql security definer set search_path = '' as $$ declare result public.sessions; begin if public.current_email() is null then raise exception 'not authenticated' using errcode = '42501'; end if; select * into result from public.sessions where id = p_session_id for update; if not found then raise exception 'session not found'; end if; if not (public.is_admin() or public.owns_speaker_session(p_session_id)) then raise exception 'not a registered speaker of this session' using errcode = '42501'; end if; if p_material_path is not null and not public.is_admin() then if not public.can_write_session_material(p_material_path) or split_part(p_material_path, '/', 2) <> p_session_id::text or not exists (select 1 from storage.objects where bucket_id = 'materials' and name = p_material_path) then raise exception 'material must be an uploaded object for this session' using errcode = '42501'; end if; end if; update public.sessions set material_path = p_material_path where id = p_session_id returning * into result; return result; end; $$; drop policy if exists "materials_write" on storage.objects; drop policy if exists "materials_update" on storage.objects; drop policy if exists "materials_delete" on storage.objects; create policy "materials_write" on storage.objects for insert to authenticated with check (bucket_id = 'materials' and public.can_write_session_material(name)); create policy "materials_update" on storage.objects for update to authenticated using (bucket_id = 'materials' and public.can_write_session_material(name)) with check (bucket_id = 'materials' and public.can_write_session_material(name)); create policy "materials_delete" on storage.objects for delete to authenticated using (bucket_id = 'materials' and public.can_write_session_material(name)); revoke all on function public.owns_speaker_session(uuid), public.can_write_session_material(text), public.get_my_speaker_sessions(), public.set_session_material(uuid, text) from public, anon; grant execute on function public.owns_speaker_session(uuid), public.can_write_session_material(text), public.get_my_speaker_sessions(), public.set_session_material(uuid, text) to authenticated, service_role; notify pgrst, 'reload schema';