feat: add magic-link admin login and speaker materials portal

This commit is contained in:
Lago committed 2026-10-07 18:27:56 +02:00
1 parent 3adc0fd58f
commit 8d8d3ae4b8
28 files changed
+1623 -232

No files matched your search

+22 -12
View File
@@ -160,7 +160,17 @@ site_url = "http://127.0.0.1:3000"
# The public URL that Auth serves on. Defaults to the API external URL with `/auth/v1` appended.
# external_url = ""
# A list of *exact* URLs that auth providers are permitted to redirect to post authentication.
additional_redirect_urls = ["https://127.0.0.1:3000"]
additional_redirect_urls = [
"https://127.0.0.1:3000",
"http://localhost:3000",
"http://127.0.0.1:3000",
"http://localhost:3000/speaker",
"http://localhost:3000/speaker/secret",
"http://127.0.0.1:3000/speaker",
"http://127.0.0.1:3000/speaker/secret",
"http://localhost:3001/auth/callback",
"http://127.0.0.1:3001/auth/callback",
]
# How long tokens are valid for, in seconds. Defaults to 3600 (1 hour), maximum 604,800 (1 week).
jwt_expiry = 86400
# JWT issuer URL. If not set, defaults to auth.external_url.
@@ -196,7 +206,7 @@ password_requirements = ""
[auth.rate_limit]
# Number of emails that can be sent per hour. Requires auth.email.smtp to be enabled.
email_sent = 2
email_sent = 100
# Number of SMS messages that can be sent per hour. Requires auth.sms to be enabled.
sms_sent = 30
# Number of anonymous sign-ins that can be made per hour per IP address. Requires enable_anonymous_sign_ins = true.
@@ -227,21 +237,21 @@ enable_confirmations = false
# If enabled, users will need to reauthenticate or have logged in recently to change their password.
secure_password_change = false
# Controls the minimum amount of time that must pass before sending another signup confirmation or password reset email.
max_frequency = "1s"
max_frequency = "60s"
# Number of characters used in the email OTP.
otp_length = 6
# Number of seconds before the email OTP expires (defaults to 1 hour).
otp_expiry = 3600
# Use a production-ready SMTP server
# [auth.email.smtp]
# enabled = true
# host = "smtp.sendgrid.net"
# port = 587
# user = "apikey"
# pass = "env(SENDGRID_API_KEY)"
# admin_email = "[email protected]"
# sender_name = "Admin"
# Local Auth explicitly opts into real email delivery; password stays in a private env file.
[auth.email.smtp]
enabled = true
host = "heracles.mxrouting.net"
port = 465
user = "[email protected]"
pass = "env(CONVIVO_SMTP_PASSWORD)"
admin_email = "[email protected]"
sender_name = "Convivo APP"
# Uncomment to customize email template
# [auth.email.template.invite]
Whitespace-only changes.
@@ -0,0 +1,89 @@
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';
@@ -0,0 +1,21 @@
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 in ('speaker', 'staff')
and slot.type = 'session'
);
$$;
revoke all on function public.owns_speaker_session(uuid) from public, anon;
grant execute on function public.owns_speaker_session(uuid) to authenticated, service_role;
notify pgrst, 'reload schema';
+245
View File
@@ -0,0 +1,245 @@
\set ON_ERROR_STOP on
begin;
set local storage.allow_delete_query = 'true';
create function pg_temp.assert_speaker(p_condition boolean, p_name text)
returns void language plpgsql as $$
begin
if p_condition is distinct from true then
raise exception 'FAIL: %', p_name;
end if;
raise notice 'PASS: %', p_name;
end;
$$;
create function pg_temp.expect_speaker_denied(p_statement text, p_name text)
returns void language plpgsql as $$
begin
begin
execute p_statement;
exception when insufficient_privilege then
raise notice 'PASS: %', p_name;
return;
end;
raise exception 'FAIL: % was allowed', p_name;
end;
$$;
create temporary table speaker_fixture as
select kind, gen_random_uuid() as event_id, gen_random_uuid() as session_id,
(select least(coalesce(min(id), 0), 0) from public.slots) - row_number() over ()::integer as slot_id,
case when kind like '%future' then current_date + 365 else current_date - 365 end as event_date
from unnest(array['past', 'future', 'active', 'other', 'service', 'attendee', 'unregistered',
'staff_past', 'staff_future', 'staff_active', 'unassigned_staff', 'wrong_event_staff',
'admin_past', 'admin_future', 'admin_active', 'admin_service']) as kind;
create temporary table speaker_identity as
select 'portal-owner-' || gen_random_uuid() || '@example.invalid' as owner_email,
'portal-other-' || gen_random_uuid() || '@example.invalid' as other_email,
'portal-admin-' || gen_random_uuid() || '@appe.at' as admin_email;
grant select on speaker_fixture, speaker_identity to authenticated;
insert into public.contacts(email, full_name)
select owner_email, 'Portal Owner' from speaker_identity
union all select other_email, 'Other Speaker' from speaker_identity
union all select admin_email, 'Portal Admin' from speaker_identity;
insert into public.events(id, name, start_date, end_date, is_active)
select event_id, 'Portal regression ' || kind, event_date, event_date, false from speaker_fixture;
insert into public.slots(id, event_id, name, type, slot_date, start_time, end_time)
select slot_id, event_id, 'Portal slot',
(case when kind in ('service', 'admin_service') then 'service_session' else 'session' end)::public.slot_type,
event_date, '09:00', '10:00' from speaker_fixture;
insert into public.sessions(id, event_id, slot_id, title)
select session_id, event_id, slot_id, 'Portal talk ' || kind from speaker_fixture;
insert into public.registrations(event_id, contact_email, type, points, checked_in)
select event_id, case when kind in ('other', 'wrong_event_staff') then other_email
when kind like 'admin_%' then admin_email else owner_email end,
(case when kind like 'staff_%' or kind in ('service', 'unassigned_staff', 'wrong_event_staff')
then 'staff' else 'speaker' end)::public.registration_type, 37, false
from speaker_fixture cross join speaker_identity where kind <> 'unregistered';
insert into public.session_speakers(session_id, contact_email)
select session_id, case when kind in ('other', 'unassigned_staff') then other_email
when kind like 'admin_%' then admin_email else owner_email end
from speaker_fixture cross join speaker_identity where kind not in ('service', 'admin_service');
select public._assign_service_session_speakers(session_id, event_id)
from speaker_fixture where kind in ('service', 'admin_service');
update public.registrations set type = 'attendee'
where event_id = (select event_id from speaker_fixture where kind = 'attendee');
update public.events set is_active = false where is_active;
update public.events set is_active = true
where id = (select event_id from speaker_fixture where kind = 'active');
create temporary table speaker_registration_before as
select registration.* from public.registrations registration join speaker_fixture using (event_id);
create temporary table speaker_points_before as select * from public.points_log;
create temporary table speaker_checkins_before as select * from public.checkins;
create temporary table speaker_leaderboard_before as select * from public.leaderboard;
select pg_temp.assert_speaker(not has_function_privilege('anon', 'public.get_my_speaker_sessions()', 'execute'), 'anonymous portal RPC revoked');
select pg_temp.assert_speaker(not has_function_privilege('anon', 'public.set_session_material(uuid,text)', 'execute'), 'anonymous material RPC revoked');
select set_config('request.jwt.claims', json_build_object('email', owner_email, 'role', 'authenticated')::text, true)
from speaker_identity;
set local role authenticated;
select pg_temp.assert_speaker(
(select count(*) = 6 from public.get_my_speaker_sessions()),
'only own registered non-service talks across active, past and future events');
select pg_temp.assert_speaker(
(select count(*) = 5 from public.get_my_speaker_sessions() where not is_active),
'inactive events remain accessible without physical check-in');
select pg_temp.assert_speaker(not public.is_admin(), 'staff material restrictions tested without Admin privileges');
reset role;
update public.events set is_active = false where is_active;
update public.events set is_active = true
where id = (select event_id from speaker_fixture where kind = 'staff_active');
set local role authenticated;
select pg_temp.assert_speaker(
(select count(*) = 1 from public.get_my_speaker_sessions() where is_active),
'assigned non-admin staff can access an active-event talk without check-in');
select pg_temp.assert_speaker(
not public.can_write_session_material(path), 'reject malformed path: ' || coalesce(path, 'NULL'))
from unnest(array[null, '', 'sessions/not-a-uuid/slides.pdf', 'foreign/slides.pdf',
'sessions/' || (select session_id from speaker_fixture where kind = 'past') || '/',
'sessions/' || (select session_id from speaker_fixture where kind = 'past') || '/nested/slides.pdf']) as path;
select pg_temp.expect_speaker_denied(
format('insert into storage.objects(bucket_id,name) values (''materials'',%L)', path),
'malformed storage insert denied: ' || path)
from unnest(array['sessions/not-a-uuid/slides.pdf', 'foreign/slides.pdf',
'sessions/' || (select session_id from speaker_fixture where kind = 'past') || '/nested/slides.pdf']) as path;
do $$
declare
fixture record;
material text;
affected integer;
begin
for fixture in select * from speaker_fixture where kind in ('past', 'future', 'active', 'staff_past', 'staff_future', 'staff_active') loop
material := 'sessions/' || fixture.session_id || '/slides.pdf';
perform pg_temp.assert_speaker(public.owns_speaker_session(fixture.session_id), 'ownership: ' || fixture.kind);
perform pg_temp.expect_speaker_denied(
format('select public.set_session_material(%L::uuid, %L)', fixture.session_id, material),
'nonexistent object cannot be attached: ' || fixture.kind);
insert into storage.objects(bucket_id, name, metadata)
values ('materials', material, '{"version":1}');
perform public.set_session_material(fixture.session_id, material);
perform pg_temp.assert_speaker(
(select material_path = material from public.sessions where id = fixture.session_id),
'upload attached: ' || fixture.kind);
update storage.objects set metadata = '{"version":2}' where bucket_id = 'materials' and name = material;
get diagnostics affected = row_count;
perform pg_temp.assert_speaker(affected = 1, 'storage replacement allowed: ' || fixture.kind);
insert into storage.objects(bucket_id, name) values ('materials', replace(material, 'slides.pdf', 'replacement.pdf'));
perform public.set_session_material(fixture.session_id, replace(material, 'slides.pdf', 'replacement.pdf'));
perform pg_temp.assert_speaker(
(select material_path = replace(material, 'slides.pdf', 'replacement.pdf') from public.sessions where id = fixture.session_id),
'replacement attached: ' || fixture.kind);
perform pg_temp.expect_speaker_denied(
format('update storage.objects set name = %L where bucket_id = ''materials'' and name = %L',
'sessions/' || (select session_id from speaker_fixture where kind = 'other') || '/stolen.pdf', material),
'UPDATE WITH CHECK prevents reassignment: ' || fixture.kind);
perform public.set_session_material(fixture.session_id, null);
delete from storage.objects where bucket_id = 'materials'
and name in (material, replace(material, 'slides.pdf', 'replacement.pdf'));
get diagnostics affected = row_count;
perform pg_temp.assert_speaker(affected = 2, 'storage delete allowed: ' || fixture.kind);
end loop;
for fixture in select * from speaker_fixture where kind in ('other', 'service', 'attendee', 'unregistered', 'unassigned_staff', 'wrong_event_staff', 'admin_past') loop
material := 'sessions/' || fixture.session_id || '/slides.pdf';
perform pg_temp.assert_speaker(not public.owns_speaker_session(fixture.session_id), 'ownership denied: ' || fixture.kind);
perform pg_temp.assert_speaker(not public.can_write_session_material(material), 'storage ownership denied: ' || fixture.kind);
perform pg_temp.expect_speaker_denied(
format('insert into storage.objects(bucket_id,name) values (''materials'',%L)', material),
'storage insert denied: ' || fixture.kind);
perform pg_temp.expect_speaker_denied(
format('select public.set_session_material(%L::uuid,null)', fixture.session_id),
'material detach denied: ' || fixture.kind);
end loop;
perform pg_temp.expect_speaker_denied(
format('select public.set_session_material(%L::uuid,%L)',
(select session_id from speaker_fixture where kind = 'past'),
'sessions/' || (select session_id from speaker_fixture where kind = 'future') || '/slides.pdf'),
'another owned session path cannot be attached');
end;
$$;
reset role;
insert into storage.objects(bucket_id, name)
select 'materials', 'sessions/' || session_id || '/foreign.pdf'
from speaker_fixture where kind = 'other';
set local role authenticated;
do $$
declare affected integer;
begin
update storage.objects set metadata = '{"changed":true}'
where bucket_id = 'materials' and name = 'sessions/' || (select session_id from speaker_fixture where kind = 'other') || '/foreign.pdf';
get diagnostics affected = row_count;
perform pg_temp.assert_speaker(affected = 0, 'foreign storage update denied');
delete from storage.objects
where bucket_id = 'materials' and name = 'sessions/' || (select session_id from speaker_fixture where kind = 'other') || '/foreign.pdf';
get diagnostics affected = row_count;
perform pg_temp.assert_speaker(affected = 0, 'foreign storage delete denied');
end;
$$;
select set_config('request.jwt.claims', json_build_object('email', other_email, 'role', 'authenticated')::text, true)
from speaker_identity;
select pg_temp.assert_speaker(
(select count(*) = 1 from public.get_my_speaker_sessions()), 'other speaker sees only own assignment');
reset role;
update public.registrations set type = 'attendee'
where event_id = (select event_id from speaker_fixture where kind = 'other');
set local role authenticated;
select pg_temp.assert_speaker(
not exists (select 1 from public.get_my_speaker_sessions()), 'attendee with assignment has empty access');
reset role;
update public.registrations set type = 'speaker'
where event_id = (select event_id from speaker_fixture where kind = 'other');
select set_config('request.jwt.claims', '{"role":"authenticated"}', true);
set local role authenticated;
select pg_temp.assert_speaker(
not exists (select 1 from public.get_my_speaker_sessions()), 'missing identity has empty access');
select pg_temp.expect_speaker_denied(
format('select public.set_session_material(%L::uuid,null)',
(select session_id from speaker_fixture where kind = 'past')), 'missing identity cannot detach material');
reset role;
select set_config('request.jwt.claims', json_build_object('email', admin_email, 'role', 'authenticated')::text, true)
from speaker_identity;
update public.events set is_active = false where is_active;
update public.events set is_active = true
where id = (select event_id from speaker_fixture where kind = 'admin_active');
set local role authenticated;
select pg_temp.assert_speaker(public.is_admin(), 'assigned Admin retains Admin access');
select pg_temp.assert_speaker(
(select count(*) = 3 from public.get_my_speaker_sessions()), 'Admin portal lists only own assigned talks across event dates');
select pg_temp.assert_speaker(
(select count(*) = 1 from public.get_my_speaker_sessions() where is_active),
'assigned Admin staff can access an active-event talk without check-in');
select pg_temp.assert_speaker(
public.owns_speaker_session(session_id), 'Admin staff owns assigned talk: ' || kind)
from speaker_fixture where kind in ('admin_past', 'admin_future', 'admin_active');
select pg_temp.assert_speaker(
not public.owns_speaker_session(session_id), 'Admin portal ownership denied: ' || kind)
from speaker_fixture where kind in ('admin_service', 'other');
select pg_temp.assert_speaker(
public.can_write_session_material('sessions/' || session_id || '/admin.pdf'), 'Admin global material permission preserved')
from speaker_fixture where kind = 'other';
select public.set_session_material(session_id, null)
from speaker_fixture where kind = 'other';
reset role;
select pg_temp.assert_speaker(
not exists (select * from speaker_registration_before except select registration.* from public.registrations registration join speaker_fixture using (event_id))
and not exists (select registration.* from public.registrations registration join speaker_fixture using (event_id) except select * from speaker_registration_before),
'registrations, points, QR tokens and physical check-in unchanged');
select pg_temp.assert_speaker(
not exists (select * from public.points_log except select * from speaker_points_before)
and not exists (select * from speaker_points_before except select * from public.points_log), 'no points-log mutation');
select pg_temp.assert_speaker(
not exists (select * from public.checkins except select * from speaker_checkins_before)
and not exists (select * from speaker_checkins_before except select * from public.checkins), 'no check-in mutation');
select pg_temp.assert_speaker(
not exists (select * from public.leaderboard except select * from speaker_leaderboard_before)
and not exists (select * from speaker_leaderboard_before except select * from public.leaderboard), 'no leaderboard mutation');
rollback;