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>
31 lines
1.2 KiB
TypeScript
31 lines
1.2 KiB
TypeScript
import { eq } from 'drizzle-orm'
|
|
import { z } from 'zod'
|
|
import { roles, users } from '../../../../db/schema'
|
|
|
|
const bodySchema = z.object({
|
|
roleId: z.number().int().positive(),
|
|
})
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
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' })
|
|
}
|
|
const { roleId } = await readValidatedBody(event, bodySchema.parse)
|
|
const db = useDb()
|
|
|
|
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, 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 }
|
|
})
|