src/Hitso/Bundle/AdminBundle/EventListener/SiteRedirectingListener.php line 51

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\AdminBundle\EventListener;
  4. use Hitso\Bundle\CommonBundle\Entity\User;
  5. use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteContext;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Session\Session;
  9. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. class SiteRedirectingListener
  14. {
  15.     /**
  16.      * @var SiteContext
  17.      */
  18.     protected $context;
  19.     /**
  20.      * @var TranslatorInterface
  21.      */
  22.     protected $translator;
  23.     /**
  24.      * @var UrlGeneratorInterface
  25.      */
  26.     protected $generator;
  27.     /**
  28.      * @var TokenStorageInterface
  29.      */
  30.     protected $storage;
  31.     public function __construct(
  32.         SiteContext $context,
  33.         TokenStorageInterface $storage,
  34.         TranslatorInterface $translator,
  35.         UrlGeneratorInterface $generator
  36.     ) {
  37.         $this->context    $context;
  38.         $this->translator $translator;
  39.         $this->generator  $generator;
  40.         $this->storage    $storage;
  41.     }
  42.     public function onRequest(GetResponseEvent $e)
  43.     {
  44.         $runningSite $this->context->getRunningSite();
  45.         $content     $this->context->getContentSite();
  46.         $token       $this->storage->getToken();
  47.         $user        $token $token->getUser() : null;
  48.         $sites       $this->context->getSites();
  49.         $session     $e->getRequest()->getSession();
  50.         if (                                 //prerequirements:
  51.             $e->isMasterRequest()        // * current request is master request
  52.             && $runningSite                 // * running site is NOT content site (so is the admin)
  53.             && !$runningSite->isContent()   // * running site is NOT content site (so is the admin)
  54.             && ($user instanceof User)      // * user is logget in and $user is instance of Hitso user class
  55.             && $content                     // * there is set a content site
  56.         ) {
  57.             $allowed $user->getAllowedSites();
  58.             // Set locale for user
  59.             $siteId $session->get('site_id');
  60.             $site   $sites->get($siteId);
  61.             if (!empty($site)) {
  62.                 $this->setLocale($e->getRequest(), $site->getLocale());
  63.             }
  64.             //Check access only if user has set allowed sites.
  65.             //If user has not set allowed sites, then user has access to all sites.
  66.             if ($allowed && !in_array($content->getId(), $allowed)) {
  67.                 while (($siteId array_shift($allowed)) && !$sites->has($siteId)) {
  68.                     continue;
  69.                 }
  70.                 //if somehow there is no defined site that user has access to
  71.                 //then logout the user with some nice message.
  72.                 if (!$siteId) {
  73.                     $this->storage->setToken(null);
  74.                     $session->invalidate();
  75.                     if ($session instanceof Session) {
  76.                         $session->getFlashBag()->add(
  77.                             'error',
  78.                             $this->translator->trans(
  79.                                 'Nie masz uprawnień zarządzania żadną aplikacją. Skontaktuj się z Administratorem.',
  80.                                 [],
  81.                                 'admin'
  82.                             )
  83.                         );
  84.                     }
  85.                     $e->setResponse(new RedirectResponse($this->generator->generate('hitso_admin_login')));
  86.                     return;
  87.                 }
  88.                 $this->context->setContentSite($sites->get($siteId));
  89.                 $e->setResponse(new RedirectResponse($e->getRequest()->getRequestUri()));
  90.             }
  91.         }
  92.     }
  93.     private function setLocale(Request $requeststring $locale)
  94.     {
  95.         $this->translator->setLocale($locale);
  96.         $request->attributes->set('_locale'$locale);
  97.         $request->attributes->set('_route_params', ['_locale' => $locale]);
  98.         $request->setLocale($locale);
  99.     }
  100. }