forked from BRETT/BRETT
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>
45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import { unlink } from 'node:fs/promises'
|
|
import { join } from 'node:path'
|
|
import { eq, sql } from 'drizzle-orm'
|
|
import { commentAttachments, comments, uploads, users } from '../../db/schema'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const actor = await requirePermission(event, 'comment.delete')
|
|
const id = Number(getRouterParam(event, 'id'))
|
|
if (!Number.isInteger(id) || id <= 0) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Ungültige ID' })
|
|
}
|
|
const db = useDb()
|
|
const comment = await db
|
|
.select({ uploadId: comments.uploadId, content: comments.content, userId: comments.userId, userName: users.name })
|
|
.from(comments)
|
|
.innerJoin(users, eq(comments.userId, users.id))
|
|
.where(eq(comments.id, id))
|
|
.limit(1)
|
|
.then(r => r[0])
|
|
const files = await db
|
|
.select({ fileName: commentAttachments.fileName, cover: commentAttachments.cover })
|
|
.from(commentAttachments)
|
|
.where(eq(commentAttachments.commentId, id))
|
|
await db.delete(comments).where(eq(comments.id, id))
|
|
if (comment) {
|
|
await db.update(uploads).set({ commentCount: sql`GREATEST(comment_count - 1, 0)` }).where(eq(uploads.id, comment.uploadId))
|
|
}
|
|
for (const f of files) {
|
|
await unlink(join(uploadDir(), 'attachments', f.fileName)).catch(() => {})
|
|
if (f.cover) {
|
|
await unlink(join(process.cwd(), 'public', f.cover)).catch(() => {})
|
|
}
|
|
}
|
|
if (comment) {
|
|
await logAudit(actor.id, 'delete_comment', { type: 'comment', id }, {
|
|
uploadId: comment.uploadId,
|
|
authorId: comment.userId,
|
|
authorName: comment.userName,
|
|
content: comment.content.slice(0, 500),
|
|
attachments: files.length,
|
|
})
|
|
}
|
|
return { ok: true }
|
|
})
|