46 lines
1.3 KiB
PHP
46 lines
1.3 KiB
PHP
<?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();
|
|
}
|
|
}
|