diff --git a/supabase/migrations/20261106090000_firma_loeschen.sql b/supabase/migrations/20261106090000_firma_loeschen.sql new file mode 100644 index 0000000..4a20243 --- /dev/null +++ b/supabase/migrations/20261106090000_firma_loeschen.sql @@ -0,0 +1,88 @@ +-- Firma endgueltig loeschen (Superadmin). Spec: docs/superpowers/specs/2026-11-06-firma-loeschen-design.md +-- +-- Storage-Dateien loescht vorher die Server Action ueber die Storage-API +-- (storage.objects ist per Trigger protect_objects_delete gegen direktes +-- SQL-Loeschen geschuetzt). Diese Funktion entfernt alle Datenbankdaten der +-- Firma atomar -- inklusive Logins (auth.users) und Audit-Log -- und +-- hinterlaesst genau einen Audit-Eintrag "firma/geloescht". + +-- Audit-Log bleibt unveraenderlich. Einzige Ausnahme: DELETE von Zeilen genau +-- der Firma, die firma_endgueltig_loeschen() in dieser Transaktion loescht +-- (transaktionslokale Einstellung app.firma_loeschen). authenticated und +-- service_role haben weiterhin kein DELETE-Grant auf audit_log; die Ausnahme +-- ist also nur aus security-definer-Code des Eigentuemers erreichbar. +create or replace function public.audit_log_unveraenderlich() +returns trigger +language plpgsql +set search_path = public +as $$ +begin + if tg_op = 'DELETE' + and old.firma_id is not null + and current_setting('app.firma_loeschen', true) = old.firma_id::text then + return old; + end if; + raise exception 'Audit-Log-Eintraege sind unveraenderlich' using errcode = '42501'; +end; +$$; + +create or replace function public.firma_endgueltig_loeschen(p_firma_id uuid, p_akteur_id uuid) +returns jsonb +language plpgsql +security definer +set search_path = public +as $$ +declare + v_name text; + v_aktiv boolean; + v_zaehler jsonb; +begin + select name, aktiv into v_name, v_aktiv from public.firmen where id = p_firma_id for update; + if not found then + raise exception 'Firma nicht gefunden'; + end if; + if v_aktiv then + raise exception 'Firma ist noch aktiv'; + end if; + + v_zaehler := jsonb_build_object( + 'nutzer', (select count(*) from public.profiles where firma_id = p_firma_id), + 'auftraege', (select count(*) from public.auftraege where firma_id = p_firma_id), + 'fotos', (select count(*) from public.auftrag_fotos where firma_id = p_firma_id), + 'orte', (select count(*) from public.orte where firma_id = p_firma_id), + 'fahrzeuge', (select count(*) from public.fahrzeuge where firma_id = p_firma_id), + 'arbeitszeiten', (select count(*) from public.arbeitszeiten where firma_id = p_firma_id) + ); + + perform set_config('app.firma_loeschen', p_firma_id::text, true); + + -- Reihenfolge folgt den Fremdschluesseln (alle ohne Cascade ausser + -- auftrag_fotos->auftraege, nutzer_details->profiles, profiles->auth.users, + -- firmen_smtp->firmen). + delete from public.auftrag_fotos where firma_id = p_firma_id; + delete from public.notification_outbox where firma_id = p_firma_id; + update public.auftraege set vorgaenger_auftrag_id = null + where firma_id = p_firma_id and vorgaenger_auftrag_id is not null; + delete from public.auftraege where firma_id = p_firma_id; + delete from public.arbeitszeiten where firma_id = p_firma_id; + delete from public.orte where firma_id = p_firma_id; + delete from public.fahrzeuge where firma_id = p_firma_id; + delete from public.auftragsnummer_zaehler where firma_id = p_firma_id; + delete from public.nutzer_details where firma_id = p_firma_id; + delete from auth.users where id in (select id from public.profiles where firma_id = p_firma_id); + delete from public.firmen where id = p_firma_id; + + -- Inklusive der Eintraege, die die Audit-Trigger beim Loeschen eben erzeugt haben. + delete from public.audit_log where firma_id = p_firma_id; + + perform public.audit_schreiben( + p_firma_id, p_akteur_id, 'firma', 'geloescht', p_firma_id::text, null, + jsonb_build_object('name', v_name) || v_zaehler + ); + + return v_zaehler; +end; +$$; + +revoke execute on function public.firma_endgueltig_loeschen(uuid, uuid) from public, anon, authenticated; +grant execute on function public.firma_endgueltig_loeschen(uuid, uuid) to service_role; diff --git a/supabase/tests/firma_loeschen_test.sql b/supabase/tests/firma_loeschen_test.sql new file mode 100644 index 0000000..ceeb6d3 --- /dev/null +++ b/supabase/tests/firma_loeschen_test.sql @@ -0,0 +1,122 @@ +begin; +select plan(23); + +-- Firma A (deaktiviert, wird geloescht) und Firma B (aktiv, muss bleiben) +insert into public.firmen (id, name, aktiv) values + ('00000000-0000-0000-0000-0000000fa001', 'Loesch A', false), + ('00000000-0000-0000-0000-0000000fb001', 'Bleibt B', true); + +insert into auth.users (id, email) values + ('aa000000-0000-0000-0000-000000000001', 'admin-a@loeschen.test.local'), + ('aa000000-0000-0000-0000-000000000002', 'fahrer-a@loeschen.test.local'), + ('bb000000-0000-0000-0000-000000000001', 'admin-b@loeschen.test.local'), + ('5a000000-0000-0000-0000-0000000005a1', 'super@loeschen.test.local'); + +update public.profiles set firma_id = '00000000-0000-0000-0000-0000000fa001', role = 'admin' + where id = 'aa000000-0000-0000-0000-000000000001'; +update public.profiles set firma_id = '00000000-0000-0000-0000-0000000fa001', role = 'fahrer' + where id = 'aa000000-0000-0000-0000-000000000002'; +update public.profiles set firma_id = '00000000-0000-0000-0000-0000000fb001', role = 'admin' + where id = 'bb000000-0000-0000-0000-000000000001'; +update public.profiles set role = 'superadmin' where id = '5a000000-0000-0000-0000-0000000005a1'; + +insert into public.nutzer_details (user_id, firma_id) values + ('aa000000-0000-0000-0000-000000000002', '00000000-0000-0000-0000-0000000fa001'); + +insert into public.orte (id, adresse, ansprechpartner, firma_id, created_by) values + ('aa000000-0000-0000-0000-0000000000c1', 'Ort A1', 'Herr A', '00000000-0000-0000-0000-0000000fa001', 'aa000000-0000-0000-0000-000000000001'), + ('aa000000-0000-0000-0000-0000000000c2', 'Ort A2', 'Frau A', '00000000-0000-0000-0000-0000000fa001', 'aa000000-0000-0000-0000-000000000001'), + ('bb000000-0000-0000-0000-0000000000c1', 'Ort B1', 'Herr B', '00000000-0000-0000-0000-0000000fb001', null), + ('bb000000-0000-0000-0000-0000000000c2', 'Ort B2', 'Frau B', '00000000-0000-0000-0000-0000000fb001', null); +insert into public.fahrzeuge (id, kennzeichen, firma_id) values + ('aa000000-0000-0000-0000-0000000000d1', 'LA-A 1', '00000000-0000-0000-0000-0000000fa001'), + ('bb000000-0000-0000-0000-0000000000d1', 'LB-B 1', '00000000-0000-0000-0000-0000000fb001'); + +insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, zugewiesen_an, status, firma_id) values + ('aa000000-0000-0000-0000-0000000000e1', 'aa000000-0000-0000-0000-0000000000c1', 'aa000000-0000-0000-0000-0000000000c2', + 'aa000000-0000-0000-0000-0000000000d1', 'aa000000-0000-0000-0000-000000000001', 'aa000000-0000-0000-0000-000000000002', + 'zugewiesen', '00000000-0000-0000-0000-0000000fa001'), + ('bb000000-0000-0000-0000-0000000000e1', 'bb000000-0000-0000-0000-0000000000c1', 'bb000000-0000-0000-0000-0000000000c2', + 'bb000000-0000-0000-0000-0000000000d1', 'bb000000-0000-0000-0000-000000000001', null, + 'offen', '00000000-0000-0000-0000-0000000fb001'); +-- Folgeauftrag in A (Selbstreferenz) +insert into public.auftraege (abholort_id, zielort_id, fahrzeug_id, erstellt_von, vorgaenger_auftrag_id, firma_id) values + ('aa000000-0000-0000-0000-0000000000c2', 'aa000000-0000-0000-0000-0000000000c1', 'aa000000-0000-0000-0000-0000000000d1', + 'aa000000-0000-0000-0000-000000000001', 'aa000000-0000-0000-0000-0000000000e1', '00000000-0000-0000-0000-0000000fa001'); + +insert into public.auftrag_fotos (auftrag_id, position, storage_path, uploaded_by, firma_id) values + ('aa000000-0000-0000-0000-0000000000e1', 'front', 'aa000000-0000-0000-0000-0000000000e1/front.jpg', + 'aa000000-0000-0000-0000-000000000002', '00000000-0000-0000-0000-0000000fa001'); + +insert into public.arbeitszeiten (fahrer_id, kommt_um, firma_id) values + ('aa000000-0000-0000-0000-000000000002', now() - interval '2 hours', '00000000-0000-0000-0000-0000000fa001'); + +-- 1) aktive Firma laesst sich nicht loeschen +select throws_ok( + $$ select public.firma_endgueltig_loeschen('00000000-0000-0000-0000-0000000fb001', '5a000000-0000-0000-0000-0000000005a1') $$, + 'P0001', 'Firma ist noch aktiv', + 'Aktive Firma wird nicht geloescht' +); + +-- 2) unbekannte Firma +select throws_ok( + $$ select public.firma_endgueltig_loeschen(gen_random_uuid(), '5a000000-0000-0000-0000-0000000005a1') $$, + 'P0001', 'Firma nicht gefunden', + 'Unbekannte Firma meldet Fehler' +); + +-- 3) Rechte +select ok(not has_function_privilege('authenticated', 'public.firma_endgueltig_loeschen(uuid, uuid)', 'execute'), + 'authenticated darf die Funktion nicht ausfuehren'); +select ok(not has_function_privilege('anon', 'public.firma_endgueltig_loeschen(uuid, uuid)', 'execute'), + 'anon darf die Funktion nicht ausfuehren'); +select ok(has_function_privilege('service_role', 'public.firma_endgueltig_loeschen(uuid, uuid)', 'execute'), + 'service_role darf die Funktion ausfuehren'); + +-- 4) Audit-Log bleibt ohne Einstellung unveraenderlich +select throws_ok( + $$ delete from public.audit_log where firma_id = '00000000-0000-0000-0000-0000000fb001' $$, + '42501', null, + 'Audit-Log-Eintraege bleiben ohne Loesch-Einstellung unveraenderlich' +); + +-- 5) Loeschen von A +create temp table ergebnis as + select public.firma_endgueltig_loeschen('00000000-0000-0000-0000-0000000fa001', '5a000000-0000-0000-0000-0000000005a1') as z; + +select is((select (z ->> 'nutzer')::int from ergebnis), 2, 'Zaehler Nutzer'); +select is((select (z ->> 'auftraege')::int from ergebnis), 2, 'Zaehler Auftraege'); +select is((select (z ->> 'fotos')::int from ergebnis), 1, 'Zaehler Fotos'); + +select is((select count(*)::int from public.firmen where id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Firma A weg'); +select is((select count(*)::int from public.auftraege where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Auftraege A weg'); +select is((select count(*)::int from public.auftrag_fotos where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Fotos A weg'); +select is((select count(*)::int from public.orte where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Orte A weg'); +select is((select count(*)::int from public.fahrzeuge where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Fahrzeuge A weg'); +select is((select count(*)::int from public.arbeitszeiten where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Arbeitszeiten A weg'); +select is((select count(*)::int from public.nutzer_details where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Nutzerdetails A weg'); +select is((select count(*)::int from public.notification_outbox where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Outbox A weg'); +select is((select count(*)::int from public.auftragsnummer_zaehler where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Nummernzaehler A weg'); +select is((select count(*)::int from public.profiles where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Profile A weg'); +select is((select count(*)::int from auth.users where id in ('aa000000-0000-0000-0000-000000000001', 'aa000000-0000-0000-0000-000000000002')), 0, + 'Logins A weg'); + +-- 6) B unveraendert +select is( + (select count(*)::int from public.auftraege where firma_id = '00000000-0000-0000-0000-0000000fb001') + + (select count(*)::int from public.orte where firma_id = '00000000-0000-0000-0000-0000000fb001') + + (select count(*)::int from public.profiles where firma_id = '00000000-0000-0000-0000-0000000fb001') + + (select count(*)::int from public.firmen where id = '00000000-0000-0000-0000-0000000fb001'), + 5, 'Firma B bleibt vollstaendig erhalten' +); + +-- 7) genau ein Loesch-Eintrag im Audit-Log +select is((select count(*)::int from public.audit_log where firma_id = '00000000-0000-0000-0000-0000000fa001'), 1, + 'Nur der Loesch-Eintrag bleibt im Audit-Log'); +select is( + (select bereich || '/' || aktion || '/' || akteur_rolle from public.audit_log where firma_id = '00000000-0000-0000-0000-0000000fa001'), + 'firma/geloescht/superadmin', 'Loesch-Eintrag mit Superadmin als Akteur' +); + +select * from finish(); +rollback;