vendor/dh/sylius-access-control-layer-plugin/src/Access/AdministrationGroupChecker.php line 56

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DH\SyliusAccessControlPlugin\Access;
  4. use DH\SyliusAccessControlPlugin\Access\Checker\PermissionsCheckerInterface;
  5. use DH\SyliusAccessControlPlugin\Entity\AdministrationGroupInterface;
  6. use Sylius\Component\Core\Model\AdminUserInterface;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class AdministrationGroupChecker
  16. {
  17.     /** @var PermissionsCheckerInterface */
  18.     protected $permissionChecker;
  19.     /** @var RequestStack */
  20.     protected $requestStack;
  21.     /** @var Session */
  22.     protected $session;
  23.     /** @var TokenStorageInterface */
  24.     protected $tokenStorage;
  25.     /** @var UrlGeneratorInterface */
  26.     protected $router;
  27.     /** @var TranslatorInterface */
  28.     protected $translator;
  29.     public function __construct(
  30.         PermissionsCheckerInterface $permissionChecker,
  31.         RequestStack $requestStack,
  32.         Session $session,
  33.         TokenStorageInterface $tokenStorage,
  34.         UrlGeneratorInterface $router,
  35.         TranslatorInterface $translator
  36.     ) {
  37.         $this->permissionChecker $permissionChecker;
  38.         $this->requestStack $requestStack;
  39.         $this->session $session;
  40.         $this->tokenStorage $tokenStorage;
  41.         $this->router $router;
  42.         $this->translator $translator;
  43.     }
  44.     public function onKernelRequest(RequestEvent $event): void
  45.     {
  46.         $request $event->getRequest();
  47.         $route $request->attributes->get('_route');
  48.         $permissionChecker $this->permissionChecker;
  49.         $user $this->getUser();
  50.         if (null !== $route && $user instanceof AdminUserInterface && $user->getAdministrationGroup() instanceof AdministrationGroupInterface) {
  51.             if (!$permissionChecker->isUserGranted($route$user)) {
  52.                 if (!$this->requestStack->getCurrentRequest()->isXmlHttpRequest()) {
  53.                     $this->addAccessErrorFlash($event->getRequest()->getMethod());
  54.                     $event->setResponse($this->getRedirectResponse($event->getRequest()->headers->get('referer')));
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     private function addAccessErrorFlash(string $requestMethod): void
  60.     {
  61.         if ('GET' === $requestMethod || 'HEAD' === $requestMethod) {
  62.             $message $this->translator->trans('dh_sylius_access_control_plugin.ui.no_access', [], 'flashes');
  63.             $this->session->getFlashBag()->add('error'$message);
  64.             return;
  65.         }
  66.         $message $this->translator->trans('dh_sylius_access_control_plugin.ui.not_allowed', [], 'flashes');
  67.         $this->session->getFlashBag()->add('error'$message);
  68.     }
  69.     private function getRedirectResponse(?string $referer): RedirectResponse
  70.     {
  71.         if (null !== $referer) {
  72.             return new RedirectResponse($referer);
  73.         }
  74.         return new RedirectResponse($this->router->generate('sylius_admin_dashboard'));
  75.     }
  76.     protected function getUser(): ?UserInterface
  77.     {
  78.         if (null === $token $this->tokenStorage->getToken()) {
  79.             return null;
  80.         }
  81.         if (!is_object($user $token->getUser())) {
  82.             return null;
  83.         }
  84.         return $user;
  85.     }
  86. }