Kommentare beschreiben den heutigen Stand und warum er so ist, nicht mehr die Entwicklungsgeschichte: keine Datumsangaben zu Ereignissen, keine Vorfaelle an Testpersonen, keine Lieferantensicht, keine internen Namen oder Plattformen. In Migrationen nur Kommentare ausserhalb von Dollar-Quoting; Kommentare in Funktionskoerpern nur dort, wo ihr Text in keiner anderen Migration als Anker vorkommt. - Andreis Anpassungen aus windows-uebergabe nach master uebernommen. - LOLYO-Startpasswort kommt aus LOLYO_STARTPASSWORT statt aus dem Code; ohne Wert lehnt der Export mit Hinweis ab. - Beispiel- und erzeugte E-Mail-Adressen ohne Kundendomain. - Content-Security-Policy wird durchgesetzt; form-action fest mit login.microsoftonline.com, weil das Paket ohne Umgebungswerte gebaut wird. Geprueft im Browser: alle Seiten ohne Verstoss, Weiterleitung zu Microsoft geht durch. - migrate.mjs: einmalige Uebernahme der alten Buchfuehrung entfernt (seit 20260907100000 ohne Wirkung). - freischalten.ps1 vergibt und entzieht den HR-Zugang (-Entziehen) und protokolliert beides im Audit-Log. - docs/security-review.md -> docs/sicherheitskonzept.md: beschreibt den heutigen Stand ohne offene Punkte. README, entra-sso.md und deploy/windows/README.md ohne Widersprueche (Leserolle der IT, Freischaltung, Konto des Nachtlaufs), Abnahme-Checkliste statt "noch offen". Geprueft: lint, tsc, types:check, 823 Unit-Tests, Waechter (412 Dateien, keine Funde), alle 114 Migrationen auf leerem PostgreSQL 18 (zweiter Lauf leer), rauchtest.sql und vertragsangaben.sql bestanden.
165 lines
7.6 KiB
PL/PgSQL
165 lines
7.6 KiB
PL/PgSQL
-- HR-only access model
|
|
--
|
|
-- Rationale: nobody except an active, explicitly-provisioned HR user may
|
|
-- open the app at all. This migration:
|
|
-- 1. Collapses profiles.role to the single allowed value 'hr'.
|
|
-- 2. Adds profiles.is_active (default false — new users get zero access
|
|
-- until an existing HR user explicitly activates them).
|
|
-- 3. Renames the authorization gate from is_hr_admin() to is_hr_user()
|
|
-- (checks role='hr' AND is_active=true) and repoints every RLS
|
|
-- policy at it.
|
|
-- 4. Scopes the read policies of divisions/departments/teams/
|
|
-- locations, employee_history, positions, audit_log, reorg_scenarios/
|
|
-- reorg_moves to is_hr_user() instead of `auth.role() = 'authenticated'`
|
|
-- — which would admit ANY signed-in user, not just HR.
|
|
-- 5. Drops the employees_directory salary-masking view: with a single
|
|
-- role and salary not part of the application (see the salary
|
|
-- deprecation migration), there is nothing to mask and no second
|
|
-- role to mask it from. All reads go through the base `employees`
|
|
-- table, gated by the same is_hr_user()-only policy as writes.
|
|
--
|
|
-- The application layer checks role and activation as well; this
|
|
-- migration is what makes the rule enforceable at the data layer
|
|
-- regardless of what the UI does.
|
|
|
|
-- ── profiles: single role, explicit activation ──────────────────────
|
|
alter table profiles drop constraint if exists profiles_role_check;
|
|
update profiles set role = 'hr' where role <> 'hr';
|
|
alter table profiles add constraint profiles_role_check check (role = 'hr');
|
|
alter table profiles alter column role set default 'hr';
|
|
|
|
alter table profiles add column if not exists is_active boolean not null default false;
|
|
alter table profiles add column if not exists created_by uuid references auth.users(id);
|
|
alter table profiles add column if not exists updated_at timestamptz not null default now();
|
|
|
|
-- Profile rows that already exist when this migration runs belong to
|
|
-- explicitly provisioned users and stay usable — activation is only "off by
|
|
-- default" for newly created rows.
|
|
update profiles set is_active = true where is_active = false;
|
|
|
|
create or replace function fn_touch_profiles_updated_at()
|
|
returns trigger language plpgsql as $$
|
|
begin
|
|
new.updated_at = now();
|
|
return new;
|
|
end;
|
|
$$;
|
|
|
|
drop trigger if exists trg_profiles_touch_updated_at on profiles;
|
|
create trigger trg_profiles_touch_updated_at
|
|
before update on profiles
|
|
for each row execute function fn_touch_profiles_updated_at();
|
|
|
|
-- ── Authorization gate: is_hr_user() replaces is_hr_admin() ─────────
|
|
create or replace function is_hr_user()
|
|
returns boolean
|
|
language sql
|
|
security definer
|
|
set search_path = public
|
|
stable
|
|
as $$
|
|
select exists (
|
|
select 1 from profiles p where p.id = auth.uid() and p.role = 'hr' and p.is_active = true
|
|
);
|
|
$$;
|
|
|
|
create or replace function current_hr_user_id()
|
|
returns uuid
|
|
language sql
|
|
security definer
|
|
set search_path = public
|
|
stable
|
|
as $$
|
|
select p.id from profiles p where p.id = auth.uid() and p.role = 'hr' and p.is_active = true;
|
|
$$;
|
|
|
|
-- Compatibility shim so call sites and functions that still use
|
|
-- is_hr_admin() keep working; new code should call is_hr_user() directly.
|
|
create or replace function is_hr_admin()
|
|
returns boolean
|
|
language sql
|
|
stable
|
|
as $$
|
|
select is_hr_user();
|
|
$$;
|
|
|
|
create or replace function require_hr_admin()
|
|
returns void language plpgsql as $$
|
|
begin
|
|
if not is_hr_user() then
|
|
raise exception 'Nicht berechtigt: nur aktive HR-Benutzer:innen dürfen diese Aktion ausführen.';
|
|
end if;
|
|
end;
|
|
$$;
|
|
|
|
-- ── Re-scope every "any authenticated user" read policy to HR-only ──
|
|
drop policy if exists "org_read" on divisions;
|
|
create policy "org_read" on divisions for select using (is_hr_user());
|
|
drop policy if exists "org_write" on divisions;
|
|
create policy "org_write" on divisions for all using (is_hr_user()) with check (is_hr_user());
|
|
|
|
drop policy if exists "org_read" on departments;
|
|
create policy "org_read" on departments for select using (is_hr_user());
|
|
drop policy if exists "org_write" on departments;
|
|
create policy "org_write" on departments for all using (is_hr_user()) with check (is_hr_user());
|
|
|
|
drop policy if exists "org_read" on teams;
|
|
create policy "org_read" on teams for select using (is_hr_user());
|
|
drop policy if exists "org_write" on teams;
|
|
create policy "org_write" on teams for all using (is_hr_user()) with check (is_hr_user());
|
|
|
|
drop policy if exists "org_read" on locations;
|
|
create policy "org_read" on locations for select using (is_hr_user());
|
|
drop policy if exists "org_write" on locations;
|
|
create policy "org_write" on locations for all using (is_hr_user()) with check (is_hr_user());
|
|
|
|
drop policy if exists "history_read" on employee_history;
|
|
create policy "history_read" on employee_history for select using (is_hr_user());
|
|
drop policy if exists "history_insert_admin" on employee_history;
|
|
create policy "history_insert_admin" on employee_history for insert with check (is_hr_user());
|
|
|
|
drop policy if exists "positions_read" on positions;
|
|
create policy "positions_read" on positions for select using (is_hr_user());
|
|
drop policy if exists "positions_write_admin" on positions;
|
|
create policy "positions_write_admin" on positions for all using (is_hr_user()) with check (is_hr_user());
|
|
|
|
drop policy if exists "audit_read" on audit_log;
|
|
create policy "audit_read" on audit_log for select using (is_hr_user());
|
|
drop policy if exists "audit_insert_admin" on audit_log;
|
|
create policy "audit_insert_admin" on audit_log for insert with check (is_hr_user());
|
|
|
|
drop policy if exists "reorg_scenarios_read" on reorg_scenarios;
|
|
create policy "reorg_scenarios_read" on reorg_scenarios for select using (is_hr_user());
|
|
drop policy if exists "reorg_scenarios_write_admin" on reorg_scenarios;
|
|
create policy "reorg_scenarios_write_admin" on reorg_scenarios for all using (is_hr_user()) with check (is_hr_user());
|
|
|
|
drop policy if exists "reorg_moves_read" on reorg_moves;
|
|
create policy "reorg_moves_read" on reorg_moves for select using (is_hr_user());
|
|
drop policy if exists "reorg_moves_write_admin" on reorg_moves;
|
|
create policy "reorg_moves_write_admin" on reorg_moves for all using (is_hr_user()) with check (is_hr_user());
|
|
|
|
drop policy if exists "employees_admin_all" on employees;
|
|
create policy "employees_hr_all" on employees for all using (is_hr_user()) with check (is_hr_user());
|
|
|
|
-- profiles: users may always read their own row (needed to determine their
|
|
-- own HR status before is_hr_user() would otherwise apply); HR manages all.
|
|
drop policy if exists "profiles_select_admin" on profiles;
|
|
create policy "profiles_select_admin" on profiles for select using (is_hr_user());
|
|
drop policy if exists "profiles_write_admin" on profiles;
|
|
create policy "profiles_write_admin" on profiles for insert with check (is_hr_user());
|
|
drop policy if exists "profiles_update_admin" on profiles;
|
|
create policy "profiles_update_admin" on profiles for update using (is_hr_user()) with check (is_hr_user());
|
|
|
|
-- hire_drafts / saved_reports stay owner-scoped (unchanged) — but an owner
|
|
-- who is no longer an active HR user should not retain access either.
|
|
drop policy if exists "hire_drafts_owner" on hire_drafts;
|
|
create policy "hire_drafts_owner" on hire_drafts for all
|
|
using (created_by = auth.uid() and is_hr_user()) with check (created_by = auth.uid() and is_hr_user());
|
|
|
|
drop policy if exists "saved_reports_owner" on saved_reports;
|
|
create policy "saved_reports_owner" on saved_reports for all
|
|
using (created_by = auth.uid() and is_hr_user()) with check (created_by = auth.uid() and is_hr_user());
|
|
|
|
-- ── Drop the salary-masking view: no second role left to mask from ──
|
|
drop view if exists employees_directory;
|