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

52 lines
1.9 KiB
TypeScript

import { unlink } from 'node:fs/promises'
import { join } from 'node:path'
import { eq } from 'drizzle-orm'
import { commentAttachments, comments, uploads, users } from '../../db/schema'
export default defineEventHandler(async (event) => {
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' })
}
const db = useDb()
const [upload] = await db
.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) {
throw createError({ statusCode: 404, statusMessage: 'Upload nicht gefunden' })
}
const attachmentFiles = await db
.select({ fileName: commentAttachments.fileName, cover: commentAttachments.cover })
.from(commentAttachments)
.innerJoin(comments, eq(commentAttachments.commentId, comments.id))
.where(eq(comments.uploadId, id))
await db.delete(uploads).where(eq(uploads.id, id))
if (upload.fileName) {
await unlink(join(uploadDir(), upload.fileName)).catch(() => {})
}
if (upload.previewImage?.startsWith('/uploads/')) {
await unlink(join(process.cwd(), 'public', upload.previewImage)).catch(() => {})
}
for (const f of attachmentFiles) {
await unlink(join(uploadDir(), 'attachments', f.fileName)).catch(() => {})
if (f.cover) {
await unlink(join(process.cwd(), 'public', f.cover)).catch(() => {})
}
}
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 }
})