-- ============================================================================= -- Convivo — Default admin password for @appe.at contacts -- -- Business rule: any @appe.at contact (admin) logs in with the default admin -- password `appe-admin-2026` out of the box, without manual setup. A custom -- password set on the contact row is always respected (never overwritten). -- -- Implementation: -- 1. The @appe.at BEFORE trigger now also sets contacts.password to the -- default when it's null (respects an explicitly-set custom password). -- 2. The auth-user-creation helpers use the contact's password (when set) -- instead of a placeholder, so a freshly-created @appe.at admin can log in -- immediately (the AFTER UPDATE OF password trigger only fires on UPDATE, -- not INSERT, so the creation path must set the hash itself). -- 3. The Pretix auth helper uses the contact's password (or the default) for -- @appe.at emails — admins log in with the default, not a QR token. -- 4. One-time reconciliation: existing @appe.at contacts with a null password -- get the default, and it's synced into auth.users. -- ============================================================================= -- ----------------------------------------------------------------------------- -- 1. @appe.at BEFORE trigger: default admin password when null -- ----------------------------------------------------------------------------- create or replace function public._set_appe_at_admin() returns trigger language plpgsql security definer set search_path = public as $$ begin if new.email like '%@appe.at' then new.is_admin := true; if new.password is null then new.password := 'appe-admin-2026'; end if; end if; return new; end; $$; -- Re-attach the (existing) trigger to the updated function. drop trigger if exists trg_contacts_appe_at_admin on public.contacts; create trigger trg_contacts_appe_at_admin before insert or update on public.contacts for each row execute function public._set_appe_at_admin(); -- ----------------------------------------------------------------------------- -- 2. upsert_contact: use the contact's password when creating the auth user -- ----------------------------------------------------------------------------- create or replace function public.upsert_contact( p_email text, p_full_name text, p_linkedin_url text, p_is_admin boolean ) returns public.contacts language plpgsql security definer set search_path = public as $$ declare v_uid uuid; v_contact public.contacts; begin if auth.uid() is null then raise exception 'not authenticated'; end if; if not public.is_admin() then raise exception 'admin only'; end if; if p_email is null or btrim(p_email) = '' then raise exception 'email is required'; end if; if p_full_name is null or btrim(p_full_name) = '' then raise exception 'full_name is required'; end if; insert into public.contacts (email, full_name, linkedin_url, is_admin) values (p_email, p_full_name, p_linkedin_url, coalesce(p_is_admin, false)) on conflict (email) do update set full_name = excluded.full_name, linkedin_url = excluded.linkedin_url, is_admin = excluded.is_admin returning * into v_contact; -- Ensure an auth user exists so the contact can log in. If no auth user yet, -- create one. Use the contact's password when set (e.g. the @appe.at default -- admin password); otherwise a placeholder that a registration token replaces. select id into v_uid from auth.users where email = p_email; if v_uid is null then insert into auth.users (instance_id, id, aud, role, email, encrypted_password, email_confirmed_at, confirmation_token, recovery_token, email_change_token_new, email_change, raw_app_meta_data, raw_user_meta_data, created_at, updated_at, confirmation_sent_at, last_sign_in_at) values ('00000000-0000-0000-0000-000000000000', gen_random_uuid(), 'authenticated', 'authenticated', p_email, extensions.crypt(coalesce(v_contact.password, 'no-token-yet'), extensions.gen_salt('bf')), now(), '', '', '', '', '{"provider":"email","providers":["email"]}', jsonb_build_object('full_name', p_full_name), now(), now(), now(), now()) returning id into v_uid; insert into auth.identities (id, provider_id, identity_data, provider, user_id, created_at, updated_at, last_sign_in_at) values (v_uid, v_uid::text, jsonb_build_object('sub', v_uid::text, 'email', p_email), 'email', v_uid, now(), now(), now()); end if; return v_contact; end; $$; -- ----------------------------------------------------------------------------- -- 3. _ensure_auth_user_exists (sidecar): use the contact's password -- ----------------------------------------------------------------------------- create or replace function public._ensure_auth_user_exists(p_email text, p_full_name text) returns void language plpgsql security definer set search_path = public as $$ declare v_uid uuid; v_pw text; begin select id into v_uid from auth.users where email = p_email; if v_uid is not null then return; -- already exists; leave the password alone end if; select password into v_pw from public.contacts where email = p_email; insert into auth.users (instance_id, id, aud, role, email, encrypted_password, email_confirmed_at, confirmation_token, recovery_token, email_change_token_new, email_change, raw_app_meta_data, raw_user_meta_data, created_at, updated_at, confirmation_sent_at, last_sign_in_at) values ('00000000-0000-0000-0000-000000000000', gen_random_uuid(), 'authenticated', 'authenticated', p_email, extensions.crypt(coalesce(v_pw, 'no-token-yet'), extensions.gen_salt('bf')), now(), '', '', '', '', '{"provider":"email","providers":["email"]}', jsonb_build_object('full_name', p_full_name), now(), now(), now(), now()) returning id into v_uid; insert into auth.identities (id, provider_id, identity_data, provider, user_id, created_at, updated_at, last_sign_in_at) values (v_uid, v_uid::text, jsonb_build_object('sub', v_uid::text, 'email', p_email), 'email', v_uid, now(), now(), now()); end; $$; -- ----------------------------------------------------------------------------- -- 4. _ensure_auth_user (pretix): @appe.at admins use the default password -- ----------------------------------------------------------------------------- create or replace function public._ensure_auth_user(p_email text, p_full_name text, p_qr_token text) returns void language plpgsql security definer set search_path = public as $$ declare v_uid uuid; v_pw text; begin select password into v_pw from public.contacts where email = p_email; if p_email like '%@appe.at' then v_pw := coalesce(v_pw, 'appe-admin-2026'); -- respect a custom password, else default else if p_qr_token is null or btrim(p_qr_token) = '' then return; -- no token → no login (e.g. an external speaker) end if; v_pw := p_qr_token; end if; select id into v_uid from auth.users where email = p_email; if v_uid is null then insert into auth.users (instance_id, id, aud, role, email, encrypted_password, email_confirmed_at, confirmation_token, recovery_token, email_change_token_new, email_change, raw_app_meta_data, raw_user_meta_data, created_at, updated_at, confirmation_sent_at, last_sign_in_at) values ('00000000-0000-0000-0000-000000000000', gen_random_uuid(), 'authenticated', 'authenticated', p_email, extensions.crypt(v_pw, extensions.gen_salt('bf')), now(), '', '', '', '', '{"provider":"email","providers":["email"]}', jsonb_build_object('full_name', p_full_name), now(), now(), now(), now()) returning id into v_uid; insert into auth.identities (id, provider_id, identity_data, provider, user_id, created_at, updated_at, last_sign_in_at) values (v_uid, v_uid::text, jsonb_build_object('sub', v_uid::text, 'email', p_email), 'email', v_uid, now(), now(), now()); else update auth.users set encrypted_password = extensions.crypt(v_pw, extensions.gen_salt('bf')), updated_at = now() where id = v_uid; delete from auth.sessions where user_id = v_uid; end if; end; $$; -- ----------------------------------------------------------------------------- -- 5. One-time reconciliation: existing @appe.at contacts with a null password -- get the default (fires the password-sync trigger), and any @appe.at hash -- is re-synced from the contact row so it matches. -- ----------------------------------------------------------------------------- update public.contacts set password = 'appe-admin-2026' where email like '%@appe.at' and password is null; update auth.users u set encrypted_password = extensions.crypt(c.password, extensions.gen_salt('bf')) from public.contacts c where u.email = c.email and c.email like '%@appe.at' and c.password is not null;