143 lines
5.2 KiB
PHP
143 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Modules\Audit;
|
|
|
|
use BackedEnum;
|
|
use Closure;
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Str;
|
|
use IriksIT\Manager\Models\AuditLog;
|
|
use IriksIT\Manager\Support\AuditTrail;
|
|
use IriksIT\Manager\Support\ModuleRegistry;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Writes down what an account does in the manager: one line for every request that changes
|
|
* something and went through, one for every page that is opened, and one for everything the
|
|
* account was not allowed to do. It sits behind the login on all manager routes, so a new route
|
|
* is in the log without anyone thinking of it; the modules give routes a readable name
|
|
* (Module::actions() and Module::views()).
|
|
*/
|
|
class RecordActions
|
|
{
|
|
/** Opening the same page again within this time is not a new line. */
|
|
private const SAME_LOOK_MINUTES = 5;
|
|
|
|
public function __construct(private readonly AuditTrail $trail, private readonly ModuleRegistry $modules) {}
|
|
|
|
/**
|
|
* @param Closure(Request): Response $next
|
|
*/
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
$user = Auth::user();
|
|
$this->trail->reset();
|
|
|
|
$response = $next($request);
|
|
|
|
if ($user !== null) {
|
|
$this->write($request, $response, $user, $this->trail->firstMade());
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
private function write(Request $request, Response $response, Authenticatable $user, ?Model $subject): void
|
|
{
|
|
$route = Str::after((string) $request->route()?->getName(), 'manager.');
|
|
$actions = $this->modules->actions();
|
|
$views = $this->modules->views();
|
|
$thrown = property_exists($response, 'exception') && $response->exception instanceof Throwable ? $response->exception : null;
|
|
$details = $this->details($request, $actions[$route][1] ?? []);
|
|
$name = $actions[$route][0] ?? ($views[$route] ?? '');
|
|
$name = $name !== '' ? $name : 'Asked for '.$request->method().' /'.ltrim($request->path(), '/');
|
|
|
|
if ($thrown !== null) {
|
|
// Only a refusal is worth a line; a form with a mistake or a page that is not there is not.
|
|
if ($thrown instanceof AuthorizationException || ($thrown instanceof HttpExceptionInterface && $thrown->getStatusCode() === 403)) {
|
|
$this->trail->record(AuditLog::REFUSED, 'Was not allowed: '.Str::lcfirst($name), $user, $subject, $details, $route);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if ($response->getStatusCode() >= 400) {
|
|
return;
|
|
}
|
|
|
|
$changes = ! in_array($request->method(), ['GET', 'HEAD', 'OPTIONS'], true);
|
|
|
|
if (! $changes && ! isset($actions[$route])) {
|
|
$this->viewed($user, $route, $views[$route] ?? '', $subject, $details);
|
|
|
|
return;
|
|
}
|
|
|
|
$this->trail->record(AuditLog::ACTION, $name, $user, $subject, $details, $route);
|
|
}
|
|
|
|
/**
|
|
* A page that was opened. Not twice in a row: filtering a list or loading a page again within
|
|
* a few minutes is the same look. A route with an empty name stays silent (what a page asks
|
|
* for in the background).
|
|
*
|
|
* @param array<string, mixed> $details
|
|
*/
|
|
private function viewed(Authenticatable $user, string $route, string $summary, ?Model $subject, array $details): void
|
|
{
|
|
if ($summary === '') {
|
|
return;
|
|
}
|
|
|
|
$last = AuditLog::query()->where('user_id', $user->getAuthIdentifier())->orderByDesc('id')->first();
|
|
$about = $subject === null ? null : AuditTrail::describe($subject);
|
|
|
|
if ($last !== null && $last->event === AuditLog::VIEWED && $last->action === $route
|
|
&& $last->subject_id === ($about['id'] ?? null) && ($last->details ?? []) === $details
|
|
&& $last->created_at->gt(now()->subMinutes(self::SAME_LOOK_MINUTES))) {
|
|
return;
|
|
}
|
|
|
|
$this->trail->record(AuditLog::VIEWED, $summary, $user, $subject, $details, $route);
|
|
}
|
|
|
|
/**
|
|
* What the address names (`/posts/5`) and the fields of the form a module said may be kept.
|
|
*
|
|
* @param list<string> $kept
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function details(Request $request, array $kept): array
|
|
{
|
|
$input = [];
|
|
|
|
foreach ($request->route()?->parameters() ?? [] as $name => $parameter) {
|
|
$value = $parameter instanceof BackedEnum ? $parameter->value : $parameter;
|
|
|
|
if (is_scalar($value) && ! is_bool($value) && $name !== 'type') {
|
|
$input[$name] = Str::limit((string) $value, 100, '…');
|
|
}
|
|
}
|
|
|
|
foreach ($kept as $field) {
|
|
$value = $request->input($field);
|
|
|
|
if (is_bool($value)) {
|
|
$value = $value ? 'yes' : 'no';
|
|
}
|
|
|
|
if (is_string($value) && $value !== '') {
|
|
$input[$field] = Str::limit($value, 100, '…');
|
|
}
|
|
}
|
|
|
|
return $input === [] ? [] : ['input' => $input];
|
|
}
|
|
}
|