- schemas private/audit, set_updated_at, append-only audit.audit_logs, private.log_event - profiles created by auth.users trigger; own-row RLS; column-level UPDATE grant - platform_role enum (owner/admin/moderator/support), profile_roles history table, is_platform_* helpers, grant/revoke RPCs audited; writes only via RPC - config.toml (public schema only, strong passwords, no edge runtime), local seed - 79 pgTAP checks: allowed and denied paths for every policy
122 lines
4.5 KiB
PL/PgSQL
122 lines
4.5 KiB
PL/PgSQL
-- Profiles: one row per auth.users row, created by trigger.
|
|
-- Business architecture §4.1 (profiles), §17.3 (own row only).
|
|
-- avatar_asset_id waits for the assets table (files step): added then.
|
|
|
|
create table public.profiles (
|
|
id uuid primary key references auth.users (id) on delete cascade,
|
|
display_name text not null
|
|
constraint profiles_display_name_length check (char_length(display_name) between 1 and 80),
|
|
first_name text
|
|
constraint profiles_first_name_length check (char_length(first_name) <= 80),
|
|
last_name text
|
|
constraint profiles_last_name_length check (char_length(last_name) <= 80),
|
|
-- E.164; verification is server-side (phone_verified_at is not user-writable).
|
|
phone text
|
|
constraint profiles_phone_e164 check (phone ~ '^\+[1-9][0-9]{6,14}$'),
|
|
phone_verified_at timestamptz,
|
|
locale text not null default 'fr'
|
|
constraint profiles_locale_supported check (locale in ('fr', 'en')),
|
|
timezone text not null default 'Europe/Paris'
|
|
constraint profiles_timezone_length check (char_length(timezone) between 1 and 64),
|
|
-- Platform-level switch (deactivation by staff). Not user-writable.
|
|
active boolean not null default true,
|
|
created_at timestamptz not null default now(),
|
|
updated_at timestamptz not null default now()
|
|
);
|
|
|
|
comment on table public.profiles is
|
|
'Account profile, id = auth.users.id. Created by trigger on auth.users; never inserted through the API.';
|
|
comment on column public.profiles.active is
|
|
'false = deactivated by platform staff; role helpers treat the account as having no platform role.';
|
|
|
|
create trigger profiles_set_updated_at
|
|
before update on public.profiles
|
|
for each row execute function private.set_updated_at();
|
|
|
|
-- A new phone number is unverified until the server verifies it again.
|
|
create or replace function private.reset_phone_verification()
|
|
returns trigger
|
|
language plpgsql
|
|
set search_path = ''
|
|
as $$
|
|
begin
|
|
if new.phone is distinct from old.phone then
|
|
new.phone_verified_at := null;
|
|
end if;
|
|
return new;
|
|
end;
|
|
$$;
|
|
|
|
create trigger profiles_reset_phone_verification
|
|
before update of phone on public.profiles
|
|
for each row execute function private.reset_phone_verification();
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- auth.users -> profiles
|
|
-- ---------------------------------------------------------------------------
|
|
create or replace function private.handle_new_auth_user()
|
|
returns trigger
|
|
language plpgsql
|
|
security definer
|
|
set search_path = ''
|
|
as $$
|
|
declare
|
|
v_meta jsonb := coalesce(new.raw_user_meta_data, '{}'::jsonb);
|
|
v_display_name text;
|
|
v_locale text;
|
|
begin
|
|
v_display_name := left(
|
|
coalesce(
|
|
nullif(trim(v_meta ->> 'display_name'), ''),
|
|
nullif(split_part(coalesce(new.email, ''), '@', 1), ''),
|
|
'Utilisateur'
|
|
),
|
|
80
|
|
);
|
|
v_locale := case
|
|
when v_meta ->> 'locale' in ('fr', 'en') then v_meta ->> 'locale'
|
|
else 'fr'
|
|
end;
|
|
|
|
insert into public.profiles (id, display_name, locale)
|
|
values (new.id, v_display_name, v_locale)
|
|
on conflict (id) do nothing;
|
|
|
|
return new;
|
|
end;
|
|
$$;
|
|
|
|
comment on function private.handle_new_auth_user() is
|
|
'AFTER INSERT on auth.users: creates the profile row. Display name from metadata, else the email local part.';
|
|
|
|
create trigger on_auth_user_created
|
|
after insert on auth.users
|
|
for each row execute function private.handle_new_auth_user();
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- Privileges + RLS (API roles). Supabase default privileges grant ALL to
|
|
-- anon/authenticated/service_role on new tables: start from zero instead.
|
|
-- ---------------------------------------------------------------------------
|
|
revoke all on public.profiles from public, anon, authenticated;
|
|
grant select on public.profiles to authenticated;
|
|
-- Column-level UPDATE: active and phone_verified_at stay server-only.
|
|
grant update (display_name, first_name, last_name, phone, locale, timezone)
|
|
on public.profiles to authenticated;
|
|
|
|
alter table public.profiles enable row level security;
|
|
|
|
create policy profiles_select_own
|
|
on public.profiles
|
|
for select
|
|
to authenticated
|
|
using (id = (select auth.uid()));
|
|
|
|
create policy profiles_update_own
|
|
on public.profiles
|
|
for update
|
|
to authenticated
|
|
using (id = (select auth.uid()))
|
|
with check (id = (select auth.uid()));
|
|
|
|
-- No INSERT policy (trigger only), no DELETE policy (no deletion of user data).
|