Console : nommer ou retirer un admin depuis l'onglet Membres
deploy / deploy (push) Successful in 1m6s
deploy / deploy (push) Successful in 1m6s
- Boutons « Nommer admin » / « Retirer admin » (motif obligatoire, journal, alerte e-mail). - Garde-fous côté base (admin.set_admin, migration 20261130170000 appliquée) : double authentification active exigée, pas de compte suspendu, jamais sur son propre compte (il reste toujours un admin). - Fonction Edge « admin » : action set_admin, messages d'erreur explicites. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
This commit is contained in:
5 files changed
+72
-8
No files matched your search
@@ -162,6 +162,14 @@ Deno.serve(async (req) => {
|
||||
if (r?.ok) alert(body.value ? 'Compte suspendu' : 'Compte réactivé', [`Par ${user.email}.`, `Compte : ${target}`, `Motif : ${reason}`])
|
||||
return json(200, r)
|
||||
}
|
||||
case 'set_admin': {
|
||||
const target = str(body.user, 36)
|
||||
if (!UUID.test(target) || typeof body.value !== 'boolean') return json(400, { error: 'paramètres' })
|
||||
const reason = reasonOf(body.reason)
|
||||
const [{ r }] = await sql`select admin.set_admin(${user.id}::uuid, ${target}::uuid, ${body.value}::boolean, ${reason}::text) as r`
|
||||
if (r?.ok) alert(body.value ? 'Nouvel admin' : 'Admin retiré', [`Par ${user.email}.`, `Compte : ${target}`, `Motif : ${reason}`])
|
||||
return json(200, r)
|
||||
}
|
||||
case 'reviews': {
|
||||
const [{ r }] = await sql`select admin.reviews(${str(body.q, 60)}::text, ${int(body.offset)}::int) as r`
|
||||
return json(200, r)
|
||||
@@ -197,7 +205,8 @@ Deno.serve(async (req) => {
|
||||
} catch (e) {
|
||||
const msg = (e as Error)?.message ?? 'erreur'
|
||||
// Messages volontaires (motif manquant, action sur son propre compte) renvoyés tels quels, le reste masqué
|
||||
if (/motif requis|impossible sur son propre compte/.test(msg)) return json(400, { error: msg.replace(/^.*?(motif requis.*|impossible sur son propre compte).*$/, '$1') })
|
||||
const known = msg.match(/motif requis \(3 caractères minimum\)|impossible sur son propre compte|double authentification requise|compte suspendu/)
|
||||
if (known) return json(400, { error: known[0] })
|
||||
console.error('admin', action, msg)
|
||||
return json(500, { error: 'erreur serveur' })
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
-- Console : nommer ou retirer un admin (30/11/2026). Idempotent.
|
||||
-- Garde-fous : double authentification active exigée, pas de compte suspendu, jamais sur son propre compte
|
||||
-- (il reste donc toujours au moins un admin). Inscrit au journal.
|
||||
create or replace function admin.set_admin(p_admin uuid, p_user uuid, p_value boolean, p_reason text)
|
||||
returns jsonb language plpgsql set search_path = '' as $$
|
||||
begin
|
||||
if p_user = p_admin then raise exception 'impossible sur son propre compte'; end if;
|
||||
if p_value then
|
||||
if not exists (select 1 from auth.mfa_factors f where f.user_id = p_user and f.status = 'verified') then
|
||||
raise exception 'double authentification requise';
|
||||
end if;
|
||||
if exists (select 1 from public.profiles p where p.id = p_user and p.suspended) then
|
||||
raise exception 'compte suspendu';
|
||||
end if;
|
||||
insert into admin.admins (user_id) values (p_user) on conflict do nothing;
|
||||
else
|
||||
delete from admin.admins where user_id = p_user;
|
||||
end if;
|
||||
if not found then return jsonb_build_object('ok', false); end if;
|
||||
perform admin.log(p_admin, case when p_value then 'nommer admin' else 'retirer admin' end, p_user,
|
||||
(select username from public.profiles where id = p_user), jsonb_build_object('motif', left(p_reason, 500)));
|
||||
return jsonb_build_object('ok', true);
|
||||
end;
|
||||
$$;
|
||||
revoke all on function admin.set_admin(uuid, uuid, boolean, text) from public, anon, authenticated, service_role;
|
||||
comment on function admin.set_admin(uuid, uuid, boolean, text) is 'Nommer ou retirer un admin (double authentification exigée, jamais sur son propre compte). Inscrit au journal.';
|
||||
Reference in new issue
Block a user