forked from fahrPool/fahrPool
39 lines
1.5 KiB
PL/PgSQL
39 lines
1.5 KiB
PL/PgSQL
-- Hinweis (Abweichung vom Brief): Seit dem Supabase-Security-Lockdown vom
|
|
-- 2025-04-21 ("Restricting Access on Auth, Storage, and Realtime Schemas")
|
|
-- besitzt die Migrations-Rolle `postgres` kein CREATE-Recht mehr auf dem
|
|
-- Schema `auth` (nur noch USAGE). Ein `create function auth.user_role()`
|
|
-- scheitert deshalb mit "permission denied for schema auth" (42501).
|
|
-- Die offizielle Supabase-Empfehlung ist, solche Helper-Funktionen im
|
|
-- `public`-Schema anzulegen. Die Funktion heisst daher `public.user_role()`
|
|
-- statt `auth.user_role()`. Künftige Teilprojekte müssen `public.user_role()`
|
|
-- verwenden.
|
|
|
|
-- Zusaetzliche Abweichung vom Brief: Task 2 hat keine Tabellen-Grants fuer
|
|
-- die Rolle `authenticated` auf `public.profiles` gesetzt. RLS-Policies
|
|
-- wirken nur zusaetzlich zu den normalen Postgres-Privilegien, nicht
|
|
-- anstelle davon -- ohne GRANT bekommt `authenticated` weiterhin
|
|
-- "permission denied for table profiles". Deshalb hier ergaenzt.
|
|
grant select, update on public.profiles to authenticated;
|
|
|
|
create or replace function public.user_role()
|
|
returns public.app_role
|
|
language sql
|
|
stable
|
|
security definer
|
|
set search_path = public
|
|
as $$
|
|
select role from public.profiles where id = auth.uid();
|
|
$$;
|
|
|
|
create policy "Nutzer sehen ihr eigenes Profil"
|
|
on public.profiles for select
|
|
using (auth.uid() = id);
|
|
|
|
create policy "Admins sehen alle Profile"
|
|
on public.profiles for select
|
|
using (public.user_role() = 'admin');
|
|
|
|
create policy "Admins aktualisieren alle Profile"
|
|
on public.profiles for update
|
|
using (public.user_role() = 'admin');
|