-- Super-Admin user management (organized-ideas.md §6): "CRUD user accounts" -- starts with being able to see them at all. auth.users isn't exposed -- through PostgREST (by design — Supabase doesn't expose the `auth` schema -- to the API), so this read-only, Super-Admin-gated function is the only -- way the app can list users alongside their profile/ban status. The -- mutations themselves (ban/unban/delete) go through a new Edge Function -- instead of a SQL function here — those specifically need Supabase Auth's -- Admin API (auth.admin.updateUserById/deleteUser), the stable, documented -- interface for touching auth.users, rather than writing to that schema's -- tables directly (undocumented, not guaranteed stable across upgrades). create or replace function public.list_users_for_admin() returns table(id uuid, username text, email text, role text, banned_until timestamptz, created_at timestamptz) language plpgsql stable security definer set search_path = public as $$ begin if not public.is_super_admin() then raise exception 'insufficient_privilege' using errcode = '42501'; end if; return query select p.id, p.username, u.email::text, p.role, u.banned_until, p.created_at from public.profiles p join auth.users u on u.id = p.id order by p.created_at desc; end; $$;