Files
laravel-manager/src/Support/AuditTrail.php
T
Matthijs Iriks 57f51f9b61
PR CI / flow (pull_request) Successful in 4s
PR CI / ledger (pull_request) Successful in 4s
PR CI / test (stable) (pull_request) Successful in 35s
PR CI / test (lowest) (pull_request) Successful in 40s
PR CI / security (pull_request) Successful in 51s
PR CI / pr-ci (pull_request) Successful in 1s
PR CI / release-automerge (pull_request) Successful in 4s
Audit module: audit log and address bans
2026-12-04 02:32:22 +01:00

113 lines
3.7 KiB
PHP

<?php
namespace IriksIT\Manager\Support;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use IriksIT\Manager\Models\AuditLog;
/**
* The audit log: who logged in, who failed to, and who did what in the manager. The only code
* that writes the log. Every line carries the address and the browser line of the request it came
* from; that is what the log is for, and why only accounts with "audit.view" get to see it.
*
* Never write what was typed into a form here (a password could be in it): a line says what was
* done to what.
*/
final class AuditTrail
{
/** The first thing that was made during this request. */
private ?Model $made = null;
/** A controller said what the request is about: what is made after that does not change it. */
private bool $settled = false;
/** Start of a request: forget what the one before made. */
public function reset(): void
{
$this->made = null;
$this->settled = false;
}
public function made(Model $model): void
{
if (! $this->settled && $this->made === null && ! $model instanceof AuditLog) {
$this->made = $model;
}
}
/**
* For a controller whose request is about something it did not make (an address that was
* banned before, an account that was changed), or about no single thing (null).
*/
public function about(?Model $model): void
{
$this->made = $model;
$this->settled = true;
}
public function firstMade(): ?Model
{
return $this->made;
}
/**
* @param array<string, mixed> $details
*/
public function record(
string $event,
string $summary,
?Authenticatable $user = null,
?Model $subject = null,
array $details = [],
?string $action = null,
?string $email = null,
): AuditLog {
// A command has no visitor: no address and no browser.
$request = app()->runningInConsole() && ! app()->runningUnitTests() ? null : request();
$about = $subject === null ? null : self::describe($subject);
$agent = $request?->userAgent();
$name = $user === null ? null : data_get($user, 'name');
return AuditLog::query()->create([
'event' => $event,
'action' => $action,
'summary' => Str::limit($summary, 250, '…'),
'subject_type' => $about['type'] ?? null,
'subject_id' => $about['id'] ?? null,
'subject_label' => $about['label'] ?? null,
'details' => $details === [] ? null : $details,
'user_id' => $user?->getAuthIdentifier(),
'user_name' => is_string($name) ? $name : null,
'email' => $email === null ? null : Str::limit($email, 250, '…'),
'ip' => $request?->ip(),
'user_agent' => is_string($agent) ? Str::limit($agent, 490, '…') : null,
'created_at' => now(),
]);
}
/**
* What a model is called in the log: its kind ('post', 'ip_ban'), its id and a name.
*
* @return array{type: string, id: int|null, label: string}
*/
public static function describe(Model $model): array
{
$id = $model->getKey();
$id = is_int($id) ? $id : null;
$label = '#'.($id ?? '?');
foreach (['title', 'name', 'label', 'ip', 'slug'] as $attribute) {
$value = $model->getAttribute($attribute);
if (is_string($value) && trim($value) !== '') {
$label = Str::limit(trim($value), 200, '…');
break;
}
}
return ['type' => Str::snake(class_basename($model)), 'id' => $id, 'label' => $label];
}
}