-- Bobinoo — double authentification (28 nov. 2026). Idempotent. -- Un compte qui a activé la double authentification n'accède à ses données, à l'administration -- et aux actions sociales qu'avec une session validée par le code (niveau « aal2 »). -- Les comptes sans double authentification ne voient aucune différence. create or replace function public.azuu_mfa_ok() returns boolean language sql stable security definer set search_path = '' as $$ select coalesce(auth.jwt()->>'aal', '') = 'aal2' or not exists (select 1 from auth.mfa_factors f where f.user_id = auth.uid() and f.status = 'verified') $$; revoke execute on function public.azuu_mfa_ok() from public, anon; grant execute on function public.azuu_mfa_ok() to authenticated; -- Données du compte (accès direct) : règle restrictive qui s'ajoute aux règles existantes do $$ declare t text; begin foreach t in array array['items', 'lists', 'settings', 'follows', 'profiles', 'activity'] loop execute format('drop policy if exists "mfa required" on public.%I', t); execute format('create policy "mfa required" on public.%I as restrictive for all to authenticated using ((select public.azuu_mfa_ok())) with check ((select public.azuu_mfa_ok()))', t); end loop; end $$; -- Administration et actions sociales (listes partagées, abonnements, réactions…) create or replace function public.is_admin() returns boolean language sql stable security definer set search_path = '' as $$ select exists (select 1 from public.admins a where a.user_id = auth.uid()) and public.azuu_mfa_ok() $$; create or replace function public.azuu_me_active() returns boolean language sql stable security definer set search_path = '' as $$ select auth.uid() is not null and exists (select 1 from public.profiles p where p.id = auth.uid() and not p.suspended) and public.azuu_mfa_ok() $$;