forked from fahrPool/fahrPool
419 lines
17 KiB
SQL
419 lines
17 KiB
SQL
-- Mandantenfaehigkeit, Teil 2: jede Policy prueft zusaetzlich die Firma.
|
|
-- Rollenlogik unveraendert; der Superadmin hat keine Policy auf Fachdaten.
|
|
-- Ausnahme: "Nutzer sehen ihr eigenes Profil" bleibt ohne Firmenbedingung
|
|
-- (Login-Pruefung, Layout, Superadmin).
|
|
|
|
-- ------------------------------------------------------------------ firmen
|
|
create policy "Nutzer sehen die eigene Firma"
|
|
on public.firmen for select
|
|
using (id = public.user_firma_id());
|
|
|
|
create policy "Admin aktualisiert die eigene Firma"
|
|
on public.firmen for update
|
|
using (id = public.user_firma_id() and public.user_role() = 'admin')
|
|
with check (id = public.user_firma_id() and public.user_role() = 'admin');
|
|
|
|
-- ---------------------------------------------------------------- profiles
|
|
drop policy "Admins sehen alle Profile" on public.profiles;
|
|
create policy "Admins sehen alle Profile"
|
|
on public.profiles for select
|
|
using (public.user_role() = 'admin' and firma_id = public.user_firma_id());
|
|
|
|
drop policy "Admins aktualisieren alle Profile" on public.profiles;
|
|
create policy "Admins aktualisieren alle Profile"
|
|
on public.profiles for update
|
|
using (public.user_role() = 'admin' and firma_id = public.user_firma_id())
|
|
with check (public.user_role() = 'admin' and firma_id = public.user_firma_id() and role <> 'superadmin');
|
|
|
|
drop policy "Disponent sieht Fahrer-Profile" on public.profiles;
|
|
create policy "Disponent sieht Fahrer-Profile"
|
|
on public.profiles for select
|
|
using (public.user_role() = 'disponent' and role = 'fahrer' and firma_id = public.user_firma_id());
|
|
|
|
drop policy "Fahrer und Readonly sehen Admin/Disponent-Profile" on public.profiles;
|
|
create policy "Fahrer und Readonly sehen Admin/Disponent-Profile"
|
|
on public.profiles for select
|
|
using (
|
|
public.user_role() in ('fahrer', 'readonly')
|
|
and role in ('admin', 'disponent')
|
|
and firma_id = public.user_firma_id()
|
|
);
|
|
|
|
-- -------------------------------------------------------------------- orte
|
|
drop policy "Eingeloggte Nutzer sehen alle Orte" on public.orte;
|
|
create policy "Eingeloggte Nutzer sehen alle Orte"
|
|
on public.orte for select
|
|
using (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent', 'fahrer', 'readonly'));
|
|
|
|
drop policy "Disponent und Admin legen Orte an" on public.orte;
|
|
create policy "Disponent und Admin legen Orte an"
|
|
on public.orte for insert
|
|
with check (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent'));
|
|
|
|
drop policy "Disponent und Admin bearbeiten Orte" on public.orte;
|
|
create policy "Disponent und Admin bearbeiten Orte"
|
|
on public.orte for update
|
|
using (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent'))
|
|
with check (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent'));
|
|
|
|
-- --------------------------------------------------------------- fahrzeuge
|
|
drop policy "Eingeloggte Nutzer sehen alle Fahrzeuge" on public.fahrzeuge;
|
|
create policy "Eingeloggte Nutzer sehen alle Fahrzeuge"
|
|
on public.fahrzeuge for select
|
|
using (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent', 'fahrer', 'readonly'));
|
|
|
|
drop policy "Disponent und Admin legen Fahrzeuge an" on public.fahrzeuge;
|
|
create policy "Disponent und Admin legen Fahrzeuge an"
|
|
on public.fahrzeuge for insert
|
|
with check (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent'));
|
|
|
|
drop policy "Disponent und Admin bearbeiten Fahrzeuge" on public.fahrzeuge;
|
|
create policy "Disponent und Admin bearbeiten Fahrzeuge"
|
|
on public.fahrzeuge for update
|
|
using (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent'))
|
|
with check (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent'));
|
|
|
|
-- --------------------------------------------------------------- auftraege
|
|
drop policy "Sichtbarkeit von Auftraegen nach Rolle" on public.auftraege;
|
|
create policy "Sichtbarkeit von Auftraegen nach Rolle"
|
|
on public.auftraege for select
|
|
using (
|
|
firma_id = public.user_firma_id()
|
|
and (
|
|
public.user_role() in ('admin', 'disponent', 'readonly')
|
|
or zugewiesen_an = auth.uid()
|
|
or status = 'offen'
|
|
)
|
|
);
|
|
|
|
drop policy "Disponent und Admin legen Auftraege an" on public.auftraege;
|
|
create policy "Disponent und Admin legen Auftraege an"
|
|
on public.auftraege for insert
|
|
with check (
|
|
firma_id = public.user_firma_id()
|
|
and public.user_role() in ('admin', 'disponent')
|
|
and erstellt_von = auth.uid()
|
|
);
|
|
|
|
drop policy "Disponent und Admin bearbeiten alle Auftraege" on public.auftraege;
|
|
create policy "Disponent und Admin bearbeiten alle Auftraege"
|
|
on public.auftraege for update
|
|
using (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent'))
|
|
with check (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent'));
|
|
|
|
drop policy "Fahrer uebernimmt offene oder bearbeitet eigene Auftraege" on public.auftraege;
|
|
create policy "Fahrer uebernimmt offene oder bearbeitet eigene Auftraege"
|
|
on public.auftraege for update
|
|
using (
|
|
firma_id = public.user_firma_id()
|
|
and public.user_role() = 'fahrer'
|
|
and (zugewiesen_an = auth.uid() or status = 'offen')
|
|
)
|
|
with check (
|
|
firma_id = public.user_firma_id()
|
|
and public.user_role() = 'fahrer'
|
|
and (zugewiesen_an = auth.uid() or zugewiesen_an is null)
|
|
);
|
|
|
|
-- ----------------------------------------------------------- auftrag_fotos
|
|
drop policy "Disponent Admin Readonly sehen alle Auftragsfotos" on public.auftrag_fotos;
|
|
create policy "Disponent Admin Readonly sehen alle Auftragsfotos"
|
|
on public.auftrag_fotos for select
|
|
using (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent', 'readonly'));
|
|
|
|
drop policy "Fahrer sieht Fotos des eigenen zugewiesenen Auftrags" on public.auftrag_fotos;
|
|
create policy "Fahrer sieht Fotos des eigenen zugewiesenen Auftrags"
|
|
on public.auftrag_fotos for select
|
|
using (
|
|
firma_id = public.user_firma_id()
|
|
and exists (
|
|
select 1 from public.auftraege
|
|
where auftraege.id = auftrag_fotos.auftrag_id and auftraege.zugewiesen_an = auth.uid()
|
|
)
|
|
);
|
|
|
|
drop policy "Fahrer laedt Fotos fuer eigenen zugewiesenen Auftrag hoch" on public.auftrag_fotos;
|
|
create policy "Fahrer laedt Fotos fuer eigenen zugewiesenen Auftrag hoch"
|
|
on public.auftrag_fotos for insert
|
|
with check (
|
|
firma_id = public.user_firma_id()
|
|
and public.user_role() = 'fahrer'
|
|
and uploaded_by = auth.uid()
|
|
and exists (
|
|
select 1 from public.auftraege
|
|
where auftraege.id = auftrag_fotos.auftrag_id
|
|
and auftraege.zugewiesen_an = auth.uid()
|
|
and (
|
|
(auftrag_fotos.position <> 'schaden' and auftraege.status = 'zugewiesen')
|
|
or (auftrag_fotos.position = 'schaden' and auftraege.status in ('zugewiesen', 'unterwegs'))
|
|
)
|
|
)
|
|
);
|
|
|
|
drop policy "Admin aktualisiert Auftragsfotos" on public.auftrag_fotos;
|
|
create policy "Admin aktualisiert Auftragsfotos"
|
|
on public.auftrag_fotos for update
|
|
using (firma_id = public.user_firma_id() and public.user_role() = 'admin')
|
|
with check (firma_id = public.user_firma_id() and public.user_role() = 'admin');
|
|
|
|
drop policy "Admin loescht Auftragsfotos" on public.auftrag_fotos;
|
|
create policy "Admin loescht Auftragsfotos"
|
|
on public.auftrag_fotos for delete
|
|
using (firma_id = public.user_firma_id() and public.user_role() = 'admin');
|
|
|
|
-- ----------------------------------------------------------- arbeitszeiten
|
|
drop policy "Fahrer sieht eigene Arbeitszeiten, Disponent/Admin sehen alle" on public.arbeitszeiten;
|
|
create policy "Fahrer sieht eigene Arbeitszeiten, Disponent/Admin sehen alle"
|
|
on public.arbeitszeiten for select
|
|
using (
|
|
firma_id = public.user_firma_id()
|
|
and (fahrer_id = auth.uid() or public.user_role() in ('admin', 'disponent'))
|
|
);
|
|
|
|
drop policy "Fahrer stempelt sich selbst ein" on public.arbeitszeiten;
|
|
create policy "Fahrer stempelt sich selbst ein"
|
|
on public.arbeitszeiten for insert
|
|
with check (
|
|
firma_id = public.user_firma_id()
|
|
and public.user_role() = 'fahrer'
|
|
and fahrer_id = auth.uid()
|
|
and geht_um is null
|
|
and korrigiert_von is null
|
|
and korrigiert_am is null
|
|
);
|
|
|
|
drop policy "Fahrer schliesst Schicht, Disponent/Admin korrigieren" on public.arbeitszeiten;
|
|
create policy "Fahrer schliesst Schicht, Disponent/Admin korrigieren"
|
|
on public.arbeitszeiten for update
|
|
using (
|
|
firma_id = public.user_firma_id()
|
|
and (fahrer_id = auth.uid() or public.user_role() in ('admin', 'disponent'))
|
|
)
|
|
with check (
|
|
firma_id = public.user_firma_id()
|
|
and (fahrer_id = auth.uid() or public.user_role() in ('admin', 'disponent'))
|
|
);
|
|
|
|
-- ---------------------------------------------------------- nutzer_details
|
|
drop policy "Admin und Disponent sehen alle Details" on public.nutzer_details;
|
|
create policy "Admin und Disponent sehen alle Details"
|
|
on public.nutzer_details for select
|
|
using (firma_id = public.user_firma_id() and public.user_role() in ('admin', 'disponent'));
|
|
|
|
drop policy "Nutzer und Admin legen Details an" on public.nutzer_details;
|
|
create policy "Nutzer und Admin legen Details an"
|
|
on public.nutzer_details for insert
|
|
with check (firma_id = public.user_firma_id() and (auth.uid() = user_id or public.user_role() = 'admin'));
|
|
|
|
drop policy "Nutzer und Admin aktualisieren Details" on public.nutzer_details;
|
|
create policy "Nutzer und Admin aktualisieren Details"
|
|
on public.nutzer_details for update
|
|
using (firma_id = public.user_firma_id() and (auth.uid() = user_id or public.user_role() = 'admin'))
|
|
with check (firma_id = public.user_firma_id() and (auth.uid() = user_id or public.user_role() = 'admin'));
|
|
|
|
-- "Nutzer sehen ihre eigenen Details" (auth.uid() = user_id) bleibt unveraendert.
|
|
|
|
-- ----------------------------------------------------- notification_outbox
|
|
drop policy "Admin liest notification_outbox" on public.notification_outbox;
|
|
create policy "Admin liest notification_outbox"
|
|
on public.notification_outbox for select
|
|
using (firma_id = public.user_firma_id() and public.user_role() = 'admin');
|
|
|
|
-- ================================================================ Storage
|
|
-- Pfade <auftrag_id>/...: Zugriff nur, wenn der Auftrag zur eigenen Firma gehoert.
|
|
|
|
drop policy "Fahrer liest Fotos des eigenen zugewiesenen Auftrags" on storage.objects;
|
|
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 a
|
|
where a.id = (storage.foldername(name))[1]::uuid
|
|
and a.zugewiesen_an = auth.uid()
|
|
and a.firma_id = public.user_firma_id()
|
|
)
|
|
);
|
|
|
|
drop policy "Disponent Admin Readonly lesen alle Auftragsfotos im Storage" on storage.objects;
|
|
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')
|
|
and exists (
|
|
select 1 from public.auftraege a
|
|
where a.id = (storage.foldername(name))[1]::uuid and a.firma_id = public.user_firma_id()
|
|
)
|
|
);
|
|
|
|
drop policy "Fahrer laedt Auftragsfotos hoch" on storage.objects;
|
|
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 a
|
|
where a.id = (storage.foldername(name))[1]::uuid
|
|
and a.zugewiesen_an = auth.uid()
|
|
and a.firma_id = public.user_firma_id()
|
|
and (
|
|
(split_part(name, '/', 2) not like 'schaden-%' and a.status = 'zugewiesen')
|
|
or (split_part(name, '/', 2) like 'schaden-%' and a.status in ('zugewiesen', 'unterwegs'))
|
|
)
|
|
)
|
|
);
|
|
|
|
drop policy "Admin aktualisiert Auftragsfotos im Storage" on storage.objects;
|
|
create policy "Admin aktualisiert Auftragsfotos im Storage"
|
|
on storage.objects for update
|
|
using (
|
|
bucket_id = 'auftrag-fotos'
|
|
and public.user_role() = 'admin'
|
|
and exists (
|
|
select 1 from public.auftraege a
|
|
where a.id = (storage.foldername(name))[1]::uuid and a.firma_id = public.user_firma_id()
|
|
)
|
|
);
|
|
|
|
drop policy "Admin loescht Auftragsfotos im Storage" on storage.objects;
|
|
create policy "Admin loescht Auftragsfotos im Storage"
|
|
on storage.objects for delete
|
|
using (
|
|
bucket_id = 'auftrag-fotos'
|
|
and public.user_role() = 'admin'
|
|
and exists (
|
|
select 1 from public.auftraege a
|
|
where a.id = (storage.foldername(name))[1]::uuid and a.firma_id = public.user_firma_id()
|
|
)
|
|
);
|
|
|
|
drop policy "Fahrer liest Unterschrift des eigenen zugewiesenen Auftrags" on storage.objects;
|
|
create policy "Fahrer liest Unterschrift des eigenen zugewiesenen Auftrags"
|
|
on storage.objects for select
|
|
using (
|
|
bucket_id = 'auftrag-unterschriften'
|
|
and exists (
|
|
select 1 from public.auftraege a
|
|
where a.id = (storage.foldername(name))[1]::uuid
|
|
and a.zugewiesen_an = auth.uid()
|
|
and a.firma_id = public.user_firma_id()
|
|
)
|
|
);
|
|
|
|
drop policy "Disponent Admin Readonly lesen alle Unterschriften im Storage" on storage.objects;
|
|
create policy "Disponent Admin Readonly lesen alle Unterschriften im Storage"
|
|
on storage.objects for select
|
|
using (
|
|
bucket_id = 'auftrag-unterschriften'
|
|
and public.user_role() in ('admin', 'disponent', 'readonly')
|
|
and exists (
|
|
select 1 from public.auftraege a
|
|
where a.id = (storage.foldername(name))[1]::uuid and a.firma_id = public.user_firma_id()
|
|
)
|
|
);
|
|
|
|
drop policy "Fahrer laedt Unterschrift bei Fahrtende hoch" on storage.objects;
|
|
create policy "Fahrer laedt Unterschrift bei Fahrtende hoch"
|
|
on storage.objects for insert
|
|
with check (
|
|
bucket_id = 'auftrag-unterschriften'
|
|
and public.user_role() = 'fahrer'
|
|
and exists (
|
|
select 1 from public.auftraege a
|
|
where a.id = (storage.foldername(name))[1]::uuid
|
|
and a.zugewiesen_an = auth.uid()
|
|
and a.firma_id = public.user_firma_id()
|
|
and a.status = 'unterwegs'
|
|
)
|
|
);
|
|
|
|
drop policy "Fahrer aktualisiert eigene Unterschrift bei Fahrtende" on storage.objects;
|
|
create policy "Fahrer aktualisiert eigene Unterschrift bei Fahrtende"
|
|
on storage.objects for update
|
|
using (
|
|
bucket_id = 'auftrag-unterschriften'
|
|
and public.user_role() = 'fahrer'
|
|
and exists (
|
|
select 1 from public.auftraege a
|
|
where a.id = (storage.foldername(name))[1]::uuid
|
|
and a.zugewiesen_an = auth.uid()
|
|
and a.firma_id = public.user_firma_id()
|
|
and a.status = 'unterwegs'
|
|
)
|
|
)
|
|
with check (
|
|
bucket_id = 'auftrag-unterschriften'
|
|
and public.user_role() = 'fahrer'
|
|
and exists (
|
|
select 1 from public.auftraege a
|
|
where a.id = (storage.foldername(name))[1]::uuid
|
|
and a.zugewiesen_an = auth.uid()
|
|
and a.firma_id = public.user_firma_id()
|
|
and a.status = 'unterwegs'
|
|
)
|
|
);
|
|
|
|
drop policy "Admin aktualisiert Unterschriften im Storage" on storage.objects;
|
|
create policy "Admin aktualisiert Unterschriften im Storage"
|
|
on storage.objects for update
|
|
using (
|
|
bucket_id = 'auftrag-unterschriften'
|
|
and public.user_role() = 'admin'
|
|
and exists (
|
|
select 1 from public.auftraege a
|
|
where a.id = (storage.foldername(name))[1]::uuid and a.firma_id = public.user_firma_id()
|
|
)
|
|
);
|
|
|
|
drop policy "Admin loescht Unterschriften im Storage" on storage.objects;
|
|
create policy "Admin loescht Unterschriften im Storage"
|
|
on storage.objects for delete
|
|
using (
|
|
bucket_id = 'auftrag-unterschriften'
|
|
and public.user_role() = 'admin'
|
|
and exists (
|
|
select 1 from public.auftraege a
|
|
where a.id = (storage.foldername(name))[1]::uuid and a.firma_id = public.user_firma_id()
|
|
)
|
|
);
|
|
|
|
-- ------------------------------------------------------------ firmen-logos
|
|
insert into storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
|
|
values ('firmen-logos', 'firmen-logos', false, 1048576, array['image/png', 'image/jpeg', 'image/svg+xml'])
|
|
on conflict (id) do nothing;
|
|
|
|
create policy "Nutzer lesen das Logo der eigenen Firma"
|
|
on storage.objects for select
|
|
using (bucket_id = 'firmen-logos' and (storage.foldername(name))[1] = public.user_firma_id()::text);
|
|
|
|
create policy "Admin laedt das Logo der eigenen Firma hoch"
|
|
on storage.objects for insert
|
|
with check (
|
|
bucket_id = 'firmen-logos'
|
|
and public.user_role() = 'admin'
|
|
and (storage.foldername(name))[1] = public.user_firma_id()::text
|
|
);
|
|
|
|
create policy "Admin aktualisiert das Logo der eigenen Firma"
|
|
on storage.objects for update
|
|
using (
|
|
bucket_id = 'firmen-logos'
|
|
and public.user_role() = 'admin'
|
|
and (storage.foldername(name))[1] = public.user_firma_id()::text
|
|
)
|
|
with check (
|
|
bucket_id = 'firmen-logos'
|
|
and public.user_role() = 'admin'
|
|
and (storage.foldername(name))[1] = public.user_firma_id()::text
|
|
);
|
|
|
|
create policy "Admin loescht das Logo der eigenen Firma"
|
|
on storage.objects for delete
|
|
using (
|
|
bucket_id = 'firmen-logos'
|
|
and public.user_role() = 'admin'
|
|
and (storage.foldername(name))[1] = public.user_firma_id()::text
|
|
);
|