feat : add maintenance mode toggle in admin panel
All checks were successful
Auto Tag Develop / tag (push) Successful in 8s
All checks were successful
Auto Tag Develop / tag (push) Successful in 8s
- Backend: MaintenanceModeListener blocks non-admin API requests when var/maintenance flag file exists. MaintenanceController provides toggle (PUT /api/admin/maintenance) and public check endpoint (GET /api/maintenance/check). - Frontend: Toggle button in admin page, maintenance.vue page for blocked users, middleware redirects non-admins to /maintenance. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
58
src/Controller/MaintenanceController.php
Normal file
58
src/Controller/MaintenanceController.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class MaintenanceController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly KernelInterface $kernel,
|
||||
) {}
|
||||
|
||||
#[Route('/api/maintenance/check', name: 'maintenance_check', methods: ['GET'])]
|
||||
public function check(): JsonResponse
|
||||
{
|
||||
return new JsonResponse([
|
||||
'enabled' => file_exists($this->flagPath()),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/api/admin/maintenance', name: 'admin_maintenance_status', methods: ['GET'])]
|
||||
public function status(): JsonResponse
|
||||
{
|
||||
$this->denyAccessUnlessGranted('ROLE_ADMIN');
|
||||
|
||||
return new JsonResponse([
|
||||
'enabled' => file_exists($this->flagPath()),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/api/admin/maintenance', name: 'admin_maintenance_toggle', methods: ['PUT'])]
|
||||
public function toggle(): JsonResponse
|
||||
{
|
||||
$this->denyAccessUnlessGranted('ROLE_ADMIN');
|
||||
|
||||
$path = $this->flagPath();
|
||||
|
||||
if (file_exists($path)) {
|
||||
unlink($path);
|
||||
$enabled = false;
|
||||
} else {
|
||||
file_put_contents($path, (string) time());
|
||||
$enabled = true;
|
||||
}
|
||||
|
||||
return new JsonResponse(['enabled' => $enabled]);
|
||||
}
|
||||
|
||||
private function flagPath(): string
|
||||
{
|
||||
return $this->kernel->getProjectDir() . '/var/maintenance';
|
||||
}
|
||||
}
|
||||
60
src/EventListener/MaintenanceModeListener.php
Normal file
60
src/EventListener/MaintenanceModeListener.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\EventListener;
|
||||
|
||||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
|
||||
#[AsEventListener(event: 'kernel.request', priority: 10)]
|
||||
final class MaintenanceModeListener
|
||||
{
|
||||
public function __construct(
|
||||
private readonly KernelInterface $kernel,
|
||||
private readonly TokenStorageInterface $tokenStorage,
|
||||
) {}
|
||||
|
||||
public function __invoke(RequestEvent $event): void
|
||||
{
|
||||
if (!$event->isMainRequest()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$flagFile = $this->kernel->getProjectDir() . '/var/maintenance';
|
||||
|
||||
if (!file_exists($flagFile)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$request = $event->getRequest();
|
||||
$path = $request->getPathInfo();
|
||||
|
||||
// Always allow maintenance status endpoint and session endpoints
|
||||
if (str_starts_with($path, '/api/admin/maintenance')
|
||||
|| str_starts_with($path, '/api/maintenance/check')
|
||||
|| str_starts_with($path, '/api/session')
|
||||
|| str_starts_with($path, '/api/health')
|
||||
|| str_starts_with($path, '/api/docs')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Allow admin users through
|
||||
$token = $this->tokenStorage->getToken();
|
||||
if ($token && $token->getUser()) {
|
||||
$roles = $token->getRoleNames();
|
||||
if (in_array('ROLE_ADMIN', $roles, true)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$event->setResponse(new JsonResponse(
|
||||
['message' => 'Application en maintenance. Veuillez réessayer ultérieurement.'],
|
||||
JsonResponse::HTTP_SERVICE_UNAVAILABLE,
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user