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
@@ -0,0 +1,13 @@
CREATE TABLE `audit_log` (
`id` bigint unsigned AUTO_INCREMENT NOT NULL,
`user_id` bigint unsigned,
`action` varchar(50) NOT NULL,
`target_type` varchar(32),
`target_id` varchar(64),
`details` json,
`created_at` timestamp NOT NULL DEFAULT (now()),
CONSTRAINT `audit_log_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
ALTER TABLE `audit_log` ADD CONSTRAINT `audit_log_user_id_users_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE INDEX `audit_log_action_idx` ON `audit_log` (`action`);
File diff suppressed because it is too large Load Diff
+7
View File
@@ -183,6 +183,13 @@
"when": 1791040007768,
"tag": "0025_rare_william_stryker",
"breakpoints": true
},
{
"idx": 26,
"version": "5",
"when": 1791391067859,
"tag": "0026_sleepy_colonel_america",
"breakpoints": true
}
]
}
+18 -1
View File
@@ -1,5 +1,5 @@
import { relations } from 'drizzle-orm'
import { bigint, boolean, char, int, json, mysqlEnum, mysqlTable, primaryKey, text, timestamp, varchar } from 'drizzle-orm/mysql-core'
import { bigint, boolean, char, index, int, json, mysqlEnum, mysqlTable, primaryKey, text, timestamp, varchar } from 'drizzle-orm/mysql-core'
export const users = mysqlTable('users', {
id: bigint('id', { mode: 'number', unsigned: true }).autoincrement().primaryKey(),
@@ -138,6 +138,19 @@ export const uploadTags = mysqlTable('upload_tags', {
tagId: bigint('tag_id', { mode: 'number', unsigned: true }).notNull().references(() => tags.id, { onDelete: 'cascade' }),
}, t => [primaryKey({ columns: [t.uploadId, t.tagId] })])
// Protokoll von Moderations- und Admin-Aktionen. Der Akteur bleibt beim
// Löschen des Nutzers als NULL erhalten, Details als JSON-Snapshot, damit
// Einträge auch nach dem Löschen des Ziels lesbar bleiben
export const auditLog = mysqlTable('audit_log', {
id: bigint('id', { mode: 'number', unsigned: true }).autoincrement().primaryKey(),
userId: bigint('user_id', { mode: 'number', unsigned: true }).references(() => users.id, { onDelete: 'set null' }),
action: varchar('action', { length: 50 }).notNull(),
targetType: varchar('target_type', { length: 32 }),
targetId: varchar('target_id', { length: 64 }),
details: json('details'),
createdAt: timestamp('created_at').defaultNow().notNull(),
}, t => [index('audit_log_action_idx').on(t.action)])
// Generischer Key-Value-Store für Seiten-Einstellungen (z. B. MOTD)
export const settings = mysqlTable('settings', {
key: varchar('key', { length: 100 }).primaryKey(),
@@ -212,6 +225,10 @@ export const threadSubscriptionsRelations = relations(threadSubscriptions, ({ on
user: one(users, { fields: [threadSubscriptions.userId], references: [users.id] }),
}))
export const auditLogRelations = relations(auditLog, ({ one }) => ({
user: one(users, { fields: [auditLog.userId], references: [users.id] }),
}))
export const notificationsRelations = relations(notifications, ({ one }) => ({
user: one(users, { fields: [notifications.userId], references: [users.id] }),
actor: one(users, { fields: [notifications.actorId], references: [users.id] }),