'Audit log', 'route' => 'manager.audit.index', 'permission' => 'audit.view']]; } public function commands(): array { return [PruneAuditLog::class, LiftBan::class]; } public function actions(): array { return [ 'audit.bans.store' => ['Banned an address', ['reason', 'whole_site']], 'audit.bans.destroy' => ['Lifted the ban of an address'], 'audit.groups.ban' => ['Banned the addresses of a group', ['group', 'reason', 'whole_site']], 'audit.groups.unban' => ['Lifted the bans of a group', ['group']], 'audit.notes.update' => ['Wrote or removed the note about an address', ['ip', 'group']], 'audit.settings' => ['Changed the rules of the audit log'], ]; } public function views(): array { return ['audit.index' => 'Opened the audit log']; } /** A banned address is refused before the session starts; every request of a manager account is written down. */ public function middleware(): array { return ['before' => [RefuseBannedAddress::class], 'after' => [RecordActions::class]]; } public function boot(): void { app()->singleton(IpBans::class); app('router')->aliasMiddleware('manager.not-banned', RefuseBannedAddress::class); // The first thing a request makes is what the line of the log is about. Event::listen('eloquent.created: *', function (string $event, array $data) { if (($data[0] ?? null) instanceof Model) { app(AuditTrail::class)->made($data[0]); } }); $this->listenForLogins(); if (config('manager.audit.whole_site_bans') && ($kernel = app(Kernel::class)) && method_exists($kernel, 'pushMiddleware')) { $kernel->pushMiddleware(RefuseBannedFromTheSite::class); } // Only the console runs the schedule; a web request never needs to resolve it. if (app()->runningInConsole()) { app()->afterResolving(Schedule::class, fn (Schedule $schedule) => $schedule->command(PruneAuditLog::class)->dailyAt('03:40')); } } /** The host owns the login: Laravel's own events say what happened, whatever the login looks like. */ private function listenForLogins(): void { $model = config('manager.user_model'); Event::listen(Login::class, function (Login $event) use ($model) { if ($event->user instanceof $model) { app(AuditTrail::class)->record(AuditLog::LOGIN, 'Logged in', $event->user); } }); Event::listen(Logout::class, function (Logout $event) use ($model) { if ($event->user instanceof $model) { app(AuditTrail::class)->record(AuditLog::LOGOUT, 'Logged out', $event->user); } }); Event::listen(Failed::class, function (Failed $event) { $email = $event->credentials['email'] ?? null; $email = is_string($email) ? $email : null; app(AuditTrail::class)->record( AuditLog::LOGIN_FAILED, $event->user === null ? 'Failed login: no such account' : 'Failed login: wrong password', $event->user, email: $email, ); app(IpBans::class)->afterFailedLogin(request()->ip()); }); } }