add all controllers logic
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Security;
|
||||
|
||||
use App\Models\IpAttempt;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class IpBanCommandService
|
||||
{
|
||||
public function banNow(?int $id, ?string $ip, int $hours): ?IpAttempt
|
||||
{
|
||||
$row = $this->findRow($id, $ip);
|
||||
if (!$row) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$blockedUntil = $this->addHours($hours);
|
||||
$attempts = max((int) $row->attempts, 10);
|
||||
|
||||
return DB::transaction(function () use ($row, $blockedUntil, $attempts) {
|
||||
$row->blocked_until = $blockedUntil;
|
||||
$row->attempts = $attempts;
|
||||
$row->save();
|
||||
return $row->refresh();
|
||||
});
|
||||
}
|
||||
|
||||
public function unbanOne(?int $id, ?string $ip): ?IpAttempt
|
||||
{
|
||||
$row = $this->findRow($id, $ip);
|
||||
if (!$row) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($row) {
|
||||
$row->blocked_until = null;
|
||||
$row->attempts = 0;
|
||||
$row->save();
|
||||
return $row->refresh();
|
||||
});
|
||||
}
|
||||
|
||||
public function unbanAll(): int
|
||||
{
|
||||
return (int) IpAttempt::query()
|
||||
->where('blocked_until', '>', $this->utcNow())
|
||||
->update([
|
||||
'blocked_until' => null,
|
||||
'attempts' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
private function findRow(?int $id, ?string $ip): ?IpAttempt
|
||||
{
|
||||
if ($id && $id > 0) {
|
||||
return IpAttempt::query()->find($id);
|
||||
}
|
||||
if ($ip !== null && $ip !== '') {
|
||||
return IpAttempt::query()->where('ip_address', $ip)->first();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function utcNow(): string
|
||||
{
|
||||
if (function_exists('utc_now')) {
|
||||
return (string) utc_now();
|
||||
}
|
||||
return now('UTC')->toDateTimeString();
|
||||
}
|
||||
|
||||
private function addHours(int $hours): string
|
||||
{
|
||||
$hours = max(1, $hours);
|
||||
if (function_exists('utc_now')) {
|
||||
return (string) date('Y-m-d H:i:s', strtotime("+{$hours} hours", strtotime((string) utc_now())));
|
||||
}
|
||||
return now('UTC')->addHours($hours)->toDateTimeString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Security;
|
||||
|
||||
use App\Models\IpAttempt;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
class IpBanQueryService
|
||||
{
|
||||
public function paginate(array $filters, int $page = 1, int $perPage = 20): LengthAwarePaginator
|
||||
{
|
||||
$query = IpAttempt::query()->orderBy('updated_at', 'desc');
|
||||
|
||||
$status = strtolower(trim((string) ($filters['status'] ?? 'all')));
|
||||
if ($status === 'active') {
|
||||
$query->where('blocked_until', '>', $this->utcNow());
|
||||
}
|
||||
|
||||
if (!empty($filters['search'])) {
|
||||
$term = '%' . trim((string) $filters['search']) . '%';
|
||||
$query->where('ip_address', 'like', $term);
|
||||
}
|
||||
|
||||
$sortBy = (string) ($filters['sort_by'] ?? 'updated_at');
|
||||
$sortDir = strtolower((string) ($filters['sort_dir'] ?? 'desc')) === 'asc' ? 'asc' : 'desc';
|
||||
$allowedSorts = ['updated_at', 'blocked_until', 'attempts', 'ip_address', 'last_attempt_at'];
|
||||
if (in_array($sortBy, $allowedSorts, true)) {
|
||||
$query->orderBy($sortBy, $sortDir);
|
||||
}
|
||||
|
||||
return $query->paginate($perPage, ['*'], 'page', $page);
|
||||
}
|
||||
|
||||
public function find(int $id): ?IpAttempt
|
||||
{
|
||||
return IpAttempt::query()->find($id);
|
||||
}
|
||||
|
||||
private function utcNow(): string
|
||||
{
|
||||
if (function_exists('utc_now')) {
|
||||
return (string) utc_now();
|
||||
}
|
||||
return now('UTC')->toDateTimeString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user