1
0
Fork 0
fahrPool/supabase/migrations/20260705144530_auftrag_foto...

68 lines
2.6 KiB
SQL

create table public.auftrag_fotos (
id uuid primary key default gen_random_uuid(),
auftrag_id uuid not null references public.auftraege (id) on delete cascade,
position text not null check (position in (
'front', 'seitlich_rechts', 'rechte_seite', 'seitlich_rechts_heck',
'heck', 'seitlich_links_heck', 'linke_seite', 'seitlich_vorne_links',
'schaden'
)),
storage_path text not null,
uploaded_by uuid references public.profiles (id),
uploaded_at timestamptz not null default now()
);
-- Pro Auftrag genau ein Foto je Pflicht-Position, aber beliebig viele
-- "schaden"-Fotos (partieller Index deckt nur die Pflicht-Positionen ab).
create unique index auftrag_fotos_pflicht_position_unique
on public.auftrag_fotos (auftrag_id, position)
where position <> 'schaden';
alter table public.auftrag_fotos enable row level security;
grant select, insert on public.auftrag_fotos to authenticated;
grant delete, update on public.auftrag_fotos to authenticated;
create policy "Fahrer sieht Fotos des eigenen zugewiesenen Auftrags"
on public.auftrag_fotos for select
using (
exists (
select 1 from public.auftraege
where id = auftrag_fotos.auftrag_id and zugewiesen_an = auth.uid()
)
);
create policy "Disponent Admin Readonly sehen alle Auftragsfotos"
on public.auftrag_fotos for select
using (public.user_role() in ('admin', 'disponent', 'readonly'));
-- public.user_role() = 'fahrer' und uploaded_by = auth.uid() sind beide
-- zwingend (nicht nur Dokumentation): ohne die Rollen-Pruefung wuerde die
-- Policy fuer jede eingeloggte Rolle gelten (gleiches Muster wie die
-- analoge Luecke bei der auftraege-UPDATE-Policy aus Task 2); ohne die
-- Identitaets-Bindung koennte ein Fahrer ein fremdes uploaded_by
-- unterschieben. Die App (Task 10) MUSS deshalb `uploaded_by` beim
-- Insert/Upsert explizit auf die eigene auth.uid() setzen -- ein Insert
-- ohne diesen Wert (uploaded_by bleibt NULL) scheitert an dieser Policy,
-- da `null = auth.uid()` in Postgres nie wahr ist.
create policy "Fahrer laedt Fotos fuer eigenen zugewiesenen Auftrag hoch"
on public.auftrag_fotos for insert
with check (
public.user_role() = 'fahrer'
and uploaded_by = auth.uid()
and exists (
select 1 from public.auftraege
where id = auftrag_fotos.auftrag_id
and zugewiesen_an = auth.uid()
and status = 'zugewiesen'
)
);
create policy "Admin aktualisiert Auftragsfotos"
on public.auftrag_fotos for update
using (public.user_role() = 'admin')
with check (public.user_role() = 'admin');
create policy "Admin loescht Auftragsfotos"
on public.auftrag_fotos for delete
using (public.user_role() = 'admin');