src/EventListener/MaintenanceListener.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Security\Core\Security;
  6. use Twig\Environment;
  7. class MaintenanceListener
  8. {
  9.     private $twig;
  10.     private $maintenance;
  11.     private $security;
  12.     public function __construct(Environment $twigbool $maintenanceSecurity $security)
  13.     {
  14.         $this->twig $twig;
  15.         $this->maintenance $maintenance;
  16.         $this->security $security;
  17.     }
  18.     public function onKernelRequest(RequestEvent $event)
  19.     {
  20.         // Permitir siempre el acceso a la ruta de inicio de sesión
  21.         if ($event->getRequest()->get('_route') === 'app_login') {
  22.             return;
  23.         }
  24.         if ($event->getRequest()->get('_route') === 'stripe_api_webhook_listener') {
  25.             return;
  26.         }
  27.         if ($this->maintenance) {
  28.             $user $this->security->getUser();
  29.             
  30.             // Comprobar si el usuario está autenticado y tiene el rol ROLE_ADM
  31.             if (!$user || !$this->security->isGranted('ROLE_ADM')) {
  32.                 $response = new Response(
  33.                     $this->twig->render('mantenimiento.html.twig'),
  34.                     Response::HTTP_SERVICE_UNAVAILABLE
  35.                 );
  36.                 $event->setResponse($response);
  37.             }
  38.         }
  39.     }
  40. }