forked from fahrPool/fahrPool
feat: RLS und Storage pro Firma, Bucket firmen-logos
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2acaaced93
commit
f9a154603a
|
|
@ -0,0 +1,418 @@
|
|||
-- 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
|
||||
);
|
||||
|
|
@ -0,0 +1,147 @@
|
|||
begin;
|
||||
select plan(30);
|
||||
|
||||
insert into public.firmen (id, name) values ('00000000-0000-0000-0000-00000000f002', 'Firma B');
|
||||
|
||||
insert into auth.users (id, email) values
|
||||
('a0000000-0000-0000-0000-0000000000a1', 'admin-a@rls.test.local'),
|
||||
('a0000000-0000-0000-0000-0000000000a2', 'dispo-a@rls.test.local'),
|
||||
('a0000000-0000-0000-0000-0000000000a3', 'fahrer-a@rls.test.local'),
|
||||
('b0000000-0000-0000-0000-0000000000b1', 'admin-b@rls.test.local'),
|
||||
('b0000000-0000-0000-0000-0000000000b2', 'dispo-b@rls.test.local'),
|
||||
('b0000000-0000-0000-0000-0000000000b3', 'fahrer-b@rls.test.local'),
|
||||
('5a000000-0000-0000-0000-000000000001', 'superadmin@rls.test.local');
|
||||
|
||||
update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001', role = 'admin' where id = 'a0000000-0000-0000-0000-0000000000a1';
|
||||
update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001', role = 'disponent' where id = 'a0000000-0000-0000-0000-0000000000a2';
|
||||
update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001', role = 'fahrer' where id = 'a0000000-0000-0000-0000-0000000000a3';
|
||||
update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f002', role = 'admin' where id = 'b0000000-0000-0000-0000-0000000000b1';
|
||||
update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f002', role = 'disponent' where id = 'b0000000-0000-0000-0000-0000000000b2';
|
||||
update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f002', role = 'fahrer' where id = 'b0000000-0000-0000-0000-0000000000b3';
|
||||
update public.profiles set role = 'superadmin' where id = '5a000000-0000-0000-0000-000000000001';
|
||||
|
||||
update public.firmen set whatsapp_empfaenger = '+4915100000001' where id = '00000000-0000-0000-0000-00000000f001';
|
||||
|
||||
-- Daten Firma A
|
||||
insert into public.orte (id, adresse, ansprechpartner, firma_id) values
|
||||
('a0000000-0000-0000-0000-0000000000c1', 'Ort A', 'Herr A', '00000000-0000-0000-0000-00000000f001');
|
||||
insert into public.fahrzeuge (id, kennzeichen, firma_id) values
|
||||
('a0000000-0000-0000-0000-0000000000d1', 'MT-A 1', '00000000-0000-0000-0000-00000000f001');
|
||||
insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, firma_id) values
|
||||
('a0000000-0000-0000-0000-0000000000e1', 'a0000000-0000-0000-0000-0000000000c1', 'a0000000-0000-0000-0000-0000000000c1',
|
||||
'a0000000-0000-0000-0000-0000000000d1', 'a0000000-0000-0000-0000-0000000000a1', 'zugewiesen',
|
||||
'a0000000-0000-0000-0000-0000000000a3', '00000000-0000-0000-0000-00000000f001'),
|
||||
('a0000000-0000-0000-0000-0000000000e2', 'a0000000-0000-0000-0000-0000000000c1', 'a0000000-0000-0000-0000-0000000000c1',
|
||||
'a0000000-0000-0000-0000-0000000000d1', 'a0000000-0000-0000-0000-0000000000a1', 'offen', null,
|
||||
'00000000-0000-0000-0000-00000000f001');
|
||||
insert into public.auftrag_fotos (auftrag_id, position, storage_path, uploaded_by) values
|
||||
('a0000000-0000-0000-0000-0000000000e1', 'front', 'a0000000-0000-0000-0000-0000000000e1/front.jpg', 'a0000000-0000-0000-0000-0000000000a3');
|
||||
insert into public.arbeitszeiten (fahrer_id, kommt_um, firma_id) values
|
||||
('a0000000-0000-0000-0000-0000000000a3', now(), '00000000-0000-0000-0000-00000000f001');
|
||||
insert into public.nutzer_details (user_id, telefon) values
|
||||
('a0000000-0000-0000-0000-0000000000a3', '+4915100000002');
|
||||
insert into storage.objects (bucket_id, name, owner) values
|
||||
('auftrag-fotos', 'a0000000-0000-0000-0000-0000000000e1/front.jpg', 'a0000000-0000-0000-0000-0000000000a3'),
|
||||
('auftrag-unterschriften', 'a0000000-0000-0000-0000-0000000000e1/unterschrift.png', 'a0000000-0000-0000-0000-0000000000a3'),
|
||||
('firmen-logos', '00000000-0000-0000-0000-00000000f001/logo-1.png', 'a0000000-0000-0000-0000-0000000000a1');
|
||||
|
||||
-- Daten Firma B
|
||||
insert into public.orte (id, adresse, ansprechpartner, firma_id) values
|
||||
('b0000000-0000-0000-0000-0000000000c1', 'Ort B', 'Frau B', '00000000-0000-0000-0000-00000000f002');
|
||||
|
||||
-- ====================== Admin B
|
||||
set local role authenticated;
|
||||
set local request.jwt.claims to '{"sub": "b0000000-0000-0000-0000-0000000000b1", "role": "authenticated"}';
|
||||
|
||||
select ok(not exists (select 1 from public.orte where firma_id <> '00000000-0000-0000-0000-00000000f002'), 'Admin B sieht keine Orte von A');
|
||||
select ok(exists (select 1 from public.orte where id = 'b0000000-0000-0000-0000-0000000000c1'), 'Admin B sieht eigene Orte');
|
||||
select ok(not exists (select 1 from public.fahrzeuge where firma_id <> '00000000-0000-0000-0000-00000000f002'), 'Admin B sieht keine Fahrzeuge von A');
|
||||
select ok(not exists (select 1 from public.auftraege where firma_id <> '00000000-0000-0000-0000-00000000f002'), 'Admin B sieht keine Auftraege von A');
|
||||
select ok(not exists (select 1 from public.auftrag_fotos), 'Admin B sieht keine Auftragsfotos von A');
|
||||
select ok(not exists (select 1 from public.arbeitszeiten), 'Admin B sieht keine Arbeitszeiten von A');
|
||||
select ok(not exists (select 1 from public.nutzer_details), 'Admin B sieht keine Nutzer-Details von A');
|
||||
select ok(not exists (select 1 from public.profiles where firma_id = '00000000-0000-0000-0000-00000000f001'), 'Admin B sieht keine Profile von A');
|
||||
select ok(not exists (select 1 from public.notification_outbox), 'Admin B sieht keine Outbox-Zeilen von A');
|
||||
select is((select array_agg(id) from public.firmen), array['00000000-0000-0000-0000-00000000f002'::uuid], 'Admin B sieht nur die eigene Firma');
|
||||
select ok(not exists (select 1 from storage.objects where name like 'a0000000-0000-0000-0000-0000000000e1/%' or name like '00000000-0000-0000-0000-00000000f001/%'), 'Admin B sieht keine Storage-Objekte von A');
|
||||
|
||||
select is_empty(
|
||||
$$ update public.orte set adresse = 'x' where id = 'a0000000-0000-0000-0000-0000000000c1' returning 1 $$,
|
||||
'Admin B kann keinen Ort von A aendern');
|
||||
select is_empty(
|
||||
$$ update public.auftraege set status = 'storniert' where id = 'a0000000-0000-0000-0000-0000000000e2' returning 1 $$,
|
||||
'Admin B kann keinen Auftrag von A aendern');
|
||||
select is_empty(
|
||||
$$ update public.profiles set name = 'x' where id = 'a0000000-0000-0000-0000-0000000000a3' returning 1 $$,
|
||||
'Admin B kann kein Profil von A aendern');
|
||||
select is_empty(
|
||||
$$ update public.firmen set name = 'x' where id = '00000000-0000-0000-0000-00000000f001' returning 1 $$,
|
||||
'Admin B kann Firma A nicht umbenennen');
|
||||
select isnt_empty(
|
||||
$$ update public.firmen set name = 'Firma B neu' where id = '00000000-0000-0000-0000-00000000f002' returning 1 $$,
|
||||
'Admin B kann die eigene Firma umbenennen');
|
||||
|
||||
select throws_ok(
|
||||
$$ update public.profiles set role = 'superadmin' where id = 'b0000000-0000-0000-0000-0000000000b3' $$,
|
||||
null, null,
|
||||
'Admin B kann niemanden zum Superadmin machen');
|
||||
select throws_ok(
|
||||
$$ insert into public.nutzer_details (user_id, telefon) values ('a0000000-0000-0000-0000-0000000000a1', '+4915100000003') $$,
|
||||
'42501', 'new row violates row-level security policy for table "nutzer_details"',
|
||||
'Admin B kann keine Details fuer Nutzer von A anlegen');
|
||||
select throws_ok(
|
||||
$$ insert into storage.objects (bucket_id, name, owner) values ('firmen-logos', '00000000-0000-0000-0000-00000000f001/logo-2.png', 'b0000000-0000-0000-0000-0000000000b1') $$,
|
||||
'42501', 'new row violates row-level security policy for table "objects"',
|
||||
'Admin B kann kein Logo fuer Firma A hochladen');
|
||||
select lives_ok(
|
||||
$$ insert into storage.objects (bucket_id, name, owner) values ('firmen-logos', '00000000-0000-0000-0000-00000000f002/logo-1.png', 'b0000000-0000-0000-0000-0000000000b1') $$,
|
||||
'Admin B kann ein Logo fuer die eigene Firma hochladen');
|
||||
select throws_ok(
|
||||
$$ select * from public.firmen_smtp $$,
|
||||
'42501', null,
|
||||
'firmen_smtp ist fuer angemeldete Nutzer gesperrt');
|
||||
|
||||
reset role;
|
||||
reset request.jwt.claims;
|
||||
|
||||
-- ====================== Fahrer B
|
||||
set local role authenticated;
|
||||
set local request.jwt.claims to '{"sub": "b0000000-0000-0000-0000-0000000000b3", "role": "authenticated"}';
|
||||
select ok(not exists (select 1 from public.auftraege where id = 'a0000000-0000-0000-0000-0000000000e2'), 'Fahrer B sieht den offenen Auftrag von A nicht');
|
||||
select is_empty(
|
||||
$$ update public.auftraege set zugewiesen_an = 'b0000000-0000-0000-0000-0000000000b3', status = 'zugewiesen' where id = 'a0000000-0000-0000-0000-0000000000e2' returning 1 $$,
|
||||
'Fahrer B kann den offenen Auftrag von A nicht uebernehmen');
|
||||
reset role;
|
||||
reset request.jwt.claims;
|
||||
|
||||
-- ====================== Disponent A
|
||||
set local role authenticated;
|
||||
set local request.jwt.claims to '{"sub": "a0000000-0000-0000-0000-0000000000a2", "role": "authenticated"}';
|
||||
select ok(
|
||||
exists (select 1 from public.profiles where id = 'a0000000-0000-0000-0000-0000000000a3')
|
||||
and not exists (select 1 from public.profiles where id = 'b0000000-0000-0000-0000-0000000000b3'),
|
||||
'Disponent A sieht nur Fahrer der eigenen Firma');
|
||||
select ok(exists (select 1 from public.auftraege where id = 'a0000000-0000-0000-0000-0000000000e1'), 'Disponent A sieht eigene Auftraege');
|
||||
reset role;
|
||||
reset request.jwt.claims;
|
||||
|
||||
-- ====================== Superadmin
|
||||
set local role authenticated;
|
||||
set local request.jwt.claims to '{"sub": "5a000000-0000-0000-0000-000000000001", "role": "authenticated"}';
|
||||
select ok(not exists (select 1 from public.auftraege), 'Superadmin sieht keine Auftraege');
|
||||
select ok(not exists (select 1 from public.firmen), 'Superadmin sieht ueber RLS keine Firmen');
|
||||
select ok(exists (select 1 from public.profiles where id = '5a000000-0000-0000-0000-000000000001'), 'Superadmin sieht sein eigenes Profil');
|
||||
reset role;
|
||||
reset request.jwt.claims;
|
||||
|
||||
-- ====================== Firma A deaktiviert
|
||||
update public.firmen set aktiv = false where id = '00000000-0000-0000-0000-00000000f001';
|
||||
set local role authenticated;
|
||||
set local request.jwt.claims to '{"sub": "a0000000-0000-0000-0000-0000000000a1", "role": "authenticated"}';
|
||||
select ok(not exists (select 1 from public.auftraege), 'Deaktivierte Firma: Admin sieht keine Auftraege mehr');
|
||||
select ok(exists (select 1 from public.profiles where id = 'a0000000-0000-0000-0000-0000000000a1'), 'Deaktivierte Firma: eigenes Profil bleibt lesbar');
|
||||
reset role;
|
||||
reset request.jwt.claims;
|
||||
|
||||
select * from finish();
|
||||
rollback;
|
||||
|
|
@ -81,6 +81,9 @@ select is(
|
|||
-- Admin-Update zu 'disponent' geaendert).
|
||||
insert into auth.users (id, email) values
|
||||
('44444444-4444-4444-4444-444444444444', 'fahrer2@test.local');
|
||||
-- Firmenzuordnung neuer Fixture-Nutzer: nur ohne JWT-Kontext erlaubt (Trigger firma_id_unveraenderlich)
|
||||
reset request.jwt.claims;
|
||||
update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null;
|
||||
update public.profiles set role = 'fahrer' where id = '44444444-4444-4444-4444-444444444444';
|
||||
|
||||
set local role authenticated;
|
||||
|
|
|
|||
Loading…
Reference in New Issue