src/EventSubscriber/RequestSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Currency;
  4. use App\Entity\User;
  5. use App\Repository\CurrencyRepository;
  6. use App\Repository\MenuRepository;
  7. use Doctrine\ORM\NonUniqueResultException;
  8. use Doctrine\ORM\NoResultException;
  9. use JetBrains\PhpStorm\ArrayShape;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class RequestSubscriber implements EventSubscriberInterface
  18. {
  19.     protected array $ignoredRoute = [];
  20.     public function __construct(protected Security              $security,
  21.                                 protected MenuRepository        $menuRepository,
  22.                                 protected CurrencyRepository    $currencyRepository,
  23.                                 protected TranslatorInterface   $translator,
  24.                                 protected UrlGeneratorInterface $urlGenerator)
  25.     {
  26.         $this->ignoredRoute = [
  27.             'admin_page''admin_dashboard_index''run_command_index'
  28.         ];
  29.     }
  30.     /**
  31.      * @throws NonUniqueResultException
  32.      * @throws NoResultException
  33.      */
  34.     public function onKernelRequest(RequestEvent $event)
  35.     {
  36.         if ($event->getRequest()->get('_timezone')) {
  37.             date_default_timezone_set($event->getRequest()->get('_timezone'));
  38.         }
  39.         $event->getRequest()->getSession()->set('currentCurrency'$this->currencyRepository->findOneBy(['name' => Currency::CURRENT_CURRENCY]));
  40.         $routeName $event->getRequest()->get('_route');
  41.         $isSelect2 $event->getRequest()->get('isSelect2');
  42.         if (!$isSelect2 && $event->isMainRequest() && !in_array($routeName$this->ignoredRoute)) {
  43.             /**
  44.              * @var User $user
  45.              */
  46.             $user $this->security->getUser();
  47.             if ($user) {
  48.                 $role $user->getRole();
  49.                 if ($role) {
  50.                     $roleNameCrypt $role->getNameCrypt();
  51.                     $access        $this->menuRepository->checkAccessRoleByRouteName($roleNameCrypt$routeName);
  52.                     if (!$access) {
  53.                         if (!$event->getRequest()->isXmlHttpRequest()) {
  54.                             $event->getRequest()->getSession()->getFlashBag()
  55.                                 ->add('error'$this->translator->trans('app.unauthorized.access'));
  56.                             $event->setResponse(new RedirectResponse($this->urlGenerator->generate('admin_page')));
  57.                         } else {
  58.                             throw new AccessDeniedException();
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     }
  65.     #[ArrayShape(['kernel.request' => 'string'])]
  66.     public static function getSubscribedEvents(): array
  67.     {
  68.         return [
  69.             'kernel.request' => 'onKernelRequest',
  70.         ];
  71.     }
  72. }