diff --git a/supabase/migrations/20261012090000_superadmin_rolle.sql b/supabase/migrations/20261012090000_superadmin_rolle.sql new file mode 100644 index 0000000..981f999 --- /dev/null +++ b/supabase/migrations/20261012090000_superadmin_rolle.sql @@ -0,0 +1,3 @@ +-- Eigene Migration: ein neuer Enum-Wert ist erst nach dem Commit der +-- Transaktion verwendbar, die ihn anlegt (die Folgemigration nutzt ihn). +alter type public.app_role add value if not exists 'superadmin'; diff --git a/supabase/migrations/20261012090100_mandanten_struktur.sql b/supabase/migrations/20261012090100_mandanten_struktur.sql new file mode 100644 index 0000000..f38db09 --- /dev/null +++ b/supabase/migrations/20261012090100_mandanten_struktur.sql @@ -0,0 +1,411 @@ +-- Mandantenfaehigkeit, Teil 1: Firmen, firma_id in allen Fachtabellen, +-- Trigger zum Setzen/Schuetzen der firma_id, Eindeutigkeit pro Firma. +-- Die RLS-Policies folgen in 20261012090200_mandanten_rls.sql. + +-- ---------------------------------------------------------------- firmen +create table public.firmen ( + id uuid primary key default gen_random_uuid(), + name text not null, + aktiv boolean not null default true, + logo_pfad text, + whatsapp_empfaenger text, + farbe_primaer text, + farbe_dunkel text, + created_at timestamptz not null default now(), + constraint firmen_name_nicht_leer check (length(btrim(name)) > 0), + constraint firmen_whatsapp_e164 check (whatsapp_empfaenger is null or whatsapp_empfaenger ~ '^\+[1-9][0-9]{7,14}$'), + constraint firmen_farbe_primaer_hex check (farbe_primaer is null or farbe_primaer ~ '^#[0-9a-f]{6}$'), + constraint firmen_farbe_dunkel_hex check (farbe_dunkel is null or farbe_dunkel ~ '^#[0-9a-f]{6}$') +); + +alter table public.firmen enable row level security; +-- Supabase vergibt per Default-Privileges alle Rechte an anon/authenticated +-- (siehe information_schema.role_table_grants). Explizit entziehen und nur +-- das Noetige granten. +revoke all on public.firmen from anon, authenticated; +grant select, update on public.firmen to authenticated; + +-- Feste ID, damit Tests und das Layout (Standard-Logo) die Bestandsfirma kennen. +insert into public.firmen (id, name) values ('00000000-0000-0000-0000-00000000f001', 'Widmann'); + +-- Status und ID einer Firma aendert nur der Service-Client (Plattform-Actions). +create or replace function public.schuetze_firmen_status() +returns trigger +language plpgsql +as $$ +begin + if auth.uid() is not null and (new.aktiv is distinct from old.aktiv or new.id is distinct from old.id) then + raise exception 'Status und ID einer Firma kann nur der Plattform-Betreiber aendern' using errcode = '42501'; + end if; + return new; +end; +$$; + +create trigger schuetze_firmen_status + before update on public.firmen + for each row execute function public.schuetze_firmen_status(); + +-- ---------------------------------------------------------- firmen_smtp +-- Kein Grant, keine Policy: nur der Service-Client kommt heran, das +-- (verschluesselte) Passwort verlaesst die REST-API also nie. +create table public.firmen_smtp ( + firma_id uuid primary key references public.firmen (id) on delete cascade, + host text not null, + port int not null, + secure boolean not null default false, + benutzer text, + passwort_verschluesselt text, + absender_adresse text not null, + absender_name text, + updated_at timestamptz not null default now(), + constraint firmen_smtp_port check (port between 1 and 65535) +); + +alter table public.firmen_smtp enable row level security; +revoke all on public.firmen_smtp from anon, authenticated; + +-- --------------------------------------------------------------- profiles +alter table public.profiles add column firma_id uuid references public.firmen (id); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where role <> 'superadmin'; +alter table public.profiles + add constraint profiles_superadmin_ohne_firma check (role <> 'superadmin' or firma_id is null); +create index profiles_firma_id_idx on public.profiles (firma_id); + +-- Firma des angemeldeten Nutzers, nur wenn die Firma aktiv ist. null = kein +-- Zugriff auf Fachdaten (Superadmin, deaktivierte Firma, nicht zugeordnet). +create or replace function public.user_firma_id() +returns uuid +language sql +stable +security definer +set search_path = public +as $$ + select p.firma_id + from public.profiles p + join public.firmen f on f.id = p.firma_id + where p.id = auth.uid() and f.aktiv; +$$; + +-- ------------------------------------------------- firma_id in Fachtabellen +alter table public.orte add column firma_id uuid references public.firmen (id); +alter table public.fahrzeuge add column firma_id uuid references public.firmen (id); +alter table public.auftraege add column firma_id uuid references public.firmen (id); +alter table public.auftrag_fotos add column firma_id uuid references public.firmen (id); +alter table public.arbeitszeiten add column firma_id uuid references public.firmen (id); +alter table public.nutzer_details add column firma_id uuid references public.firmen (id); +alter table public.notification_outbox add column firma_id uuid references public.firmen (id); +alter table public.auftragsnummer_zaehler add column firma_id uuid references public.firmen (id); + +update public.orte set firma_id = '00000000-0000-0000-0000-00000000f001'; +update public.fahrzeuge set firma_id = '00000000-0000-0000-0000-00000000f001'; +update public.auftraege set firma_id = '00000000-0000-0000-0000-00000000f001'; +update public.auftrag_fotos set firma_id = '00000000-0000-0000-0000-00000000f001'; +update public.arbeitszeiten set firma_id = '00000000-0000-0000-0000-00000000f001'; +update public.nutzer_details set firma_id = '00000000-0000-0000-0000-00000000f001'; +update public.notification_outbox set firma_id = '00000000-0000-0000-0000-00000000f001'; +update public.auftragsnummer_zaehler set firma_id = '00000000-0000-0000-0000-00000000f001'; + +alter table public.orte alter column firma_id set not null; +alter table public.fahrzeuge alter column firma_id set not null; +alter table public.auftraege alter column firma_id set not null; +alter table public.auftrag_fotos alter column firma_id set not null; +alter table public.arbeitszeiten alter column firma_id set not null; +alter table public.nutzer_details alter column firma_id set not null; +alter table public.notification_outbox alter column firma_id set not null; +alter table public.auftragsnummer_zaehler alter column firma_id set not null; + +create index orte_firma_id_idx on public.orte (firma_id); +create index fahrzeuge_firma_id_idx on public.fahrzeuge (firma_id); +create index auftraege_firma_id_idx on public.auftraege (firma_id); +create index auftrag_fotos_firma_id_idx on public.auftrag_fotos (firma_id); +create index arbeitszeiten_firma_id_idx on public.arbeitszeiten (firma_id); +create index nutzer_details_firma_id_idx on public.nutzer_details (firma_id); +create index notification_outbox_firma_id_idx on public.notification_outbox (firma_id); + +-- ---------------------------------------------------- Trigger: firma_id setzen +-- Mit Nutzer-Session: immer die eigene Firma (ueberschreibt jeden +-- mitgeschickten Wert). Ohne Session (Service-Client, Migrationen, Tests als +-- postgres): firma_id muss mitgegeben werden. +create or replace function public.firma_aus_sitzung_setzen() +returns trigger +language plpgsql +security definer +set search_path = public +as $$ +begin + if auth.uid() is not null then + new.firma_id := public.user_firma_id(); + if new.firma_id is null then + raise exception 'Keine aktive Firma fuer den angemeldeten Nutzer' using errcode = '42501'; + end if; + elsif new.firma_id is null then + raise exception 'firma_id fehlt' using errcode = '23502'; + end if; + return new; +end; +$$; + +create or replace function public.firma_aus_auftrag_setzen() +returns trigger +language plpgsql +security definer +set search_path = public +as $$ +begin + if new.auftrag_id is not null then + new.firma_id := (select a.firma_id from public.auftraege a where a.id = new.auftrag_id); + end if; + if new.firma_id is null then + raise exception 'firma_id fehlt' using errcode = '23502'; + end if; + return new; +end; +$$; + +create or replace function public.firma_aus_profil_setzen() +returns trigger +language plpgsql +security definer +set search_path = public +as $$ +begin + new.firma_id := (select p.firma_id from public.profiles p where p.id = new.user_id); + if new.firma_id is null then + raise exception 'firma_id fehlt' using errcode = '23502'; + end if; + return new; +end; +$$; + +-- Nur der Erstzuordnung null -> Wert ohne Session (Service-Client nach +-- auth.admin.createUser) ist erlaubt; jede andere Aenderung wirft. +create or replace function public.firma_id_unveraenderlich() +returns trigger +language plpgsql +as $$ +begin + if new.firma_id is distinct from old.firma_id + and not (old.firma_id is null and auth.uid() is null) then + raise exception 'firma_id darf nicht geaendert werden' using errcode = '42501'; + end if; + return new; +end; +$$; + +-- Trigger-Namen mit Praefix a0_/a1_: Postgres feuert gleichartige Trigger +-- alphabetisch, die firma_id muss vor auftragsnummer_generieren stehen. +create trigger a0_firma_setzen before insert on public.orte + for each row execute function public.firma_aus_sitzung_setzen(); +create trigger a0_firma_setzen before insert on public.fahrzeuge + for each row execute function public.firma_aus_sitzung_setzen(); +create trigger a0_firma_setzen before insert on public.auftraege + for each row execute function public.firma_aus_sitzung_setzen(); +create trigger a0_firma_setzen before insert on public.arbeitszeiten + for each row execute function public.firma_aus_sitzung_setzen(); +create trigger a0_firma_setzen before insert on public.auftrag_fotos + for each row execute function public.firma_aus_auftrag_setzen(); +create trigger a0_firma_setzen before insert on public.notification_outbox + for each row execute function public.firma_aus_auftrag_setzen(); +create trigger a0_firma_setzen before insert on public.nutzer_details + for each row execute function public.firma_aus_profil_setzen(); + +create trigger a0_firma_fix before update on public.profiles + for each row execute function public.firma_id_unveraenderlich(); +create trigger a0_firma_fix before update on public.orte + for each row execute function public.firma_id_unveraenderlich(); +create trigger a0_firma_fix before update on public.fahrzeuge + for each row execute function public.firma_id_unveraenderlich(); +create trigger a0_firma_fix before update on public.auftraege + for each row execute function public.firma_id_unveraenderlich(); +create trigger a0_firma_fix before update on public.arbeitszeiten + for each row execute function public.firma_id_unveraenderlich(); +create trigger a0_firma_fix before update on public.auftrag_fotos + for each row execute function public.firma_id_unveraenderlich(); +create trigger a0_firma_fix before update on public.notification_outbox + for each row execute function public.firma_id_unveraenderlich(); +create trigger a0_firma_fix before update on public.nutzer_details + for each row execute function public.firma_id_unveraenderlich(); + +-- ------------------------------------------- Trigger: firmenfremde Verweise +create or replace function public.auftrag_firma_konsistenz() +returns trigger +language plpgsql +security definer +set search_path = public +as $$ +begin + if exists (select 1 from public.orte o + where o.id in (new.abholort_id, new.zielort_id) and o.firma_id <> new.firma_id) + or exists (select 1 from public.fahrzeuge f + where f.id = new.fahrzeug_id and f.firma_id <> new.firma_id) + or exists (select 1 from public.profiles p + where p.id in (new.zugewiesen_an, new.erstellt_von) and p.firma_id is distinct from new.firma_id) + or exists (select 1 from public.auftraege v + where v.id = new.vorgaenger_auftrag_id and v.firma_id <> new.firma_id) then + raise exception 'Firmenfremder Verweis' using errcode = '42501'; + end if; + return new; +end; +$$; + +create trigger a1_firma_konsistenz before insert or update on public.auftraege + for each row execute function public.auftrag_firma_konsistenz(); + +create or replace function public.arbeitszeit_firma_konsistenz() +returns trigger +language plpgsql +security definer +set search_path = public +as $$ +begin + if exists (select 1 from public.profiles p + where p.id in (new.fahrer_id, new.korrigiert_von) and p.firma_id is distinct from new.firma_id) then + raise exception 'Firmenfremder Verweis' using errcode = '42501'; + end if; + return new; +end; +$$; + +create trigger a1_firma_konsistenz before insert or update on public.arbeitszeiten + for each row execute function public.arbeitszeit_firma_konsistenz(); + +-- ------------------------------------------------- Eindeutigkeit pro Firma +alter table public.fahrzeuge + drop constraint fahrzeuge_kennzeichen_key, + drop constraint fahrzeuge_fin_key, + add constraint fahrzeuge_firma_kennzeichen_key unique (firma_id, kennzeichen), + add constraint fahrzeuge_firma_fin_key unique (firma_id, fin); + +alter table public.auftraege + drop constraint auftraege_auftragsnummer_key, + add constraint auftraege_firma_auftragsnummer_key unique (firma_id, auftragsnummer); + +alter table public.auftragsnummer_zaehler drop constraint auftragsnummer_zaehler_pkey; +alter table public.auftragsnummer_zaehler add primary key (firma_id, tag); + +create or replace function public.generiere_auftragsnummer() +returns trigger +language plpgsql +security definer +set search_path = public +as $$ +declare + naechste int; +begin + if new.auftragsnummer is null or new.auftragsnummer = '' then + insert into public.auftragsnummer_zaehler (firma_id, tag, letzte_nummer) + values (new.firma_id, current_date, 1) + on conflict (firma_id, tag) do update set letzte_nummer = auftragsnummer_zaehler.letzte_nummer + 1 + returning letzte_nummer into naechste; + new.auftragsnummer := to_char(current_date, 'YY-MM-DD') || '-' || lpad(naechste::text, 4, '0'); + end if; + return new; +end; +$$; + +-- ------------------------------------------- FIN-Abgleich nur innerhalb der Firma +create or replace function public.fin_bei_fahrtbeginn(p_auftrag_id uuid, p_fin text) +returns public.app_fin_abgleich +language plpgsql +security definer +set search_path = '' +as $$ +declare + v_fin text := upper(btrim(coalesce(p_fin, ''))); + v_fahrzeug_id uuid; + v_firma_id uuid; + v_hinterlegt text; + v_alt_fin text; + v_alt_status public.app_fin_abgleich; + v_status public.app_fin_abgleich; +begin + if public.user_role() is distinct from 'fahrer'::public.app_role then + raise exception 'Nur Fahrer duerfen den FIN-Abgleich durchfuehren'; + end if; + + if v_fin !~ '^[A-HJ-NPR-Z0-9]{17}$' then + raise exception 'Die FIN muss 17 Zeichen lang sein (A–Z, 0–9, ohne I, O, Q).'; + end if; + + select a.fahrzeug_id, a.firma_id, a.start_fin, a.start_fin_status + into v_fahrzeug_id, v_firma_id, v_alt_fin, v_alt_status + from public.auftraege a + where a.id = p_auftrag_id + and a.zugewiesen_an = auth.uid() + and a.firma_id = public.user_firma_id() + and a.status = 'zugewiesen'::public.app_auftrag_status + for update; + + if not found then + raise exception 'Auftrag nicht gefunden oder nicht im Status zugewiesen'; + end if; + + select f.fin into v_hinterlegt + from public.fahrzeuge f + where f.id = v_fahrzeug_id + for update; + + if v_alt_status = 'abweichend' then + -- Sticky: ein einmal vermerkter Abweichungs-Nachweis wird nicht + -- ueberschrieben (auch nicht durch erneuten Aufruf mit der hinterlegten FIN). + return v_alt_status; + end if; + + if v_alt_status = 'nachgetragen' and v_alt_fin = v_fin then + -- Wiederholter Aufruf mit derselben FIN: Ergebnis bleibt 'nachgetragen'. + v_status := 'nachgetragen'; + elsif v_hinterlegt is null then + if exists (select 1 from public.fahrzeuge f + where f.fin = v_fin and f.id <> v_fahrzeug_id and f.firma_id = v_firma_id) then + v_status := 'abweichend'; + else + begin + update public.fahrzeuge set fin = v_fin where id = v_fahrzeug_id; + v_status := 'nachgetragen'; + exception when unique_violation then + -- Race: parallel wurde dieselbe FIN einem anderen Fahrzeug gegeben. + v_status := 'abweichend'; + end; + end if; + elsif v_hinterlegt = v_fin then + v_status := 'bestaetigt'; + else + v_status := 'abweichend'; + end if; + + perform set_config('app.fin_abgleich', 'an', true); + update public.auftraege + set start_fin = v_fin, start_fin_status = v_status + where id = p_auftrag_id; + perform set_config('app.fin_abgleich', '', true); + + return v_status; +end; +$$; + +-- ------------------------------------- WhatsApp nur bei Firma mit Empfaenger +create or replace function public.auftrag_notification_enqueue() +returns trigger +language plpgsql +security definer +set search_path = public +as $$ +begin + insert into public.notification_outbox (event_type, auftrag_id, payload) + select + 'auftrag_erstellt', + new.id, + jsonb_build_object( + 'auftragsnummer', new.auftragsnummer, + 'abholort_adresse', abhol.adresse, + 'zielort_adresse', ziel.adresse, + 'kennzeichen', coalesce(fz.kennzeichen, fz.fin)) + from public.orte abhol + join public.orte ziel on ziel.id = new.zielort_id + join public.fahrzeuge fz on fz.id = new.fahrzeug_id + join public.firmen fi on fi.id = new.firma_id + where abhol.id = new.abholort_id + and fi.whatsapp_empfaenger is not null; + return new; +end; +$$; diff --git a/supabase/tests/arbeitszeiten_rls_test.sql b/supabase/tests/arbeitszeiten_rls_test.sql index caa2e11..7a79cdc 100644 --- a/supabase/tests/arbeitszeiten_rls_test.sql +++ b/supabase/tests/arbeitszeiten_rls_test.sql @@ -8,6 +8,7 @@ insert into auth.users (id, email) values ('aa444444-4444-4444-4444-444444444444', 'fahrer-zwei@arbeitszeiten.test.local'), ('aa555555-5555-5555-5555-555555555555', 'readonly@arbeitszeiten.test.local'); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; update public.profiles set role = 'admin' where id = 'aa111111-1111-1111-1111-111111111111'; update public.profiles set role = 'disponent' where id = 'aa222222-2222-2222-2222-222222222222'; update public.profiles set role = 'fahrer' where id = 'aa333333-3333-3333-3333-333333333333'; @@ -74,8 +75,8 @@ select throws_ok( reset role; -insert into public.arbeitszeiten (id, fahrer_id, kommt_um) values - ('b2222222-2222-2222-2222-222222222222', 'aa444444-4444-4444-4444-444444444444', '2026-07-13 07:00:00+00'); +insert into public.arbeitszeiten (id, fahrer_id, kommt_um, firma_id) values + ('b2222222-2222-2222-2222-222222222222', 'aa444444-4444-4444-4444-444444444444', '2026-07-13 07:00:00+00', '00000000-0000-0000-0000-00000000f001'); set local role authenticated; set local request.jwt.claims to '{"sub": "aa333333-3333-3333-3333-333333333333", "role": "authenticated"}'; diff --git a/supabase/tests/auftraege_rls_test.sql b/supabase/tests/auftraege_rls_test.sql index 2a01208..7674a46 100644 --- a/supabase/tests/auftraege_rls_test.sql +++ b/supabase/tests/auftraege_rls_test.sql @@ -8,6 +8,7 @@ insert into auth.users (id, email) values ('f4444444-4444-4444-4444-444444444444', 'fahrer-zwei@auftraege.test.local'), ('05555555-5555-5555-5555-555555555555', 'readonly@auftraege.test.local'); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; update public.profiles set role = 'admin' where id = 'a1111111-1111-1111-1111-111111111111'; update public.profiles set role = 'disponent' where id = 'd2222222-2222-2222-2222-222222222222'; update public.profiles set role = 'fahrer' where id = 'f3333333-3333-3333-3333-333333333333'; @@ -15,13 +16,13 @@ update public.profiles set role = 'fahrer' where id = 'f4444444-4444-4444-4444-4 -- readonly bleibt auf dem Trigger-Default 'readonly' (siehe Fundament, -- handle_new_user()), kein explizites update noetig. -insert into public.orte (id, adresse, ansprechpartner) values - ('01111111-1111-1111-1111-111111111111', 'Hauptstr. 1, Musterstadt', 'Frau Muster'), - ('02222222-2222-2222-2222-222222222222', 'Nebenweg 2, Musterstadt', 'Herr Beispiel'); +insert into public.orte (id, adresse, ansprechpartner, firma_id) values + ('01111111-1111-1111-1111-111111111111', 'Hauptstr. 1, Musterstadt', 'Frau Muster', '00000000-0000-0000-0000-00000000f001'), + ('02222222-2222-2222-2222-222222222222', 'Nebenweg 2, Musterstadt', 'Herr Beispiel', '00000000-0000-0000-0000-00000000f001'); -insert into public.fahrzeuge (id, kennzeichen) values - ('f0000000-0000-0000-0000-000000000001', 'M-AB 123'), - ('f0000000-0000-0000-0000-000000000002', 'M-CD 456'); +insert into public.fahrzeuge (id, kennzeichen, firma_id) values + ('f0000000-0000-0000-0000-000000000001', 'M-AB 123', '00000000-0000-0000-0000-00000000f001'), + ('f0000000-0000-0000-0000-000000000002', 'M-CD 456', '00000000-0000-0000-0000-00000000f001'); set local role authenticated; set local request.jwt.claims to '{"sub": "d2222222-2222-2222-2222-222222222222", "role": "authenticated"}'; diff --git a/supabase/tests/auftrag_fotos_rls_test.sql b/supabase/tests/auftrag_fotos_rls_test.sql index 2ca0d78..206d6d5 100644 --- a/supabase/tests/auftrag_fotos_rls_test.sql +++ b/supabase/tests/auftrag_fotos_rls_test.sql @@ -7,23 +7,24 @@ insert into auth.users (id, email) values ('ff333333-3333-3333-3333-333333333333', 'fahrer-eins-fotos@auftraege.test.local'), ('ff444444-4444-4444-4444-444444444444', 'fahrer-zwei-fotos@auftraege.test.local'); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; update public.profiles set role = 'admin' where id = 'aa111111-1111-1111-1111-111111111111'; update public.profiles set role = 'disponent' where id = 'dd222222-2222-2222-2222-222222222222'; update public.profiles set role = 'fahrer' where id = 'ff333333-3333-3333-3333-333333333333'; update public.profiles set role = 'fahrer' where id = 'ff444444-4444-4444-4444-444444444444'; -insert into public.orte (id, adresse, ansprechpartner) values - ('20111111-1111-1111-1111-111111111111', 'Hauptstr. 1, Musterstadt', 'Frau Muster'), - ('20222222-2222-2222-2222-222222222222', 'Nebenweg 2, Musterstadt', 'Herr Beispiel'); +insert into public.orte (id, adresse, ansprechpartner, firma_id) values + ('20111111-1111-1111-1111-111111111111', 'Hauptstr. 1, Musterstadt', 'Frau Muster', '00000000-0000-0000-0000-00000000f001'), + ('20222222-2222-2222-2222-222222222222', 'Nebenweg 2, Musterstadt', 'Herr Beispiel', '00000000-0000-0000-0000-00000000f001'); -insert into public.fahrzeuge (id, kennzeichen) values - ('20333333-0000-0000-0000-000000000001', 'M-EF 789'); +insert into public.fahrzeuge (id, kennzeichen, firma_id) values + ('20333333-0000-0000-0000-000000000001', 'M-EF 789', '00000000-0000-0000-0000-00000000f001'); -- Auftrag A: dem Fahrer "eins" zugewiesen, status = 'zugewiesen' -- fuer die -- "eigener, noch nicht gestarteter Auftrag"-Faelle (Pflichtfoto UND -- Schadensfoto duerfen hier hochgeladen werden). insert into public.auftraege ( - id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km + id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km, firma_id ) values ( '20000000-0000-0000-0000-000000000001', '20111111-1111-1111-1111-111111111111', @@ -32,13 +33,14 @@ insert into public.auftraege ( 'dd222222-2222-2222-2222-222222222222', 'zugewiesen', 'ff333333-3333-3333-3333-333333333333', - 1000 + 1000, + '00000000-0000-0000-0000-00000000f001' ); -- Auftrag B: dem Fahrer "zwei" zugewiesen -- aus Sicht von Fahrer "eins" ein -- fremder Auftrag, fuer die SELECT- und INSERT-RLS-Blockade-Faelle. insert into public.auftraege ( - id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km + id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km, firma_id ) values ( '20000000-0000-0000-0000-000000000002', '20111111-1111-1111-1111-111111111111', @@ -47,7 +49,8 @@ insert into public.auftraege ( 'dd222222-2222-2222-2222-222222222222', 'zugewiesen', 'ff444444-4444-4444-4444-444444444444', - 500 + 500, + '00000000-0000-0000-0000-00000000f001' ); -- Auftrag C: dem Fahrer "eins" zugewiesen, aber direkt mit status = @@ -58,7 +61,7 @@ insert into public.auftraege ( -- unabhaengig von auftraege_rls_test.sql und fokussiert sie ausschliesslich -- auf die auftrag_fotos/storage.objects-Policies statt auf den Trigger. insert into public.auftraege ( - id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km + id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km, firma_id ) values ( '20000000-0000-0000-0000-000000000003', '20111111-1111-1111-1111-111111111111', @@ -67,7 +70,8 @@ insert into public.auftraege ( 'dd222222-2222-2222-2222-222222222222', 'unterwegs', 'ff333333-3333-3333-3333-333333333333', - 2000 + 2000, + '00000000-0000-0000-0000-00000000f001' ); -- Ein Foto + ein Storage-Objekt fuer Auftrag B, als postgres (RLS-frei) diff --git a/supabase/tests/auftrag_unterschrift_rls_test.sql b/supabase/tests/auftrag_unterschrift_rls_test.sql index 96dcfa1..7052e92 100644 --- a/supabase/tests/auftrag_unterschrift_rls_test.sql +++ b/supabase/tests/auftrag_unterschrift_rls_test.sql @@ -6,20 +6,21 @@ insert into auth.users (id, email) values ('ff666666-6666-6666-6666-666666666666', 'fahrer-eins-unterschrift@auftraege.test.local'), ('ff777777-7777-7777-7777-777777777777', 'fahrer-zwei-unterschrift@auftraege.test.local'); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; update public.profiles set role = 'admin' where id = 'aa555555-5555-5555-5555-555555555555'; update public.profiles set role = 'fahrer' where id = 'ff666666-6666-6666-6666-666666666666'; update public.profiles set role = 'fahrer' where id = 'ff777777-7777-7777-7777-777777777777'; -insert into public.orte (id, adresse, ansprechpartner) values - ('21111111-1111-1111-1111-111111111111', 'Hauptstr. 1, Musterstadt', 'Frau Muster'), - ('21222222-2222-2222-2222-222222222222', 'Nebenweg 2, Musterstadt', 'Herr Beispiel'); +insert into public.orte (id, adresse, ansprechpartner, firma_id) values + ('21111111-1111-1111-1111-111111111111', 'Hauptstr. 1, Musterstadt', 'Frau Muster', '00000000-0000-0000-0000-00000000f001'), + ('21222222-2222-2222-2222-222222222222', 'Nebenweg 2, Musterstadt', 'Herr Beispiel', '00000000-0000-0000-0000-00000000f001'); -insert into public.fahrzeuge (id, kennzeichen) values - ('21333333-0000-0000-0000-000000000001', 'M-UN 001'); +insert into public.fahrzeuge (id, kennzeichen, firma_id) values + ('21333333-0000-0000-0000-000000000001', 'M-UN 001', '00000000-0000-0000-0000-00000000f001'); -- Auftrag A: Fahrer "eins", status 'unterwegs' -- Upload muss hier gelingen. insert into public.auftraege ( - id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km + id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km, firma_id ) values ( '21000000-0000-0000-0000-000000000001', '21111111-1111-1111-1111-111111111111', @@ -28,13 +29,14 @@ insert into public.auftraege ( 'aa555555-5555-5555-5555-555555555555', 'unterwegs', 'ff666666-6666-6666-6666-666666666666', - 1000 + 1000, + '00000000-0000-0000-0000-00000000f001' ); -- Auftrag B: Fahrer "eins", status 'zugewiesen' -- Upload muss hier verboten -- bleiben (Unterschrift ist nur bei Fahrtende/status='unterwegs' erlaubt). insert into public.auftraege ( - id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km + id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km, firma_id ) values ( '21000000-0000-0000-0000-000000000002', '21111111-1111-1111-1111-111111111111', @@ -43,13 +45,14 @@ insert into public.auftraege ( 'aa555555-5555-5555-5555-555555555555', 'zugewiesen', 'ff666666-6666-6666-6666-666666666666', - 1000 + 1000, + '00000000-0000-0000-0000-00000000f001' ); -- Auftrag C: Fahrer "zwei", status 'unterwegs' -- aus Sicht von Fahrer -- "eins" ein fremder Auftrag. insert into public.auftraege ( - id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km + id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km, firma_id ) values ( '21000000-0000-0000-0000-000000000003', '21111111-1111-1111-1111-111111111111', @@ -58,7 +61,8 @@ insert into public.auftraege ( 'aa555555-5555-5555-5555-555555555555', 'unterwegs', 'ff777777-7777-7777-7777-777777777777', - 500 + 500, + '00000000-0000-0000-0000-00000000f001' ); insert into storage.buckets (id, name, public) values ('auftrag-unterschriften', 'auftrag-unterschriften', false) diff --git a/supabase/tests/auftragsnummer_test.sql b/supabase/tests/auftragsnummer_test.sql index 0177726..4397aca 100644 --- a/supabase/tests/auftragsnummer_test.sql +++ b/supabase/tests/auftragsnummer_test.sql @@ -3,14 +3,15 @@ select plan(5); insert into auth.users (id, email) values ('a1111111-1111-1111-1111-111111111111', 'admin@auftragsnummer.test.local'); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; update public.profiles set role = 'admin' where id = 'a1111111-1111-1111-1111-111111111111'; -insert into public.orte (id, adresse, ansprechpartner) values - ('01111111-1111-1111-1111-111111111111', 'Hauptstr. 1, Musterstadt', 'Frau Muster'), - ('02222222-2222-2222-2222-222222222222', 'Nebenweg 2, Musterstadt', 'Herr Beispiel'); +insert into public.orte (id, adresse, ansprechpartner, firma_id) values + ('01111111-1111-1111-1111-111111111111', 'Hauptstr. 1, Musterstadt', 'Frau Muster', '00000000-0000-0000-0000-00000000f001'), + ('02222222-2222-2222-2222-222222222222', 'Nebenweg 2, Musterstadt', 'Herr Beispiel', '00000000-0000-0000-0000-00000000f001'); -insert into public.fahrzeuge (id, kennzeichen) values - ('f0000000-0000-0000-0000-000000000001', 'M-AB 123'); +insert into public.fahrzeuge (id, kennzeichen, firma_id) values + ('f0000000-0000-0000-0000-000000000001', 'M-AB 123', '00000000-0000-0000-0000-00000000f001'); set local role authenticated; set local request.jwt.claims to '{"sub": "a1111111-1111-1111-1111-111111111111", "role": "authenticated"}'; @@ -55,7 +56,7 @@ select throws_ok( ('30000000-0000-0000-0000-000000000004', '01111111-1111-1111-1111-111111111111', '02222222-2222-2222-2222-222222222222', 'f0000000-0000-0000-0000-000000000001', 'a1111111-1111-1111-1111-111111111111', 'MEINE-EIGENE-NUMMER') $$, '23505', - 'duplicate key value violates unique constraint "auftraege_auftragsnummer_key"', + 'duplicate key value violates unique constraint "auftraege_firma_auftragsnummer_key"', 'Doppelte, manuell eingegebene Auftragsnummer wird durch den Unique-Constraint abgelehnt' ); diff --git a/supabase/tests/fahrzeug_fin_test.sql b/supabase/tests/fahrzeug_fin_test.sql index f6f9909..2843a3b 100644 --- a/supabase/tests/fahrzeug_fin_test.sql +++ b/supabase/tests/fahrzeug_fin_test.sql @@ -8,27 +8,29 @@ insert into auth.users (id, email) values ('f1a00000-0000-0000-0000-000000000004', 'fahrer-zwei-fin@test.local'), ('f1a00000-0000-0000-0000-000000000005', 'readonly-fin@test.local'); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; +update public.firmen set whatsapp_empfaenger = '+4915100000001' where id = '00000000-0000-0000-0000-00000000f001'; update public.profiles set role = 'admin' where id = 'f1a00000-0000-0000-0000-000000000001'; update public.profiles set role = 'disponent' where id = 'f1a00000-0000-0000-0000-000000000002'; update public.profiles set role = 'fahrer' where id = 'f1a00000-0000-0000-0000-000000000003'; update public.profiles set role = 'fahrer' where id = 'f1a00000-0000-0000-0000-000000000004'; -- readonly bleibt auf dem Trigger-Default. -insert into public.orte (id, adresse, ansprechpartner) values - ('f1b00000-0000-0000-0000-000000000001', 'Hauptstr. 1, Musterstadt', 'Frau Muster'), - ('f1b00000-0000-0000-0000-000000000002', 'Nebenweg 2, Musterstadt', 'Herr Beispiel'); +insert into public.orte (id, adresse, ansprechpartner, firma_id) values + ('f1b00000-0000-0000-0000-000000000001', 'Hauptstr. 1, Musterstadt', 'Frau Muster', '00000000-0000-0000-0000-00000000f001'), + ('f1b00000-0000-0000-0000-000000000002', 'Nebenweg 2, Musterstadt', 'Herr Beispiel', '00000000-0000-0000-0000-00000000f001'); -- F1: nur Kennzeichen (FIN wird nachgetragen) -- F2: FIN A hinterlegt -- F3: nur Kennzeichen (Fahrer gibt FIN von F2 ein) -insert into public.fahrzeuge (id, kennzeichen, fin) values - ('f1c00000-0000-0000-0000-000000000001', 'M-FN 001', null), - ('f1c00000-0000-0000-0000-000000000002', 'M-FN 002', 'WDB9066331S123456'), - ('f1c00000-0000-0000-0000-000000000003', 'M-FN 003', null); +insert into public.fahrzeuge (id, kennzeichen, fin, firma_id) values + ('f1c00000-0000-0000-0000-000000000001', 'M-FN 001', null, '00000000-0000-0000-0000-00000000f001'), + ('f1c00000-0000-0000-0000-000000000002', 'M-FN 002', 'WDB9066331S123456', '00000000-0000-0000-0000-00000000f001'), + ('f1c00000-0000-0000-0000-000000000003', 'M-FN 003', null, '00000000-0000-0000-0000-00000000f001'); -- 1) Format-Check select throws_ok( - $$ insert into public.fahrzeuge (kennzeichen, fin) values ('M-FN 010', 'WDB906633IS12345X') $$, + $$ insert into public.fahrzeuge (kennzeichen, fin, firma_id) values ('M-FN 010', 'WDB906633IS12345X', '00000000-0000-0000-0000-00000000f001') $$, '23514', null, 'FIN mit I wird vom Format-Check abgelehnt' @@ -36,7 +38,7 @@ select throws_ok( -- 2) Kennzeichen oder FIN select throws_ok( - $$ insert into public.fahrzeuge (kennzeichen, fin) values (null, null) $$, + $$ insert into public.fahrzeuge (kennzeichen, fin, firma_id) values (null, null, '00000000-0000-0000-0000-00000000f001') $$, '23514', null, 'Fahrzeug ohne Kennzeichen und ohne FIN wird abgelehnt' @@ -44,27 +46,27 @@ select throws_ok( -- 3) Nur FIN ist erlaubt select lives_ok( - $$ insert into public.fahrzeuge (id, kennzeichen, fin) values ('f1c00000-0000-0000-0000-000000000004', null, 'VF1RFB00X12345678') $$, + $$ insert into public.fahrzeuge (id, kennzeichen, fin, firma_id) values ('f1c00000-0000-0000-0000-000000000004', null, 'VF1RFB00X12345678', '00000000-0000-0000-0000-00000000f001') $$, 'Fahrzeug nur mit FIN kann angelegt werden' ); -- 4) FIN eindeutig select throws_ok( - $$ insert into public.fahrzeuge (kennzeichen, fin) values ('M-FN 011', 'WDB9066331S123456') $$, + $$ insert into public.fahrzeuge (kennzeichen, fin, firma_id) values ('M-FN 011', 'WDB9066331S123456', '00000000-0000-0000-0000-00000000f001') $$, '23505', null, 'Doppelte FIN wird abgelehnt' ); -- Auftraege (als postgres angelegt) -insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km) values - ('f1d00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000001', 'f1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'f1a00000-0000-0000-0000-000000000003', null), - ('f1d00000-0000-0000-0000-000000000002', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000002', 'f1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'f1a00000-0000-0000-0000-000000000003', null), - ('f1d00000-0000-0000-0000-000000000003', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000002', 'f1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'f1a00000-0000-0000-0000-000000000003', null), - ('f1d00000-0000-0000-0000-000000000004', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000003', 'f1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'f1a00000-0000-0000-0000-000000000003', null), - ('f1d00000-0000-0000-0000-000000000005', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000002', 'f1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'f1a00000-0000-0000-0000-000000000004', null), - ('f1d00000-0000-0000-0000-000000000006', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000002', 'f1a00000-0000-0000-0000-000000000001', 'unterwegs', 'f1a00000-0000-0000-0000-000000000003', 1000), - ('f1d00000-0000-0000-0000-000000000007', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000004', 'f1a00000-0000-0000-0000-000000000001', 'offen', null, null); +insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km, firma_id) values + ('f1d00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000001', 'f1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'f1a00000-0000-0000-0000-000000000003', null, '00000000-0000-0000-0000-00000000f001'), + ('f1d00000-0000-0000-0000-000000000002', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000002', 'f1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'f1a00000-0000-0000-0000-000000000003', null, '00000000-0000-0000-0000-00000000f001'), + ('f1d00000-0000-0000-0000-000000000003', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000002', 'f1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'f1a00000-0000-0000-0000-000000000003', null, '00000000-0000-0000-0000-00000000f001'), + ('f1d00000-0000-0000-0000-000000000004', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000003', 'f1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'f1a00000-0000-0000-0000-000000000003', null, '00000000-0000-0000-0000-00000000f001'), + ('f1d00000-0000-0000-0000-000000000005', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000002', 'f1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'f1a00000-0000-0000-0000-000000000004', null, '00000000-0000-0000-0000-00000000f001'), + ('f1d00000-0000-0000-0000-000000000006', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000002', 'f1a00000-0000-0000-0000-000000000001', 'unterwegs', 'f1a00000-0000-0000-0000-000000000003', 1000, '00000000-0000-0000-0000-00000000f001'), + ('f1d00000-0000-0000-0000-000000000007', 'f1b00000-0000-0000-0000-000000000001', 'f1b00000-0000-0000-0000-000000000002', 'f1c00000-0000-0000-0000-000000000004', 'f1a00000-0000-0000-0000-000000000001', 'offen', null, null, '00000000-0000-0000-0000-00000000f001'); -- 5) Outbox: Fahrzeug ohne Kennzeichen -> FIN im Payload select is( diff --git a/supabase/tests/mandanten_struktur_test.sql b/supabase/tests/mandanten_struktur_test.sql new file mode 100644 index 0000000..96fb37a --- /dev/null +++ b/supabase/tests/mandanten_struktur_test.sql @@ -0,0 +1,181 @@ +begin; +select plan(16); + +-- Firma B neben der Bestandsfirma Widmann (f001) +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@mandant.test.local'), + ('a0000000-0000-0000-0000-0000000000a3', 'fahrer-a@mandant.test.local'), + ('b0000000-0000-0000-0000-0000000000b1', 'admin-b@mandant.test.local'), + ('b0000000-0000-0000-0000-0000000000b3', 'fahrer-b@mandant.test.local'), + ('5a000000-0000-0000-0000-000000000001', 'superadmin@mandant.test.local'); + +-- 1) Neues Profil hat keine Firma (handle_new_user bleibt unveraendert) +select is( + (select firma_id from public.profiles where id = 'a0000000-0000-0000-0000-0000000000a1'), + null::uuid, + 'Neues Profil startet ohne Firma' +); + +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 = '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 = 'fahrer' + where id = 'b0000000-0000-0000-0000-0000000000b3'; +update public.profiles set role = 'superadmin' where id = '5a000000-0000-0000-0000-000000000001'; + +-- 2) Superadmin mit Firma ist verboten +select throws_ok( + $$ update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' + where id = '5a000000-0000-0000-0000-000000000001' $$, + '23514', null, + 'Superadmin darf keiner Firma angehoeren' +); + +-- 3) Firmenwechsel eines Profils ist verboten (auch ohne Session, wenn schon gesetzt) +select throws_ok( + $$ update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f002' + where id = 'a0000000-0000-0000-0000-0000000000a3' $$, + '42501', 'firma_id darf nicht geaendert werden', + 'firma_id eines Profils ist unveraenderlich' +); + +-- Stammdaten ohne Session: firma_id explizit +insert into public.orte (id, adresse, ansprechpartner, firma_id) values + ('a0000000-0000-0000-0000-0000000000c1', 'Ort A', 'Herr A', '00000000-0000-0000-0000-00000000f001'), + ('b0000000-0000-0000-0000-0000000000c1', 'Ort B', 'Frau B', '00000000-0000-0000-0000-00000000f002'); +insert into public.fahrzeuge (id, kennzeichen, firma_id) values + ('a0000000-0000-0000-0000-0000000000d1', 'MT-AB 1', '00000000-0000-0000-0000-00000000f001'); + +-- 4) Ohne Session und ohne firma_id: Fehler +select throws_ok( + $$ insert into public.orte (adresse, ansprechpartner) values ('X', 'Y') $$, + '23502', 'firma_id fehlt', + 'Insert ohne Session braucht eine firma_id' +); + +-- 5) Gleiches Kennzeichen in zwei Firmen ist erlaubt +select lives_ok( + $$ insert into public.fahrzeuge (id, kennzeichen, firma_id) values + ('b0000000-0000-0000-0000-0000000000d1', 'MT-AB 1', '00000000-0000-0000-0000-00000000f002') $$, + 'Gleiches Kennzeichen in einer anderen Firma ist erlaubt' +); + +-- 6) Doppeltes Kennzeichen in derselben Firma nicht +select throws_ok( + $$ insert into public.fahrzeuge (kennzeichen, firma_id) values ('MT-AB 1', '00000000-0000-0000-0000-00000000f001') $$, + '23505', null, + 'Doppeltes Kennzeichen in derselben Firma scheitert' +); + +-- Session als Admin B +set local role authenticated; +set local request.jwt.claims to '{"sub": "b0000000-0000-0000-0000-0000000000b1", "role": "authenticated"}'; + +-- 7) Mitgeschickte fremde firma_id wird ueberschrieben +select is( + (select public.user_firma_id()), + '00000000-0000-0000-0000-00000000f002'::uuid, + 'user_firma_id liefert die Firma des angemeldeten Nutzers' +); + +reset role; +reset request.jwt.claims; + +-- Insert mit Session, Rueckgabe ueber eine Hilfstabelle (RLS-Policies sind in Task 1 noch rollenbasiert) +set local role authenticated; +set local request.jwt.claims to '{"sub": "b0000000-0000-0000-0000-0000000000b1", "role": "authenticated"}'; +insert into public.orte (id, adresse, ansprechpartner, firma_id) + values ('b0000000-0000-0000-0000-0000000000c2', 'Ort B2', 'Herr B2', '00000000-0000-0000-0000-00000000f001'); +reset role; +reset request.jwt.claims; + +select is( + (select firma_id from public.orte where id = 'b0000000-0000-0000-0000-0000000000c2'), + '00000000-0000-0000-0000-00000000f002'::uuid, + 'Mitgeschickte fremde firma_id wird durch die eigene Firma ersetzt' +); + +-- 9) Auftrag mit Ort einer fremden Firma: Exception +select throws_ok( + $$ insert into public.auftraege (abholort_id, zielort_id, fahrzeug_id, erstellt_von, firma_id) values + ('a0000000-0000-0000-0000-0000000000c1', 'b0000000-0000-0000-0000-0000000000c1', + 'b0000000-0000-0000-0000-0000000000d1', 'b0000000-0000-0000-0000-0000000000b1', + '00000000-0000-0000-0000-00000000f002') $$, + '42501', 'Firmenfremder Verweis', + 'Auftrag mit Ort einer fremden Firma scheitert' +); + +-- 10) Auftrag mit Fahrer einer fremden Firma: Exception +select throws_ok( + $$ insert into public.auftraege (abholort_id, zielort_id, fahrzeug_id, erstellt_von, zugewiesen_an, status, firma_id) values + ('b0000000-0000-0000-0000-0000000000c1', 'b0000000-0000-0000-0000-0000000000c1', + 'b0000000-0000-0000-0000-0000000000d1', 'b0000000-0000-0000-0000-0000000000b1', + 'a0000000-0000-0000-0000-0000000000a3', 'zugewiesen', '00000000-0000-0000-0000-00000000f002') $$, + '42501', 'Firmenfremder Verweis', + 'Auftrag mit Fahrer einer fremden Firma scheitert' +); + +-- Je ein gueltiger Auftrag pro Firma +update public.firmen set whatsapp_empfaenger = '+4915100000001' where id = '00000000-0000-0000-0000-00000000f001'; +insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, 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', '00000000-0000-0000-0000-00000000f001'), + ('b0000000-0000-0000-0000-0000000000e1', 'b0000000-0000-0000-0000-0000000000c1', 'b0000000-0000-0000-0000-0000000000c1', + 'b0000000-0000-0000-0000-0000000000d1', 'b0000000-0000-0000-0000-0000000000b1', '00000000-0000-0000-0000-00000000f002'); + +-- 11) Auftragsnummer zaehlt pro Firma: der erste B-Auftrag des Tages endet auf -0001 +select is( + (select right(auftragsnummer, 4) from public.auftraege where id = 'b0000000-0000-0000-0000-0000000000e1'), + '0001', + 'Auftragsnummer zaehlt pro Firma' +); + +-- 12) firma_id eines Auftrags ist unveraenderlich +select throws_ok( + $$ update public.auftraege set firma_id = '00000000-0000-0000-0000-00000000f002' + where id = 'a0000000-0000-0000-0000-0000000000e1' $$, + '42501', 'firma_id darf nicht geaendert werden', + 'firma_id eines Auftrags ist unveraenderlich' +); + +-- 13) WhatsApp-Outbox nur fuer Firma mit Empfaenger, mit firma_id aus dem Auftrag +select is( + (select count(*) from public.notification_outbox + where event_type = 'auftrag_erstellt' + and auftrag_id = 'a0000000-0000-0000-0000-0000000000e1' + and firma_id = '00000000-0000-0000-0000-00000000f001'), + 1::bigint, + 'Firma mit WhatsApp-Empfaenger bekommt eine Outbox-Zeile mit ihrer firma_id' +); +select is( + (select count(*) from public.notification_outbox where auftrag_id = 'b0000000-0000-0000-0000-0000000000e1'), + 0::bigint, + 'Firma ohne WhatsApp-Empfaenger bekommt keine Outbox-Zeile' +); + +-- 15) Mit Nutzer-Session kann aktiv nicht geaendert werden. Nur Claims setzen, +-- Rolle bleibt postgres: in Task 1 gibt es noch keine Policy auf firmen, der +-- Trigger soll aber trotzdem greifen. +set local request.jwt.claims to '{"sub": "b0000000-0000-0000-0000-0000000000b1", "role": "authenticated"}'; +select throws_ok( + $$ update public.firmen set aktiv = false where id = '00000000-0000-0000-0000-00000000f002' $$, + '42501', 'Status und ID einer Firma kann nur der Plattform-Betreiber aendern', + 'Admin kann seine Firma nicht deaktivieren' +); +reset request.jwt.claims; + +-- 16) Deaktivierte Firma: user_firma_id() liefert null +update public.firmen set aktiv = false where id = '00000000-0000-0000-0000-00000000f002'; +set local role authenticated; +set local request.jwt.claims to '{"sub": "b0000000-0000-0000-0000-0000000000b1", "role": "authenticated"}'; +select is((select public.user_firma_id()), null::uuid, 'Deaktivierte Firma: user_firma_id() ist null'); +reset role; +reset request.jwt.claims; + +select * from finish(); +rollback; diff --git a/supabase/tests/notification_outbox_rls_test.sql b/supabase/tests/notification_outbox_rls_test.sql index 45002da..ca460c8 100644 --- a/supabase/tests/notification_outbox_rls_test.sql +++ b/supabase/tests/notification_outbox_rls_test.sql @@ -5,15 +5,17 @@ insert into auth.users (id, email) values ('c1111111-1111-1111-1111-111111111111', 'admin-outbox@auftraege.test.local'), ('c2222222-2222-2222-2222-222222222222', 'disponent-outbox@auftraege.test.local'); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; +update public.firmen set whatsapp_empfaenger = '+4915100000001' where id = '00000000-0000-0000-0000-00000000f001'; update public.profiles set role = 'admin' where id = 'c1111111-1111-1111-1111-111111111111'; update public.profiles set role = 'disponent' where id = 'c2222222-2222-2222-2222-222222222222'; -insert into public.orte (id, adresse, ansprechpartner) values - ('c3111111-1111-1111-1111-111111111111', 'Startstr. 1, Teststadt', 'Frau A'), - ('c3222222-2222-2222-2222-222222222222', 'Zielstr. 2, Teststadt', 'Herr B'); +insert into public.orte (id, adresse, ansprechpartner, firma_id) values + ('c3111111-1111-1111-1111-111111111111', 'Startstr. 1, Teststadt', 'Frau A', '00000000-0000-0000-0000-00000000f001'), + ('c3222222-2222-2222-2222-222222222222', 'Zielstr. 2, Teststadt', 'Herr B', '00000000-0000-0000-0000-00000000f001'); -insert into public.fahrzeuge (id, kennzeichen) values - ('c3333333-0000-0000-0000-000000000001', 'S-WA 42'); +insert into public.fahrzeuge (id, kennzeichen, firma_id) values + ('c3333333-0000-0000-0000-000000000001', 'S-WA 42', '00000000-0000-0000-0000-00000000f001'); set local role authenticated; set local request.jwt.claims to '{"sub": "c2222222-2222-2222-2222-222222222222", "role": "authenticated"}'; @@ -105,20 +107,20 @@ delete from public.notification_outbox where auftrag_id = 'c4000000-0000-0000-0000-00000000000a'; insert into public.notification_outbox - (id, event_type, status, payload, naechster_versuch_ab, updated_at) + (id, event_type, status, payload, naechster_versuch_ab, updated_at, firma_id) values -- faellig: pending, Zeitpunkt erreicht ('c5000000-0000-0000-0000-000000000001', 'auftrag_erstellt', 'pending', '{}'::jsonb, - now() - interval '1 minute', now()), + now() - interval '1 minute', now(), '00000000-0000-0000-0000-00000000f001'), -- nicht faellig: failed, Backoff noch in der Zukunft ('c5000000-0000-0000-0000-000000000002', 'auftrag_erstellt', 'failed', '{}'::jsonb, - now() + interval '1 hour', now()), + now() + interval '1 hour', now(), '00000000-0000-0000-0000-00000000f001'), -- nicht reclaimt: sending, erst vor 2 Minuten uebernommen ('c5000000-0000-0000-0000-000000000003', 'auftrag_erstellt', 'sending', '{}'::jsonb, - now(), now() - interval '2 minutes'), + now(), now() - interval '2 minutes', '00000000-0000-0000-0000-00000000f001'), -- reclaimt: sending, seit 20 Minuten haengengeblieben ('c5000000-0000-0000-0000-000000000004', 'auftrag_erstellt', 'sending', '{}'::jsonb, - now(), now() - interval '20 minutes'); + now(), now() - interval '20 minutes', '00000000-0000-0000-0000-00000000f001'); select results_eq( $$ select id::text from public.claim_notification_batch(10, array['auftrag_erstellt']) order by id $$, @@ -135,7 +137,7 @@ set local request.jwt.claims to '{"sub": "c2222222-2222-2222-2222-222222222222", -- wuerde die Beschreibung als erwartete Fehlermeldung interpretieren. Geprueft wird -- nur, dass der INSERT in SQLSTATE 42501 (RLS: keine INSERT-Policy) laeuft. select throws_ok( - $$ insert into public.notification_outbox (event_type, payload) values ('x', '{}'::jsonb) $$, + $$ insert into public.notification_outbox (event_type, payload, firma_id) values ('x', '{}'::jsonb, '00000000-0000-0000-0000-00000000f001') $$, '42501' ); diff --git a/supabase/tests/nutzer_details_rls_test.sql b/supabase/tests/nutzer_details_rls_test.sql index 27d63bb..6bc0558 100644 --- a/supabase/tests/nutzer_details_rls_test.sql +++ b/supabase/tests/nutzer_details_rls_test.sql @@ -7,6 +7,7 @@ insert into auth.users (id, email) values ('33333333-3333-3333-3333-333333333333', 'fahrer-a@test.local'), ('44444444-4444-4444-4444-444444444444', 'fahrer-b@test.local'); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; update public.profiles set role = 'admin' where id = '11111111-1111-1111-1111-111111111111'; update public.profiles set role = 'disponent' where id = '22222222-2222-2222-2222-222222222222'; update public.profiles set role = 'fahrer' where id = '33333333-3333-3333-3333-333333333333'; diff --git a/supabase/tests/orte_fahrzeuge_rls_test.sql b/supabase/tests/orte_fahrzeuge_rls_test.sql index 6ad1783..1b91616 100644 --- a/supabase/tests/orte_fahrzeuge_rls_test.sql +++ b/supabase/tests/orte_fahrzeuge_rls_test.sql @@ -7,6 +7,7 @@ insert into auth.users (id, email) values ('f3333333-3333-3333-3333-333333333333', 'fahrer@stammdaten.test.local'), ('05555555-5555-5555-5555-555555555555', 'readonly@stammdaten.test.local'); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; update public.profiles set role = 'admin' where id = 'a1111111-1111-1111-1111-111111111111'; update public.profiles set role = 'disponent' where id = 'd2222222-2222-2222-2222-222222222222'; update public.profiles set role = 'fahrer' where id = 'f3333333-3333-3333-3333-333333333333'; diff --git a/supabase/tests/profiles_rls_test.sql b/supabase/tests/profiles_rls_test.sql index 7311df4..661a87c 100644 --- a/supabase/tests/profiles_rls_test.sql +++ b/supabase/tests/profiles_rls_test.sql @@ -5,6 +5,7 @@ insert into auth.users (id, email) values ('11111111-1111-1111-1111-111111111111', 'admin@test.local'), ('22222222-2222-2222-2222-222222222222', 'fahrer@test.local'); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; update public.profiles set role = 'admin' where id = '11111111-1111-1111-1111-111111111111'; update public.profiles set role = 'fahrer' where id = '22222222-2222-2222-2222-222222222222'; diff --git a/supabase/tests/protokoll_email_test.sql b/supabase/tests/protokoll_email_test.sql index 7408781..b807dca 100644 --- a/supabase/tests/protokoll_email_test.sql +++ b/supabase/tests/protokoll_email_test.sql @@ -7,23 +7,24 @@ insert into auth.users (id, email, raw_user_meta_data) values ('e5a00000-0000-0000-0000-000000000001', 'dispo-mail@test.local', '{}'::jsonb), ('e5a00000-0000-0000-0000-000000000002', null, '{"name": "Dispo ohne Mail"}'::jsonb); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; update public.profiles set role = 'disponent' where id = 'e5a00000-0000-0000-0000-000000000001'; update public.profiles set role = 'disponent' where id = 'e5a00000-0000-0000-0000-000000000002'; -insert into public.orte (id, adresse, ansprechpartner) values - ('e5b00000-0000-0000-0000-000000000001', 'Hauptstr. 1, Musterstadt', 'Frau Muster'), - ('e5b00000-0000-0000-0000-000000000002', 'Nebenweg 2, Musterstadt', 'Herr Beispiel'); +insert into public.orte (id, adresse, ansprechpartner, firma_id) values + ('e5b00000-0000-0000-0000-000000000001', 'Hauptstr. 1, Musterstadt', 'Frau Muster', '00000000-0000-0000-0000-00000000f001'), + ('e5b00000-0000-0000-0000-000000000002', 'Nebenweg 2, Musterstadt', 'Herr Beispiel', '00000000-0000-0000-0000-00000000f001'); -insert into public.fahrzeuge (id, kennzeichen) values - ('e5c00000-0000-0000-0000-000000000001', 'M-ML 001'); +insert into public.fahrzeuge (id, kennzeichen, firma_id) values + ('e5c00000-0000-0000-0000-000000000001', 'M-ML 001', '00000000-0000-0000-0000-00000000f001'); -- A: Ersteller mit E-Mail, unterwegs -- B: Ersteller ohne E-Mail, unterwegs -- C: Ersteller mit E-Mail, zugewiesen (wird nur storniert) -insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, start_km) values - ('e5d00000-0000-0000-0000-00000000000a', 'e5b00000-0000-0000-0000-000000000001', 'e5b00000-0000-0000-0000-000000000002', 'e5c00000-0000-0000-0000-000000000001', 'e5a00000-0000-0000-0000-000000000001', 'unterwegs', 100), - ('e5d00000-0000-0000-0000-00000000000b', 'e5b00000-0000-0000-0000-000000000001', 'e5b00000-0000-0000-0000-000000000002', 'e5c00000-0000-0000-0000-000000000001', 'e5a00000-0000-0000-0000-000000000002', 'unterwegs', 100), - ('e5d00000-0000-0000-0000-00000000000c', 'e5b00000-0000-0000-0000-000000000001', 'e5b00000-0000-0000-0000-000000000002', 'e5c00000-0000-0000-0000-000000000001', 'e5a00000-0000-0000-0000-000000000001', 'zugewiesen', null); +insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, start_km, firma_id) values + ('e5d00000-0000-0000-0000-00000000000a', 'e5b00000-0000-0000-0000-000000000001', 'e5b00000-0000-0000-0000-000000000002', 'e5c00000-0000-0000-0000-000000000001', 'e5a00000-0000-0000-0000-000000000001', 'unterwegs', 100, '00000000-0000-0000-0000-00000000f001'), + ('e5d00000-0000-0000-0000-00000000000b', 'e5b00000-0000-0000-0000-000000000001', 'e5b00000-0000-0000-0000-000000000002', 'e5c00000-0000-0000-0000-000000000001', 'e5a00000-0000-0000-0000-000000000002', 'unterwegs', 100, '00000000-0000-0000-0000-00000000f001'), + ('e5d00000-0000-0000-0000-00000000000c', 'e5b00000-0000-0000-0000-000000000001', 'e5b00000-0000-0000-0000-000000000002', 'e5c00000-0000-0000-0000-000000000001', 'e5a00000-0000-0000-0000-000000000001', 'zugewiesen', null, '00000000-0000-0000-0000-00000000f001'); -- 1) Abschluss legt genau eine Zeile an update public.auftraege @@ -88,9 +89,9 @@ select is( -- 6) claim_notification_batch claimt nur die angefragten Ereignis-Arten delete from public.notification_outbox; -insert into public.notification_outbox (id, event_type, status, payload, naechster_versuch_ab) values - ('e5e00000-0000-0000-0000-000000000001', 'auftrag_erstellt', 'pending', '{}'::jsonb, now() - interval '1 minute'), - ('e5e00000-0000-0000-0000-000000000002', 'protokoll_abgeschlossen', 'pending', '{}'::jsonb, now() - interval '1 minute'); +insert into public.notification_outbox (id, event_type, status, payload, naechster_versuch_ab, firma_id) values + ('e5e00000-0000-0000-0000-000000000001', 'auftrag_erstellt', 'pending', '{}'::jsonb, now() - interval '1 minute', '00000000-0000-0000-0000-00000000f001'), + ('e5e00000-0000-0000-0000-000000000002', 'protokoll_abgeschlossen', 'pending', '{}'::jsonb, now() - interval '1 minute', '00000000-0000-0000-0000-00000000f001'); select results_eq( $$ select id::text from public.claim_notification_batch(10, array['protokoll_abgeschlossen']) $$, diff --git a/supabase/tests/zeitplanung_test.sql b/supabase/tests/zeitplanung_test.sql index 9dba940..b947b33 100644 --- a/supabase/tests/zeitplanung_test.sql +++ b/supabase/tests/zeitplanung_test.sql @@ -3,14 +3,15 @@ select plan(5); insert into auth.users (id, email) values ('a1111111-1111-1111-1111-111111111111', 'admin@zeitplanung.test.local'); +update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null; update public.profiles set role = 'admin' where id = 'a1111111-1111-1111-1111-111111111111'; -insert into public.orte (id, adresse, ansprechpartner) values - ('01111111-1111-1111-1111-111111111111', 'Hauptstr. 1, Musterstadt', 'Frau Muster'), - ('02222222-2222-2222-2222-222222222222', 'Nebenweg 2, Musterstadt', 'Herr Beispiel'); +insert into public.orte (id, adresse, ansprechpartner, firma_id) values + ('01111111-1111-1111-1111-111111111111', 'Hauptstr. 1, Musterstadt', 'Frau Muster', '00000000-0000-0000-0000-00000000f001'), + ('02222222-2222-2222-2222-222222222222', 'Nebenweg 2, Musterstadt', 'Herr Beispiel', '00000000-0000-0000-0000-00000000f001'); -insert into public.fahrzeuge (id, kennzeichen) values - ('f0000000-0000-0000-0000-000000000001', 'M-AB 123'); +insert into public.fahrzeuge (id, kennzeichen, firma_id) values + ('f0000000-0000-0000-0000-000000000001', 'M-AB 123', '00000000-0000-0000-0000-00000000f001'); set local role authenticated; set local request.jwt.claims to '{"sub": "a1111111-1111-1111-1111-111111111111", "role": "authenticated"}'; diff --git a/web/lib/orte-fahrzeuge/mutations.test.ts b/web/lib/orte-fahrzeuge/mutations.test.ts new file mode 100644 index 0000000..82513d2 --- /dev/null +++ b/web/lib/orte-fahrzeuge/mutations.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; +import { duplikatFehler } from "./mutations"; + +describe("duplikatFehler", () => { + it("erkennt den FIN-Constraint pro Firma", () => { + expect( + duplikatFehler({ message: 'duplicate key value violates unique constraint "fahrzeuge_firma_fin_key"' }) + ).toBe("Diese FIN ist bereits vergeben."); + }); + + it("meldet sonst ein doppeltes Kennzeichen", () => { + expect( + duplikatFehler({ message: 'duplicate key value violates unique constraint "fahrzeuge_firma_kennzeichen_key"' }) + ).toBe("Dieses Kennzeichen ist bereits vergeben."); + }); +}); diff --git a/web/lib/orte-fahrzeuge/mutations.ts b/web/lib/orte-fahrzeuge/mutations.ts index 135cd0f..9aeb84d 100644 --- a/web/lib/orte-fahrzeuge/mutations.ts +++ b/web/lib/orte-fahrzeuge/mutations.ts @@ -55,9 +55,10 @@ export async function updateOrtZeile(input: { } // Beide Unique-Constraints liefern 23505; unterschieden wird am -// Constraint-Namen in der Postgres-Meldung. -function duplikatFehler(error: { message: string }): string { - return error.message.includes("fahrzeuge_fin_key") +// Constraint-Namen in der Postgres-Meldung (fahrzeuge_firma_fin_key / +// fahrzeuge_firma_kennzeichen_key, eindeutig pro Firma). +export function duplikatFehler(error: { message: string }): string { + return error.message.includes("_fin_key") ? "Diese FIN ist bereits vergeben." : "Dieses Kennzeichen ist bereits vergeben."; }