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
+10 -3
View File
@@ -1,10 +1,10 @@
import { unlink } from 'node:fs/promises'
import { join } from 'node:path'
import { eq } from 'drizzle-orm'
import { commentAttachments, comments, uploads } from '../../db/schema'
import { commentAttachments, comments, uploads, users } from '../../db/schema'
export default defineEventHandler(async (event) => {
await requirePermission(event, 'upload.delete')
const actor = await requirePermission(event, 'upload.delete')
const id = getRouterParam(event, 'id') ?? ''
if (!/^[A-Za-z0-9]{1,16}$/.test(id)) {
throw createError({ statusCode: 400, statusMessage: 'Ungültige ID' })
@@ -12,8 +12,9 @@ export default defineEventHandler(async (event) => {
const db = useDb()
const [upload] = await db
.select({ fileName: uploads.fileName, previewImage: uploads.previewImage })
.select({ fileName: uploads.fileName, previewImage: uploads.previewImage, type: uploads.type, originalName: uploads.originalName, userId: uploads.userId, userName: users.name })
.from(uploads)
.innerJoin(users, eq(uploads.userId, users.id))
.where(eq(uploads.id, id))
.limit(1)
if (!upload) {
@@ -40,5 +41,11 @@ export default defineEventHandler(async (event) => {
}
}
broadcastToAll({ type: 'upload-deleted', ids: [id] })
await logAudit(actor.id, 'delete_upload', { type: 'upload', id }, {
uploadType: upload.type,
originalName: upload.originalName,
uploaderId: upload.userId,
uploaderName: upload.userName,
})
return { ok: true }
})
+3
View File
@@ -60,5 +60,8 @@ export default defineEventHandler(async (event) => {
await db.insert(uploadTags).values(tagRows.map(t => ({ uploadId: id, tagId: t.id })))
}
if (removed.length) {
await logAudit(user.id, 'remove_tags', { type: 'upload', id }, { removed, isUploader: upload.userId === user.id })
}
return { tags: tagRows.map(t => t.name).sort() }
})
+2 -1
View File
@@ -6,7 +6,7 @@ import { commentAttachments, comments, uploads } from '../../db/schema'
const ID_RE = /^[A-Za-z0-9]{1,16}$/
export default defineEventHandler(async (event) => {
await requirePermission(event, 'upload.delete')
const actor = await requirePermission(event, 'upload.delete')
const body = await readBody<{ ids?: unknown }>(event)
const raw = Array.isArray(body?.ids) ? body.ids : []
const ids = [...new Set(raw.filter((x): x is string => typeof x === 'string' && ID_RE.test(x)))].slice(0, 200)
@@ -45,5 +45,6 @@ export default defineEventHandler(async (event) => {
}
}
broadcastToAll({ type: 'upload-deleted', ids: found })
await logAudit(actor.id, 'bulk_delete_uploads', null, { ids: found, count: found.length })
return { ok: true, deleted: found.length }
})