52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Modules\Audit;
|
|
|
|
use Illuminate\Console\Command;
|
|
use IriksIT\Manager\Models\IpBan;
|
|
|
|
/** The way back in for who is locked out by a ban on their own address: it is lifted on the server. */
|
|
class LiftBan extends Command
|
|
{
|
|
protected $signature = 'manager:unban {ip? : The address to let back in; without it all bans are listed} {--all : Lift every ban}';
|
|
|
|
protected $description = 'Lift the ban of an address';
|
|
|
|
public function handle(IpBans $bans): int
|
|
{
|
|
$ip = $this->argument('ip');
|
|
|
|
if ($this->option('all')) {
|
|
$lifted = IpBan::query()->get()->each(fn (IpBan $ban) => $bans->lift($ban))->count();
|
|
$this->line("{$lifted} ban(s) lifted.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
if (! is_string($ip)) {
|
|
$rows = IpBan::query()->orderBy('ip')->get()->map(fn (IpBan $ban) => [
|
|
$ban->ip,
|
|
$ban->isActive() ? ($ban->expires_at?->format('Y-m-d H:i') ?? 'until lifted') : 'ended',
|
|
$ban->reason ?? '',
|
|
])->all();
|
|
|
|
$rows === [] ? $this->line('No address is banned.') : $this->table(['Address', 'Until', 'Reason'], $rows);
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$ban = IpBan::query()->where('ip', $ip)->first();
|
|
|
|
if ($ban === null) {
|
|
$this->error("{$ip} is not banned.");
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$bans->lift($ban);
|
|
$this->line("The ban of {$ip} is lifted.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|