forked from fahrPool/fahrPool
324 lines
12 KiB
PL/PgSQL
324 lines
12 KiB
PL/PgSQL
-- Einsatzrollen: Disponent und Admin duerfen alles, was ein Fahrer bei der
|
||
-- Durchfuehrung eines Auftrags tut (Fotos, FIN-Abgleich, Unterschrift,
|
||
-- Einstempeln) -- jeweils nur fuer Auftraege, die ihnen selbst zugewiesen sind.
|
||
-- Dazu: fahrt_beginn_at (serverseitig gesetzt), Zuweisungspruefung,
|
||
-- Ruecksetzen des FIN-Abgleichs bei Umzuweisung.
|
||
|
||
-- ------------------------------------------------------------ Hilfsfunktion
|
||
create or replace function public.ist_einsatzrolle()
|
||
returns boolean
|
||
language sql
|
||
stable
|
||
security definer
|
||
set search_path = public
|
||
as $$
|
||
select coalesce(public.user_role()::text, '') in ('fahrer','disponent','admin')
|
||
$$;
|
||
|
||
-- ----------------------------------------------------------- auftrag_fotos
|
||
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.ist_einsatzrolle()
|
||
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'))
|
||
)
|
||
)
|
||
);
|
||
|
||
-- ----------------------------------------------------------- arbeitszeiten
|
||
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.ist_einsatzrolle()
|
||
and fahrer_id = auth.uid()
|
||
and geht_um is null
|
||
and korrigiert_von is null
|
||
and korrigiert_am is null
|
||
);
|
||
|
||
-- ================================================================ Storage
|
||
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.ist_einsatzrolle()
|
||
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 "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.ist_einsatzrolle()
|
||
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.ist_einsatzrolle()
|
||
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.ist_einsatzrolle()
|
||
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'
|
||
)
|
||
);
|
||
|
||
-- ---------------------------------------------------------------- profiles
|
||
-- Disponent sieht neben Fahrern auch Disponenten und Admins der eigenen Firma
|
||
-- (fuer die Zuweisung an Einsatzrollen).
|
||
drop policy "Disponent sieht Fahrer-Profile" on public.profiles;
|
||
create policy "Disponent sieht Einsatz-Profile" on public.profiles for select using (public.user_role() = 'disponent' and role in ('fahrer','disponent','admin') and firma_id = public.user_firma_id());
|
||
|
||
-- ------------------------------------------------------ fin_bei_fahrtbeginn
|
||
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 not public.ist_einsatzrolle() then
|
||
raise exception 'Nur Einsatzrollen 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;
|
||
$$;
|
||
|
||
-- ---------------------------------------------------------- fahrt_beginn_at
|
||
alter table public.auftraege add column fahrt_beginn_at timestamptz;
|
||
|
||
create or replace function public.check_auftrag_status_transition()
|
||
returns trigger
|
||
language plpgsql
|
||
security definer
|
||
set search_path = public
|
||
as $$
|
||
begin
|
||
if public.user_role() not in ('admin', 'disponent') then
|
||
if new.abholort_id is distinct from old.abholort_id
|
||
or new.zielort_id is distinct from old.zielort_id
|
||
or new.fahrzeug_id is distinct from old.fahrzeug_id
|
||
or new.erstellt_von is distinct from old.erstellt_von
|
||
or new.vorgaenger_auftrag_id is distinct from old.vorgaenger_auftrag_id
|
||
or new.fahrt_beginn_at is distinct from old.fahrt_beginn_at
|
||
then
|
||
raise exception 'Nur Disponent/Admin duerfen Abholort, Zielort, Fahrzeug, Ersteller, Vorgaenger-Auftrag oder Fahrtbeginn aendern';
|
||
end if;
|
||
end if;
|
||
|
||
if old.status = new.status then
|
||
return new;
|
||
end if;
|
||
|
||
if not (
|
||
(old.status = 'offen' and new.status in ('zugewiesen', 'storniert'))
|
||
or (old.status = 'zugewiesen' and new.status in ('unterwegs', 'offen', 'storniert'))
|
||
or (old.status = 'unterwegs' and new.status in ('abgeschlossen', 'storniert'))
|
||
) then
|
||
raise exception 'Ungueltiger Statuswechsel: % -> %', old.status, new.status;
|
||
end if;
|
||
|
||
-- Schliesst eine weitere Rechte-Luecke (gefunden per automatisiertem
|
||
-- Security-Review waehrend Task 11): die Fahrer-UPDATE-Policy aus Task 2
|
||
-- erlaubt einem Fahrer, den Status seines EIGENEN zugewiesenen Auftrags
|
||
-- auf einen beliebigen Wert zu setzen (WITH CHECK prueft nur
|
||
-- zugewiesen_an, nicht status) -- ein Fahrer koennte seinen Auftrag also
|
||
-- direkt per RLS auf 'storniert' setzen, obwohl die Spec Stornieren
|
||
-- explizit nur fuer Disponent/Admin vorsieht ("aus jedem
|
||
-- nicht-terminalen Status"). Live verifiziert: ohne diese Pruefung
|
||
-- gelingt genau das (UPDATE 1). Analog zur Spalten-Sperre oben ist die
|
||
-- Zustandsmaschine allein (offen/zugewiesen/unterwegs/abgeschlossen sind
|
||
-- fuer Fahrer alle legitime Ziele) nicht ausreichend -- storniert ist
|
||
-- das einzige Zielstatus, das rollenabhaengig eingeschraenkt werden muss.
|
||
if new.status = 'storniert' and public.user_role() not in ('admin', 'disponent') then
|
||
raise exception 'Nur Disponent/Admin duerfen einen Auftrag stornieren';
|
||
end if;
|
||
|
||
if new.status = 'unterwegs' then
|
||
if new.start_km is null then
|
||
raise exception 'start_km muss gesetzt sein, bevor der Status auf unterwegs wechselt';
|
||
end if;
|
||
new.fahrt_beginn_at := now();
|
||
end if;
|
||
|
||
if new.status = 'abgeschlossen' then
|
||
if new.end_km is null then
|
||
raise exception 'end_km muss gesetzt sein, bevor der Status auf abgeschlossen wechselt';
|
||
end if;
|
||
if new.uebergabe_bestaetigt_at is null then
|
||
raise exception 'uebergabe_bestaetigt_at muss gesetzt sein, bevor der Status auf abgeschlossen wechselt';
|
||
end if;
|
||
end if;
|
||
|
||
return new;
|
||
end;
|
||
$$;
|
||
|
||
-- ------------------------------------------------------ Zuweisungspruefung
|
||
create or replace function public.check_auftrag_zuweisung()
|
||
returns trigger language plpgsql security definer set search_path = public as $$
|
||
begin
|
||
if new.zugewiesen_an is not null
|
||
and (tg_op = 'INSERT' or new.zugewiesen_an is distinct from old.zugewiesen_an)
|
||
and not exists (select 1 from public.profiles p
|
||
where p.id = new.zugewiesen_an and p.role in ('fahrer','disponent','admin')) then
|
||
raise exception 'Zuweisung nur an Fahrer, Disponent oder Admin' using errcode = '42501';
|
||
end if;
|
||
return new;
|
||
end; $$;
|
||
create trigger a2_check_auftrag_zuweisung before insert or update on public.auftraege
|
||
for each row execute function public.check_auftrag_zuweisung();
|
||
|
||
-- ------------------------------------------------- FIN-Abgleich bei Umzuweisung
|
||
-- Zusaetzlich zum Zurueckgeben (-> offen) wird der Abgleich auch bei einer
|
||
-- Umzuweisung (zugewiesen_an A -> B) zurueckgesetzt.
|
||
create or replace function public.schuetze_fin_abgleich()
|
||
returns trigger
|
||
language plpgsql
|
||
security definer
|
||
set search_path = public
|
||
as $$
|
||
begin
|
||
-- Gibt ein Fahrer den Auftrag zurueck (-> offen), duerfen die Pruefdaten
|
||
-- nicht auf den naechsten Fahrer vererbt werden. Gilt fuer alle Rollen.
|
||
if (new.status = 'offen' and old.status is distinct from 'offen') or (old.zugewiesen_an is not null and new.zugewiesen_an is distinct from old.zugewiesen_an) then
|
||
if old.start_fin_status is distinct from 'abweichend'::public.app_fin_abgleich then
|
||
new.start_fin := null;
|
||
new.start_fin_status := null;
|
||
return new;
|
||
end if;
|
||
-- Ein vermerkter 'abweichend' ist Nachweis und darf durch Zurueckgeben und
|
||
-- erneutes Annehmen nicht geloescht werden. Nur Admin/Disponent duerfen
|
||
-- ihn per UPDATE entfernen; fuer alle anderen bleiben die Werte stehen.
|
||
if coalesce(public.user_role()::text, '') not in ('admin', 'disponent') then
|
||
new.start_fin := old.start_fin;
|
||
new.start_fin_status := old.start_fin_status;
|
||
end if;
|
||
end if;
|
||
|
||
if (new.start_fin is distinct from old.start_fin
|
||
or new.start_fin_status is distinct from old.start_fin_status)
|
||
and coalesce(public.user_role()::text, '') not in ('admin', 'disponent')
|
||
and coalesce(current_setting('app.fin_abgleich', true), '') <> 'an'
|
||
then
|
||
raise exception 'FIN-Abgleich darf nur ueber fin_bei_fahrtbeginn gesetzt werden';
|
||
end if;
|
||
return new;
|
||
end;
|
||
$$;
|
||
|
||
-- ------------------------------------------------------------------ Backfill
|
||
-- Audit-Log existiert erst seit 2026-10-12; aeltere Auftraege bleiben leer.
|
||
update auftraege a set fahrt_beginn_at = x.t from (select datensatz_id::uuid id,
|
||
min(zeitpunkt) t from audit_log where bereich = 'auftrag' and aenderungen -> 'status' ->>
|
||
'neu' = 'unterwegs' group by 1) x where a.id = x.id and a.fahrt_beginn_at is null;
|