56 lines
2.0 KiB
PHP
56 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\AdminLoginBlock;
|
|
use App\Services\ActivityLogger;
|
|
use App\Support\Auth\AdminLoginLockout;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class AdminLoginBlockController extends Controller
|
|
{
|
|
public function __construct(private readonly ActivityLogger $activityLogger) {}
|
|
|
|
public function index(): JsonResponse
|
|
{
|
|
$blocks = AdminLoginBlock::where('blocked_until', '>', now())
|
|
->orWhere('last_failed_at', '>', now()->subMinutes(AdminLoginLockout::BLOCK_MINUTES))
|
|
->latest('last_failed_at')
|
|
->get()
|
|
->map(fn (AdminLoginBlock $block) => [
|
|
'id' => $block->id,
|
|
'ip' => $block->ip,
|
|
'failed_attempts' => $block->failed_attempts,
|
|
'last_username' => $block->last_username,
|
|
'user_agent' => $block->user_agent,
|
|
'last_failed_at' => $block->last_failed_at,
|
|
'blocked_until' => $block->isBlocked() ? $block->blocked_until : null,
|
|
]);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => [
|
|
'items' => $blocks,
|
|
'max_failures' => AdminLoginLockout::MAX_FAILURES,
|
|
'block_minutes' => AdminLoginLockout::BLOCK_MINUTES,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function destroy(Request $request, int $id): JsonResponse
|
|
{
|
|
$block = AdminLoginBlock::findOrFail($id);
|
|
$block->delete();
|
|
|
|
$this->activityLogger->log(
|
|
event: 'login_unblocked',
|
|
logName: 'auth',
|
|
description: sprintf('[admin] %s gỡ chặn đăng nhập cho IP %s (tài khoản thử gần nhất: %s)', $request->user('admin')?->username, $block->ip, $block->last_username ?? '—'),
|
|
);
|
|
|
|
return response()->json(['error' => false, 'message' => 'Đã gỡ chặn IP ' . $block->ip]);
|
|
}
|
|
}
|