From ee839db75f8a7b5e9a4efb8a53d80ab130614006 Mon Sep 17 00:00:00 2001 From: Jose Lago Date: Wed, 9 Sep 2026 12:58:52 +0200 Subject: [PATCH] Make @appe.at contacts admins automatically MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 Co-authored-by: Amelia Hernandez Co-authored-by: Markus Franz Co-authored-by: Sabrina Hota Co-authored-by: Nilüfer Doğan --- apps/admin/src/context/AuthContext.tsx | 8 +++- .../20260909010000_appe_at_admin_domain.sql | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 supabase/migrations/20260909010000_appe_at_admin_domain.sql diff --git a/apps/admin/src/context/AuthContext.tsx b/apps/admin/src/context/AuthContext.tsx index e77a8f3..6604a8f 100644 --- a/apps/admin/src/context/AuthContext.tsx +++ b/apps/admin/src/context/AuthContext.tsx @@ -12,6 +12,12 @@ interface AuthState { const AuthContext = createContext(null); +// A contact is an admin if their `is_admin` flag is set OR their email is on +// the @appe.at domain (mirrors the DB `is_admin()` rule). +function isAdminContact(c: Contact | null): boolean { + return !!c && (c.is_admin || c.email.endsWith('@appe.at')); +} + // Load the current user's contact row. Returns null if it doesn't exist or the // contact is not an admin (this is an admin-only app). async function loadAdminContact(email: string): Promise { @@ -21,7 +27,7 @@ async function loadAdminContact(email: string): Promise { .eq('email', email) .maybeSingle(); const c = data as Contact | null; - return c && c.is_admin ? c : null; + return isAdminContact(c) ? c : null; } export function AuthProvider({ children }: { children: ReactNode }) { diff --git a/supabase/migrations/20260909010000_appe_at_admin_domain.sql b/supabase/migrations/20260909010000_appe_at_admin_domain.sql new file mode 100644 index 0000000..8cc4f29 --- /dev/null +++ b/supabase/migrations/20260909010000_appe_at_admin_domain.sql @@ -0,0 +1,38 @@ +-- ============================================================================= +-- 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;