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>
This commit is contained in:
2026-10-07 19:27:28 +02:00
co-authored by Claude Opus 5.5
parent 983c37f1db
commit 06ac414457
22 changed files with 1939 additions and 19 deletions
+4 -3
View File
@@ -7,7 +7,7 @@ const bodySchema = z.object({
})
export default defineEventHandler(async (event) => {
await requirePermission(event, 'users.manage')
const actor = await requirePermission(event, 'users.manage')
const userId = Number(getRouterParam(event, 'id'))
if (!Number.isInteger(userId) || userId <= 0) {
throw createError({ statusCode: 400, statusMessage: 'Ungültige ID' })
@@ -15,15 +15,16 @@ export default defineEventHandler(async (event) => {
const { roleId } = await readValidatedBody(event, bodySchema.parse)
const db = useDb()
const [user] = await db.select({ id: users.id }).from(users).where(eq(users.id, userId)).limit(1)
const [user] = await db.select({ id: users.id, name: users.name }).from(users).where(eq(users.id, userId)).limit(1)
if (!user) {
throw createError({ statusCode: 404, statusMessage: 'Nutzer nicht gefunden' })
}
const [role] = await db.select({ id: roles.id }).from(roles).where(eq(roles.id, roleId)).limit(1)
const [role] = await db.select({ id: roles.id, name: roles.name }).from(roles).where(eq(roles.id, roleId)).limit(1)
if (!role) {
throw createError({ statusCode: 404, statusMessage: 'Rolle nicht gefunden' })
}
await assignRole(userId, roleId)
await logAudit(actor.id, 'assign_role', { type: 'user', id: userId }, { userName: user.name, role: role.name })
return { ok: true }
})