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,39 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Models;
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* One line of the audit log: a login, a failed login, or something an account did in the manager.
|
||||
* Written by IriksIT\Manager\Modules\Audit\AuditTrail only.
|
||||
*
|
||||
* @property array<string, mixed>|null $details
|
||||
* @property Carbon $created_at
|
||||
*/
|
||||
class AuditLog extends Model
|
||||
{
|
||||
public const LOGIN = 'login';
|
||||
|
||||
public const LOGIN_FAILED = 'login_failed';
|
||||
|
||||
public const LOGOUT = 'logout';
|
||||
|
||||
/** Something that was done in the manager. */
|
||||
public const ACTION = 'action';
|
||||
|
||||
/** A page of the manager that was opened. */
|
||||
public const VIEWED = 'viewed';
|
||||
|
||||
/** Something the account was not allowed to do. */
|
||||
public const REFUSED = 'refused';
|
||||
|
||||
public const BAN = 'ban';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['details' => 'array', 'created_at' => 'datetime'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Models;
|
||||
|
||||
/**
|
||||
* The rules of the audit log (one row, id 1): when an address is banned by itself and how long
|
||||
* the log is kept. Set in the manager.
|
||||
*/
|
||||
class AuditSetting extends Model
|
||||
{
|
||||
/** @var array<string, mixed> */
|
||||
protected $attributes = [
|
||||
'auto_ban' => true,
|
||||
// This many failed logins from one address ...
|
||||
'max_failures' => 10,
|
||||
// ... within this many minutes ...
|
||||
'window_minutes' => 30,
|
||||
// ... keep it out this long. 0: until someone lifts the ban.
|
||||
'ban_minutes' => 1440,
|
||||
'keep_days' => 90,
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'auto_ban' => 'boolean',
|
||||
'max_failures' => 'integer',
|
||||
'window_minutes' => 'integer',
|
||||
'ban_minutes' => 'integer',
|
||||
'keep_days' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
/** The longest the log may be kept (config `manager.audit.max_keep_days`): what a privacy policy promises. */
|
||||
public static function maxKeepDays(): int
|
||||
{
|
||||
return max(7, (int) config('manager.audit.max_keep_days', 90));
|
||||
}
|
||||
|
||||
/** The rules as they are now: the saved row, or the defaults while nothing was saved. */
|
||||
public static function current(): self
|
||||
{
|
||||
return static::query()->find(1) ?? new self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{auto_ban: bool, max_failures: int, window_minutes: int, ban_minutes: int, keep_days: int}
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'auto_ban' => (bool) $this->auto_ban,
|
||||
'max_failures' => (int) $this->max_failures,
|
||||
'window_minutes' => (int) $this->window_minutes,
|
||||
'ban_minutes' => (int) $this->ban_minutes,
|
||||
// Also for a row that was saved when more was allowed.
|
||||
'keep_days' => min(self::maxKeepDays(), (int) $this->keep_days),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Carbon;
|
||||
use IriksIT\Manager\Modules\Audit\IpBans;
|
||||
|
||||
/**
|
||||
* An address that is kept out of the manager (or, `whole_site`, off the site), until `expires_at`
|
||||
* or (null) until someone lifts it. Written by IpBans only.
|
||||
*
|
||||
* @property Carbon $created_at
|
||||
*/
|
||||
class IpBan extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['automatic' => 'boolean', 'whole_site' => 'boolean', 'hits' => 'integer', 'last_hit_at' => 'datetime', 'expires_at' => 'datetime', 'created_at' => 'datetime'];
|
||||
}
|
||||
|
||||
/** The list of bans for the whole site is kept in the cache (IpBans::wholeSite): any change makes it stale. */
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saved(fn () => IpBans::forgetWholeSite());
|
||||
static::deleted(fn () => IpBans::forgetWholeSite());
|
||||
}
|
||||
|
||||
/**
|
||||
* Bans that still keep their address out.
|
||||
*
|
||||
* @param Builder<IpBan> $query
|
||||
*/
|
||||
public function scopeActive(Builder $query): void
|
||||
{
|
||||
$query->where(fn (Builder $q) => $q->whereNull('expires_at')->orWhere('expires_at', '>', now()));
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
return $this->expires_at === null || $this->expires_at->isFuture();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Models;
|
||||
|
||||
/**
|
||||
* What someone wrote down about an address: who or what it is, or why it was banned, and the
|
||||
* group it belongs to (`group_name`: addresses with the same name are banned and let back in
|
||||
* together). One note per address, kept until it is removed: unlike the audit log and an ended
|
||||
* ban it is never deleted by the prune command, so a privacy policy has to say so.
|
||||
*/
|
||||
class IpNote extends Model
|
||||
{
|
||||
/** One spelling per address, so the note of an IPv6 address is found however it is written. */
|
||||
public static function spell(string $ip): string
|
||||
{
|
||||
$packed = @inet_pton(trim($ip));
|
||||
$spelled = $packed === false ? false : inet_ntop($packed);
|
||||
|
||||
return $spelled === false ? trim($ip) : $spelled;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user