-- ============================================================================= -- Convivo — Phase 2 (Admin app): admin read access + QR reset RPC -- -- The admin app (apps/admin) needs to: -- 1. Read ALL profiles, preferences, connections, feedback, checkins, -- points_log (a full event overview), not just the admin's own rows. -- 2. Reset an attendee's QR token (which doubles as their auth password), -- forcing them to re-login with the new lanyard code. -- -- This migration adds the missing admin SELECT policies and a single -- admin-only RPC. It does NOT change any existing policy. -- ============================================================================= -- ----------------------------------------------------------------------------- -- ADMIN READ POLICIES -- (profiles, checkins, points_log, leaderboard already allow admin/all reads.) -- ----------------------------------------------------------------------------- -- preferences: admin can read everyone's picks create policy "prefs_select_admin" on public.preferences for select to authenticated using (public.is_admin()); -- connections: admin can read all connections (event overview) create policy "conn_select_admin" on public.connections for select to authenticated using (public.is_admin()); -- feedback: admin can read all feedback create policy "feedback_select_admin" on public.feedback for select to authenticated using (public.is_admin()); -- ----------------------------------------------------------------------------- -- reset_qr_token(p_profile_id) — admin only -- -- Regenerates the attendee's QR token, syncs it into auth.users as the new -- bcrypt password (the QR token IS the login password), and clears their -- existing sessions so they must re-login with the new code. -- -- security definer: runs as the table owner (postgres), which has write access -- to both public.profiles and auth.* (verified). -- ----------------------------------------------------------------------------- create or replace function public.reset_qr_token(p_profile_id uuid) returns table (profile_id uuid, qr_token text) language plpgsql security definer set search_path = public as $$ declare v_uid uuid := auth.uid(); v_token text; v_salt text; begin if v_uid is null then raise exception 'not authenticated'; end if; if not public.is_admin() then raise exception 'admin only'; end if; if not exists (select 1 from public.profiles where id = p_profile_id) then raise exception 'profile not found'; end if; -- Generate a fresh, human-friendly token: CVV-XXX-NNNN v_token := 'CVV-' || upper(substr(md5(random()::text), 1, 3)) || '-' || lpad((floor(random() * 10000))::int::text, 4, '0'); -- Update the profile (source of truth for the lanyard). update public.profiles set qr_token = v_token where id = p_profile_id; -- Sync the auth password so the new token works for login. -- (pgcrypto's gen_salt/crypt live in the `extensions` schema — qualify them, -- since this function's search_path is pinned to `public`.) v_salt := extensions.gen_salt('bf'); update auth.users set encrypted_password = extensions.crypt(v_token, v_salt), updated_at = now() where id = p_profile_id; -- Force re-login: drop the user's existing sessions. delete from auth.sessions where user_id = p_profile_id; return query select p_profile_id, v_token; end; $$; grant execute on function public.reset_qr_token(uuid) to authenticated;