Audit module: audit log and address bans
This commit is contained in:
1 parent
b75403e5ea
commit
d2ea1bacac
41 files changed
+2137
-5
No files matched your search
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Audit;
|
||||
|
||||
use Illuminate\Auth\Events\Failed;
|
||||
use Illuminate\Auth\Events\Login;
|
||||
use Illuminate\Auth\Events\Logout;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Contracts\Http\Kernel;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use IriksIT\Manager\Models\AuditLog;
|
||||
use IriksIT\Manager\Modules\BaseModule;
|
||||
use IriksIT\Manager\Support\AuditTrail;
|
||||
use IriksIT\Manager\Support\Permission;
|
||||
|
||||
/**
|
||||
* The audit log and the bans: who logged in or failed to, who did what in the manager, and the
|
||||
* addresses that are kept out. Off by default: it stores addresses and browser lines, so a site
|
||||
* switches it on knowingly (and says so in its privacy policy).
|
||||
*/
|
||||
class AuditModule extends BaseModule
|
||||
{
|
||||
public function key(): string
|
||||
{
|
||||
return 'audit';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Audit log';
|
||||
}
|
||||
|
||||
public function enabledByDefault(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [
|
||||
new Permission('audit.view', 'See the audit log: who logged in and what was done, with addresses and browsers', 'Audit log'),
|
||||
new Permission('audit.manage', 'Ban addresses and lift bans, write notes about addresses, and set the rules of the audit log', 'Audit log', needs: ['audit.view']),
|
||||
];
|
||||
}
|
||||
|
||||
public function navigation(): array
|
||||
{
|
||||
return [['label' => '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());
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user