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>
28 lines
1.3 KiB
TypeScript
28 lines
1.3 KiB
TypeScript
import { and, eq } from 'drizzle-orm'
|
|
import { roles, userRoles, users } from '../../../../../db/schema'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const admin = await requirePermission(event, 'users.manage')
|
|
const userId = Number(getRouterParam(event, 'id'))
|
|
const roleId = Number(getRouterParam(event, 'roleId'))
|
|
if (!Number.isInteger(userId) || userId <= 0 || !Number.isInteger(roleId) || roleId <= 0) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Ungültige ID' })
|
|
}
|
|
const db = useDb()
|
|
|
|
const [role] = await db.select({ name: roles.name }).from(roles).where(eq(roles.id, roleId)).limit(1)
|
|
if (!role) {
|
|
throw createError({ statusCode: 404, statusMessage: 'Rolle nicht gefunden' })
|
|
}
|
|
if (role.name === 'admin' && userId === admin.id) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Du kannst dir nicht selbst die Admin-Rolle entziehen' })
|
|
}
|
|
|
|
const [target] = await db.select({ name: users.name }).from(users).where(eq(users.id, userId)).limit(1)
|
|
const result = await db.delete(userRoles).where(and(eq(userRoles.userId, userId), eq(userRoles.roleId, roleId)))
|
|
if (result[0].affectedRows) {
|
|
await logAudit(admin.id, 'remove_role', { type: 'user', id: userId }, { userName: target?.name ?? null, role: role.name })
|
|
}
|
|
return { ok: true }
|
|
})
|