Files
kibiandClaude Opus 5.5 06ac414457 Add audit log for moderation and admin actions
New audit_log table records actor, action, target and a JSON snapshot
of the details, so entries stay readable after the target is deleted;
the actor is set to NULL when the user is removed. logAudit() is called
from upload delete (single and bulk), comment delete, tag removal, role
create/delete, role permission changes, role assign/revoke and MOTD
updates. Logging failures only warn and never fail the action itself.

The admin page gets an audit card behind the new audit.view permission
with action filter, click-to-filter by user, readable summaries per
action and server-side pagination. Action catalog lives in
shared/utils/audit.ts, labels under admin.auditActions (de/en).

Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 19:27:28 +02:00

23 lines
1023 B
TypeScript

import { eq } from 'drizzle-orm'
import { roles } from '../../../db/schema'
import { SYSTEM_ROLES } from '../../../../shared/utils/permissions'
export default defineEventHandler(async (event) => {
const actor = await requirePermission(event, 'roles.manage')
const id = Number(getRouterParam(event, 'id'))
if (!Number.isInteger(id) || id <= 0) {
throw createError({ statusCode: 400, statusMessage: 'Ungültige ID' })
}
const db = useDb()
const existing = await db.select({ name: roles.name }).from(roles).where(eq(roles.id, id)).limit(1)
if (!existing[0]) {
throw createError({ statusCode: 404, statusMessage: 'Rolle nicht gefunden' })
}
if ((SYSTEM_ROLES as readonly string[]).includes(existing[0].name)) {
throw createError({ statusCode: 400, statusMessage: `Die ${existing[0].name}-Rolle kann nicht gelöscht werden` })
}
await db.delete(roles).where(eq(roles.id, id))
await logAudit(actor.id, 'delete_role', { type: 'role', id }, { name: existing[0].name })
return { ok: true }
})