forked from fahrPool/fahrPool
feat: Tabelle nutzer_details mit RLS (Telefon, Führerschein, Fahrerkarte, Code 95)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
9489e04859
commit
fae6a7af68
|
|
@ -0,0 +1,39 @@
|
||||||
|
-- Zusätzliche Stammdaten pro Nutzer (Telefon, Führerschein, Fahrerkarte, Code 95).
|
||||||
|
-- Bewusst eine eigene Tabelle statt Spalten in public.profiles: die dortigen
|
||||||
|
-- Policies lassen Fahrer/ReadOnly die Admin- und Disponent-Zeilen lesen, was für
|
||||||
|
-- Führerscheindaten (personenbezogen) nicht gewollt ist.
|
||||||
|
create table public.nutzer_details (
|
||||||
|
user_id uuid primary key references public.profiles (id) on delete cascade,
|
||||||
|
telefon text,
|
||||||
|
fuehrerscheinklassen text[] not null default '{}',
|
||||||
|
fahrerkarte boolean not null default false,
|
||||||
|
fahrerkarte_ablauf date,
|
||||||
|
berechtigung_95 boolean not null default false,
|
||||||
|
updated_at timestamptz not null default now(),
|
||||||
|
constraint nutzer_details_telefon_e164 check (telefon is null or telefon ~ '^\+[1-9][0-9]{7,14}$'),
|
||||||
|
constraint nutzer_details_klassen_gueltig check (
|
||||||
|
fuehrerscheinklassen <@ array['B', 'BE', 'C1', 'C1E', 'C', 'CE', 'D1', 'D1E', 'D', 'DE']::text[]
|
||||||
|
),
|
||||||
|
constraint nutzer_details_fahrerkarte_ablauf check (fahrerkarte or fahrerkarte_ablauf is null)
|
||||||
|
);
|
||||||
|
|
||||||
|
alter table public.nutzer_details enable row level security;
|
||||||
|
|
||||||
|
grant select, insert, update on public.nutzer_details to authenticated;
|
||||||
|
|
||||||
|
create policy "Nutzer sehen ihre eigenen Details"
|
||||||
|
on public.nutzer_details for select
|
||||||
|
using (auth.uid() = user_id);
|
||||||
|
|
||||||
|
create policy "Admin und Disponent sehen alle Details"
|
||||||
|
on public.nutzer_details for select
|
||||||
|
using (public.user_role() in ('admin', 'disponent'));
|
||||||
|
|
||||||
|
create policy "Nutzer und Admin legen Details an"
|
||||||
|
on public.nutzer_details for insert
|
||||||
|
with check (auth.uid() = user_id or public.user_role() = 'admin');
|
||||||
|
|
||||||
|
create policy "Nutzer und Admin aktualisieren Details"
|
||||||
|
on public.nutzer_details for update
|
||||||
|
using (auth.uid() = user_id or public.user_role() = 'admin')
|
||||||
|
with check (auth.uid() = user_id or public.user_role() = 'admin');
|
||||||
|
|
@ -0,0 +1,128 @@
|
||||||
|
begin;
|
||||||
|
select plan(15);
|
||||||
|
|
||||||
|
insert into auth.users (id, email) values
|
||||||
|
('11111111-1111-1111-1111-111111111111', 'admin@test.local'),
|
||||||
|
('22222222-2222-2222-2222-222222222222', 'disponent@test.local'),
|
||||||
|
('33333333-3333-3333-3333-333333333333', 'fahrer-a@test.local'),
|
||||||
|
('44444444-4444-4444-4444-444444444444', 'fahrer-b@test.local');
|
||||||
|
|
||||||
|
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';
|
||||||
|
update public.profiles set role = 'fahrer' where id = '44444444-4444-4444-4444-444444444444';
|
||||||
|
|
||||||
|
-- Fahrer B hat bereits Daten (als Superuser angelegt)
|
||||||
|
insert into public.nutzer_details (user_id, telefon) values
|
||||||
|
('44444444-4444-4444-4444-444444444444', '+491701234567');
|
||||||
|
|
||||||
|
-- ===== Fahrer A =====
|
||||||
|
set local role authenticated;
|
||||||
|
set local request.jwt.claims to '{"sub": "33333333-3333-3333-3333-333333333333", "role": "authenticated"}';
|
||||||
|
|
||||||
|
select lives_ok(
|
||||||
|
$$ insert into public.nutzer_details (user_id, telefon) values ('33333333-3333-3333-3333-333333333333', '+491751112233') $$,
|
||||||
|
'Fahrer kann seine eigene Zeile anlegen'
|
||||||
|
);
|
||||||
|
|
||||||
|
select results_eq(
|
||||||
|
$$ select count(*)::int from public.nutzer_details $$,
|
||||||
|
$$ values (1) $$,
|
||||||
|
'Fahrer sieht nur seine eigene Zeile'
|
||||||
|
);
|
||||||
|
|
||||||
|
select is_empty(
|
||||||
|
$$ select user_id from public.nutzer_details where user_id = '44444444-4444-4444-4444-444444444444' $$,
|
||||||
|
'Fahrer sieht die Zeile eines anderen Fahrers nicht'
|
||||||
|
);
|
||||||
|
|
||||||
|
select is_empty(
|
||||||
|
$$ update public.nutzer_details set telefon = '+491700000000' where user_id = '44444444-4444-4444-4444-444444444444' returning user_id $$,
|
||||||
|
'Fahrer kann die Zeile eines anderen Fahrers nicht ändern (0 Zeilen betroffen)'
|
||||||
|
);
|
||||||
|
|
||||||
|
select results_eq(
|
||||||
|
$$ update public.nutzer_details set telefon = '+491709998877' where user_id = '33333333-3333-3333-3333-333333333333' returning user_id $$,
|
||||||
|
$$ values ('33333333-3333-3333-3333-333333333333'::uuid) $$,
|
||||||
|
'Fahrer kann seine eigene Zeile ändern'
|
||||||
|
);
|
||||||
|
|
||||||
|
select throws_ok(
|
||||||
|
$$ insert into public.nutzer_details (user_id) values ('11111111-1111-1111-1111-111111111111') $$,
|
||||||
|
'42501',
|
||||||
|
null,
|
||||||
|
'Fahrer kann keine Zeile für einen anderen Nutzer anlegen (RLS)'
|
||||||
|
);
|
||||||
|
|
||||||
|
select throws_ok(
|
||||||
|
$$ update public.nutzer_details set fuehrerscheinklassen = array['XYZ'] where user_id = '33333333-3333-3333-3333-333333333333' $$,
|
||||||
|
'23514',
|
||||||
|
null,
|
||||||
|
'Ungültige Führerscheinklasse wird vom Check-Constraint abgelehnt'
|
||||||
|
);
|
||||||
|
|
||||||
|
select throws_ok(
|
||||||
|
$$ update public.nutzer_details set fahrerkarte = false, fahrerkarte_ablauf = '2027-03-01' where user_id = '33333333-3333-3333-3333-333333333333' $$,
|
||||||
|
'23514',
|
||||||
|
null,
|
||||||
|
'Ablaufdatum ohne Fahrerkarte wird vom Check-Constraint abgelehnt'
|
||||||
|
);
|
||||||
|
|
||||||
|
select throws_ok(
|
||||||
|
$$ update public.nutzer_details set telefon = '0170123' where user_id = '33333333-3333-3333-3333-333333333333' $$,
|
||||||
|
'23514',
|
||||||
|
null,
|
||||||
|
'Telefonnummer, die nicht im E.164-Format ist, wird vom Check-Constraint abgelehnt'
|
||||||
|
);
|
||||||
|
|
||||||
|
reset role;
|
||||||
|
|
||||||
|
-- ===== Disponent =====
|
||||||
|
set local role authenticated;
|
||||||
|
set local request.jwt.claims to '{"sub": "22222222-2222-2222-2222-222222222222", "role": "authenticated"}';
|
||||||
|
|
||||||
|
select results_eq(
|
||||||
|
$$ select count(*)::int from public.nutzer_details $$,
|
||||||
|
$$ values (2) $$,
|
||||||
|
'Disponent sieht die Details aller Fahrer'
|
||||||
|
);
|
||||||
|
|
||||||
|
select is_empty(
|
||||||
|
$$ update public.nutzer_details set telefon = '+491700000000' where user_id = '44444444-4444-4444-4444-444444444444' returning user_id $$,
|
||||||
|
'Disponent kann Details nicht ändern (0 Zeilen betroffen)'
|
||||||
|
);
|
||||||
|
|
||||||
|
select throws_ok(
|
||||||
|
$$ insert into public.nutzer_details (user_id) values ('11111111-1111-1111-1111-111111111111') $$,
|
||||||
|
'42501',
|
||||||
|
null,
|
||||||
|
'Disponent kann keine Zeile anlegen (RLS)'
|
||||||
|
);
|
||||||
|
|
||||||
|
reset role;
|
||||||
|
|
||||||
|
-- ===== Admin =====
|
||||||
|
set local role authenticated;
|
||||||
|
set local request.jwt.claims to '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated"}';
|
||||||
|
|
||||||
|
select results_eq(
|
||||||
|
$$ update public.nutzer_details set telefon = '+491700000000' where user_id = '44444444-4444-4444-4444-444444444444' returning user_id $$,
|
||||||
|
$$ values ('44444444-4444-4444-4444-444444444444'::uuid) $$,
|
||||||
|
'Admin kann die Details eines Fahrers ändern'
|
||||||
|
);
|
||||||
|
|
||||||
|
select lives_ok(
|
||||||
|
$$ insert into public.nutzer_details (user_id) values ('22222222-2222-2222-2222-222222222222') $$,
|
||||||
|
'Admin kann eine Zeile für einen anderen Nutzer anlegen'
|
||||||
|
);
|
||||||
|
|
||||||
|
select results_eq(
|
||||||
|
$$ select count(*)::int from public.nutzer_details $$,
|
||||||
|
$$ values (3) $$,
|
||||||
|
'Admin sieht alle Zeilen'
|
||||||
|
);
|
||||||
|
|
||||||
|
reset role;
|
||||||
|
|
||||||
|
select * from finish();
|
||||||
|
rollback;
|
||||||
Loading…
Reference in New Issue