forked from fahrPool/fahrPool
57 lines
2.4 KiB
SQL
57 lines
2.4 KiB
SQL
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.
|
|
--
|
|
-- Gleiche zweigeteilte Bedingung wie bei der auftrag_fotos-INSERT-Policy:
|
|
-- Pflichtfotos nur bei status = 'zugewiesen', Schadensfotos zusaetzlich auch
|
|
-- bei status = 'unterwegs'. Es gibt hier keine "position"-Spalte, nur den
|
|
-- Dateipfad (`${auftragId}/${position}.jpg` bzw.
|
|
-- `${auftragId}/schaden-<timestamp>-<i>.jpg`, siehe actions.ts) -- die
|
|
-- Unterscheidung wird deshalb aus dem zweiten Pfadsegment abgeleitet.
|
|
-- Policy-Name bewusst kurz gehalten (< 63 Byte, Postgres-Identifier-Limit)
|
|
-- und nicht mehr auf "offen" bezogen, da sie jetzt auch fuer 'unterwegs' gilt.
|
|
create policy "Fahrer laedt Auftragsfotos 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 (
|
|
(split_part(name, '/', 2) not like 'schaden-%' and status = 'zugewiesen')
|
|
or (split_part(name, '/', 2) like 'schaden-%' and status in ('zugewiesen', 'unterwegs'))
|
|
)
|
|
)
|
|
);
|
|
|
|
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');
|