src/EventListener/SiteMaintenanceModeListener.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\App\Miscellaneous;
  4. use App\Enums\Constants;
  5. use App\Repository\App\MiscellaneousRepository;
  6. use App\Service\AuthenticationService;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
  9. class SiteMaintenanceModeListener
  10. {
  11.     /**
  12.      * @var MiscellaneousRepository
  13.      */
  14.     private $miscellaneousRepository;
  15.     /**
  16.      * @var AuthenticationService
  17.      */
  18.     private $authenticationService;
  19.     public function __construct(MiscellaneousRepository $miscellaneousRepositoryAuthenticationService $authenticationService)
  20.     {
  21.         $this->miscellaneousRepository $miscellaneousRepository;
  22.         $this->authenticationService $authenticationService;
  23.     }
  24.     /**
  25.      * @param ControllerEvent $event
  26.      */
  27.     public function onKernelController(ControllerEvent $event): void
  28.     {
  29.         if (! $event->isMasterRequest()) {
  30.             return;
  31.         }
  32.         /** @var Miscellaneous $disableAllUsers */
  33.         $disableAllUsers $this->miscellaneousRepository->findOneByName(Constants::DISABLE_ALL_USERS_FIELD);
  34.         /**
  35.          * If current user is not service admin and site is in disabled mode, deny access
  36.          */
  37.         if ($disableAllUsers->getValue() && ! $this->authenticationService->userIsMmpzServiceAdmin()) {
  38.             throw new ServiceUnavailableHttpException();
  39.         }
  40.     }
  41. }