src/Labas/EventListener/ContextListener.php line 31

Open in your IDE?
  1. <?php
  2. namespace Labas\EventListener;
  3. use App\Entity\Central\Client\Client;
  4. use App\Entity\Central\User\User;
  5. use App\Service\AppManager;
  6. use App\Utils\ClientUtils;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\Routing\RouterInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. class ContextListener
  13. {
  14.     private TokenStorageInterface $tokenStorage;
  15.     private RouterInterface $router;
  16.     private FlashBagInterface $flashBag;
  17.     private AppManager $appManager;
  18.     public function __construct(AppManager $appManagerTokenStorageInterface $tokenStorageRouterInterface $routerFlashBagInterface $flashBag)
  19.     {
  20.         $this->tokenStorage $tokenStorage;
  21.         $this->router $router;
  22.         $this->flashBag $flashBag;
  23.         $this->appManager $appManager;
  24.     }
  25.     public function setClient(RequestEvent $requestEvent)
  26.     {
  27.         $token $this->tokenStorage->getToken();
  28.         $user $token !== null $token->getUser() : null;
  29.         $client $user instanceof User $user->getClient() : null;
  30.         $clientCode ClientUtils::LABAS;
  31.         if ($client instanceof Client) {
  32.             foreach($user->getRoles() as $roles){
  33.                 if ($roles === 'ROLE_LABAS') {
  34.                     $client->setCode($clientCode);
  35.                 }
  36.             }
  37.         }
  38.         if ($client instanceof Client && $client->getCode() !== $clientCode) {
  39.             $response = new RedirectResponse($this->router->generate('labas_logout'));
  40.             $requestEvent->setResponse($response);
  41.             $requestEvent->stopPropagation();
  42.             $this->flashBag->add('ERROR''ui.permission_denied');
  43.         } else {
  44.             $this->appManager->findAndSetClient('code'$clientCode);
  45. //            $this->appManager->findAndSetClient('code', 'testflight');
  46.         }
  47.     }
  48. }