Files
alpenwerk-hr/lib/auth/require-hr.ts
Maximilian Stubhan 324698ce33 GO-Gastro-Stand: herstellerneutral, lokale Anmeldung, synthetische Daten
- Alle Bezuege auf den bisherigen Kunden entfernt (Logo, Farben, Gesellschaft,
  Texte, Tests); Marke Alpenwerk HR neutral
- Lokale Anmeldung ohne Passwort fuer den Betrieb auf dem eigenen Rechner
  (LOKALE_ANMELDUNG, nie auf einem Server setzen)
- Kollektivvertrag Gastgewerbe
- scripts/seed-go-gastro.mjs: synthetische Testdaten in Struktur und Groesse
  von GO Gastro, keine Echtdaten
- CSP ohne eval-Fehler; Datenkatalog und Workshop-Rueckfragen unter docs/

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 20:03:49 +01:00

34 lines
1.6 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/austausch/* 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 zusätzlich prüft, gilt hier
// automatisch mit — etwa der ausstehende Passwortwechsel (Migration
// 20260908120000). Eine Prüfung von Hand wüsste davon nichts 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 };
}