Files
Sebastian UnterschützandClaude Opus 5.5 bd986cd49f feat(store): add audit log table and queries
Entries are visible by kind: organization entries to the organization,
account entries to the account, platform entries to admins. The built-in
admin role gains the new audit.read permission.

Refs #65

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 09:59:52 +01:00

23 lines
1.1 KiB
SQL

-- name: InsertAuditEvent :exec
INSERT INTO audit_events (kind, org_id, actor_user_id, actor_token_id, action, target_id, details, status, ip, user_agent, request_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);
-- name: ListAuditEvents :many
-- Newest first, paged by time. kind narrows to one kind; the row policy
-- already limits what the scope can see.
SELECT e.id, e.time, e.kind, e.org_id, e.actor_user_id, e.actor_token_id, e.action, e.target_id,
e.details, e.status, coalesce(host(e.ip), '')::text AS ip, coalesce(e.user_agent, '') AS user_agent,
coalesce(e.request_id, '') AS request_id,
coalesce(u.email::text, '')::text AS actor_email, coalesce(u.display_name, '') AS actor_name
FROM audit_events e
LEFT JOIN users u ON u.id = e.actor_user_id
WHERE e.kind = sqlc.arg(kind)
AND (sqlc.narg(org_id)::uuid IS NULL OR e.org_id = sqlc.narg(org_id))
AND (sqlc.narg(actor)::uuid IS NULL OR e.actor_user_id = sqlc.narg(actor))
AND (sqlc.narg(before)::timestamptz IS NULL OR e.time < sqlc.narg(before))
ORDER BY e.time DESC
LIMIT sqlc.arg(max_rows);
-- name: PurgeAuditEvents :execrows
DELETE FROM audit_events WHERE time < $1;