From 4407c6de0cb2e0deecf69378e0ccac3ca43cb9d4 Mon Sep 17 00:00:00 2001 From: Jonny Date: Sun, 5 Jul 2026 17:12:18 +0200 Subject: [PATCH] feat: add auftrag-fotos storage bucket with RLS policies --- .../20260705150911_auftrag_fotos_bucket.sql | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 supabase/migrations/20260705150911_auftrag_fotos_bucket.sql diff --git a/supabase/migrations/20260705150911_auftrag_fotos_bucket.sql b/supabase/migrations/20260705150911_auftrag_fotos_bucket.sql new file mode 100644 index 0000000..ccc5de5 --- /dev/null +++ b/supabase/migrations/20260705150911_auftrag_fotos_bucket.sql @@ -0,0 +1,44 @@ +insert into storage.buckets (id, name, public) +values ('auftrag-fotos', 'auftrag-fotos', false) +on conflict (id) do nothing; + +create policy "Fahrer liest Fotos des eigenen zugewiesenen Auftrags" + on storage.objects for select + using ( + bucket_id = 'auftrag-fotos' + and exists ( + select 1 from public.auftraege + where id = (storage.foldername(name))[1]::uuid + and zugewiesen_an = auth.uid() + ) + ); + +create policy "Disponent Admin Readonly lesen alle Auftragsfotos im Storage" + on storage.objects for select + using (bucket_id = 'auftrag-fotos' and public.user_role() in ('admin', 'disponent', 'readonly')); + +-- public.user_role() = 'fahrer' ist zwingend (gleiches Muster wie die +-- analoge Luecke bei der auftraege-UPDATE-Policy aus Task 2 und der +-- auftrag_fotos-INSERT-Policy aus Task 3): ohne dieses Praedikat wuerde +-- die Policy fuer jede eingeloggte Rolle gelten, sobald deren auth.uid() +-- zufaellig mit zugewiesen_an eines Auftrags uebereinstimmt. +create policy "Fahrer laedt Fotos fuer eigenen zugewiesenen offenen Auftrag hoch" + on storage.objects for insert + with check ( + bucket_id = 'auftrag-fotos' + and public.user_role() = 'fahrer' + and exists ( + select 1 from public.auftraege + where id = (storage.foldername(name))[1]::uuid + and zugewiesen_an = auth.uid() + and status = 'zugewiesen' + ) + ); + +create policy "Admin aktualisiert Auftragsfotos im Storage" + on storage.objects for update + using (bucket_id = 'auftrag-fotos' and public.user_role() = 'admin'); + +create policy "Admin loescht Auftragsfotos im Storage" + on storage.objects for delete + using (bucket_id = 'auftrag-fotos' and public.user_role() = 'admin');