Files
alpenwerk-hr/lib/auth/require-hr.ts
Raphael Müllner b47dec6387 Übergabestand 2026-10-14
Alpenwerk HR — HR-Stammdatenverwaltung

Mitarbeiter:innen, Organisationsstruktur, Planstellen, Eintritt, Austritt,
Versetzung, Beförderung, Karenz, On- und Offboarding, Berichte, Exporte,
Massenimport und ein lückenloser Nachweis jeder Änderung.

Next.js 16, React 19 und TypeScript; PostgreSQL über Kysely; Anmeldung
ausschliesslich über Microsoft Entra ID. Die Fachlogik liegt in der
Datenbank, die Zugriffsschranke ist Row Level Security.

Betrieb auf Windows Server: siehe deploy/windows/README.md.
Sicherheitskonzept: docs/sicherheitskonzept.md.
2026-10-14 10:12:22 +02:00

33 lines
1.5 KiB
TypeScript

import "server-only";
import { NextResponse } from "next/server";
import { sql, withUser } from "@/lib/db";
import { currentUserId } from "./session";
// Route Handlers under /api/export/* and /api/import are outside the App
// Router layout tree, so app/(app)/layout.tsx's HR gate never runs for them —
// each one has to re-establish that the caller is an active HR user itself.
// RLS is still the real boundary (an unauthorized session simply reads
// nothing); this exists so those routes answer 401/403 instead of handing back
// an empty workbook.
//
// Gefragt wird is_hr_user() und nicht profiles.role/is_active von Hand.
// Der Unterschied ist nicht kosmetisch: is_hr_user() ist dieselbe Funktion, die
// alle RLS-Policies aufrufen. Was immer sie prüft, gilt hier automatisch mit.
// Eine Prüfung von Hand liefe ihr mit der Zeit davon und lieferte einen
// Export aus, den die Policies darunter leer liessen.
export type HrGate = { denied: NextResponse } | { userId: string };
export async function requireHrUser(): Promise<HrGate> {
const userId = await currentUserId();
if (!userId) return { denied: NextResponse.json({ error: "Nicht angemeldet." }, { status: 401 }) };
const erlaubt = await withUser(userId, async (tx) => {
const ergebnis = await sql<{ ok: boolean }>`select is_hr_user() as ok`.execute(tx);
return ergebnis.rows[0]?.ok === true;
});
if (!erlaubt) return { denied: NextResponse.json({ error: "Nicht berechtigt." }, { status: 403 }) };
return { userId };
}