Files
2026-10-12 14:25:22 +02:00

40 lines
992 B
TypeScript

import { DateTime } from 'luxon'
import type { HttpContext } from '@adonisjs/core/http'
import AuditLog, { type AuditStatus } from '#models/audit_log'
/**
* Who is acting, for the action log and the jobs.
*/
export function actor({ auth, request }: HttpContext) {
return {
username: auth.user?.username ?? null,
ip: request.ip(),
}
}
/**
* Records a short action (container restart, Caddyfile change…) in the
* action log. Long actions go through the job runner instead.
*/
export async function audit(
ctx: HttpContext,
entry: {
action: string
target?: string | null
status: AuditStatus
output?: string | null
username?: string | null
}
) {
const { username, ip } = actor(ctx)
await AuditLog.create({
username: entry.username !== undefined ? entry.username : username,
ip,
action: entry.action,
target: entry.target ?? null,
status: entry.status,
output: entry.output ?? null,
finishedAt: DateTime.now(),
})
}