- is_admin() now returns true when the caller's email is on the @appe.at domain (in addition to the is_admin flag) — covers all admin RLS policies + admin-only RPCs at once. - Backfill contacts.is_admin=true for existing @appe.at rows. - Admin app login gate (AuthContext) mirrors the same rule so a freshly created @appe.at contact can sign in immediately. 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]>
39 lines
1.8 KiB
PL/PgSQL
39 lines
1.8 KiB
PL/PgSQL
-- =============================================================================
|
|
-- Convivo — @appe.at contacts are admins automatically
|
|
--
|
|
-- Business rule: any contact whose email ends with `@appe.at` is an admin,
|
|
-- regardless of the `contacts.is_admin` flag. This is enforced in two places:
|
|
-- 1. `public.is_admin()` — the single source of truth for every admin RLS
|
|
-- policy + admin-only RPC. Re-created to also return true when the
|
|
-- caller's email is on the `@appe.at` domain.
|
|
-- 2. The admin app's login gate (AuthContext) — mirrors the same rule so a
|
|
-- freshly-created `@appe.at` contact can sign in even before the flag is
|
|
-- backfilled.
|
|
--
|
|
-- We also backfill `contacts.is_admin = true` for existing `@appe.at` rows so
|
|
-- the flag stays consistent with the rule (and the admin UI's "admin" badge
|
|
-- reflects reality).
|
|
-- =============================================================================
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 1. is_admin(): domain-aware
|
|
-- A contact is an admin if their `is_admin` flag is set OR their email is on
|
|
-- the @appe.at domain.
|
|
-- -----------------------------------------------------------------------------
|
|
create or replace function public.is_admin() returns boolean
|
|
language sql stable security definer set search_path = public as $$
|
|
select exists (
|
|
select 1 from public.contacts
|
|
where email = auth.jwt() ->> 'email'
|
|
and (is_admin or email like '%@appe.at')
|
|
);
|
|
$$;
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 2. Backfill the flag for existing @appe.at contacts (idempotent).
|
|
-- -----------------------------------------------------------------------------
|
|
update public.contacts
|
|
set is_admin = true
|
|
where email like '%@appe.at'
|
|
and not is_admin;
|