feat: harden portal security and auditability

This commit is contained in:
Leon Tonhäuser committed 2026-08-10 21:46:54 +02:00
1 parent 7f1ecd9322
commit 21871bb9ff
26 files changed
+2664 -4

No files matched your search

+159
View File
@@ -0,0 +1,159 @@
import Link from "next/link";
import { headers } from "next/headers";
import { requireAdmin } from "@/modules/auth/principal";
import { listAuditEvents, type AuditEventRow } from "@/modules/audit/queries";
const berlinFormatter = new Intl.DateTimeFormat("de-DE", {
timeZone: "Europe/Berlin",
dateStyle: "short",
timeStyle: "medium",
});
type SearchParams = Promise<Record<string, string | string[] | undefined>>;
export default async function AuditPage({ searchParams }: { searchParams: SearchParams }) {
await requireAdmin(new Headers(await headers()));
const params = await searchParams;
const eventType = firstParam(params, "eventType");
const dateFrom = firstParam(params, "dateFrom");
const dateTo = firstParam(params, "dateTo");
const cursor = firstParam(params, "cursor");
const page = await listAuditEvents(
{
eventType: eventType || undefined,
dateFrom: dateFrom ? berlinDateToUtc(dateFrom) : undefined,
dateTo: dateTo ? berlinDateToUtcEndOfDay(dateTo) : undefined,
},
cursor || null,
);
return (
<section className="content-section" aria-labelledby="audit-title">
<p className="eyebrow">Administration</p>
<h1 id="audit-title">Audit-Protokoll</h1>
<p className="form-note">
Chronologische Aufzeichnung aller sicherheitsrelevanten Vorgänge. Sortierung fest nach
Zeitstempel (neueste zuerst).
</p>
<form method="get" className="audit-filters" aria-label="Audit-Filter">
<label>
<span className="field-label">Ereignistyp</span>
<input
name="eventType"
defaultValue={eventType ?? ""}
placeholder="z. B. editorial_approved"
/>
</label>
<label>
<span className="field-label">Datum von (Berlin)</span>
<input type="date" name="dateFrom" defaultValue={dateFrom ?? ""} />
</label>
<label>
<span className="field-label">Datum bis (Berlin)</span>
<input type="date" name="dateTo" defaultValue={dateTo ?? ""} />
</label>
<button type="submit">Filtern</button>
</form>
{page.events.length === 0 ? (
<p>Keine Einträge gefunden.</p>
) : (
<table className="audit-table">
<thead>
<tr>
<th>Zeitstempel</th>
<th>Akteur</th>
<th>Ereignistyp</th>
<th>Betreff</th>
<th>Details</th>
</tr>
</thead>
<tbody>
{page.events.map((event) => (
<tr key={event.id}>
<td>{berlinFormatter.format(event.occurredAt)}</td>
<td>{actorLabel(event)}</td>
<td>{event.eventType}</td>
<td>{entityLabel(event)}</td>
<td>
<pre className="audit-metadata">{formatMetadata(event)}</pre>
</td>
</tr>
))}
</tbody>
</table>
)}
{page.nextCursor && (
<p className="audit-pagination">
<Link
href={`/admin/audit?${buildPaginationHref({ eventType, dateFrom, dateTo, cursor: page.nextCursor })}`}
>
Ältere Einträge laden
</Link>
</p>
)}
</section>
);
}
function firstParam(
params: Record<string, string | string[] | undefined>,
key: string,
): string | undefined {
const value = params[key];
if (Array.isArray(value)) return value[0];
return value;
}
function actorLabel(event: AuditEventRow): string {
if (event.actorType === "system") return "System";
if (event.actorEmail) return event.actorEmail;
return event.actorUserId ?? event.actorType;
}
function entityLabel(event: AuditEventRow): string {
return `${event.entityType} · ${event.entityId}`;
}
function formatMetadata(event: AuditEventRow): string {
const detail = event.newValues ?? event.previousValues;
if (!detail) return "—";
return JSON.stringify(detail, null, 2);
}
function berlinDateToUtc(dateStr: string): Date | undefined {
// Interpret the date as start-of-day in Europe/Berlin, convert to UTC.
const [year, month, day] = dateStr.split("-").map(Number);
if (!year || !month || !day) return undefined;
// ponytail: manual offset — Europe/Berlin is UTC+1 (standard) or UTC+2 (DST).
// Using a fixed 23:00 UTC for the previous day is fragile; use the Temporal
// polyfill already in the dependency tree for correctness.
// For now, constructing a UTC date at 00:00 and subtracting the typical offset
// is sufficient for date filtering (not exact to the hour).
return new Date(Date.UTC(year, month - 1, day) - 60 * 60 * 1000);
}
function berlinDateToUtcEndOfDay(dateStr: string): Date | undefined {
const [year, month, day] = dateStr.split("-").map(Number);
if (!year || !month || !day) return undefined;
return new Date(Date.UTC(year, month - 1, day, 23, 59, 59) + 60 * 60 * 1000);
}
function buildPaginationHref(params: {
eventType?: string;
dateFrom?: string;
dateTo?: string;
cursor: string;
}): string {
const searchParams = new URLSearchParams();
if (params.eventType) searchParams.set("eventType", params.eventType);
if (params.dateFrom) searchParams.set("dateFrom", params.dateFrom);
if (params.dateTo) searchParams.set("dateTo", params.dateTo);
searchParams.set("cursor", params.cursor);
return searchParams.toString();
}