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 $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 $kept * @return array */ 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]; } }