src/EventListener/MenuListener.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\UserActivity;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpKernel\HttpKernel;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. use App\Repository\EntrepriseRepository;
  17. use App\Repository\MenuRepository;
  18. class MenuListener {
  19.     private $em;
  20.     private $security;
  21.     private $container;
  22.     private $session;
  23.     private $entrepriseRepository;
  24.     private $menuRepository;
  25.     private $router;
  26.     private $requestStack;
  27.     public function __construct(SessionInterface $sessionEntityManagerInterface $emSecurity $securityContainerInterface $containerEntrepriseRepository $entrepriseRepositoryMenuRepository $menuRepositoryRequestStack $requestStackRouterInterface $router) {
  28.         $this->em $em;
  29.         $this->security $security;
  30.         $this->container $container;
  31.         $this->session $session;
  32.         $this->router $router;
  33.         $this->requestStack $requestStack;
  34.         $this->entrepriseRepository $entrepriseRepository;
  35.         $this->menuRepository $menuRepository;
  36.     }
  37.     public function onKernelController(FilterControllerEvent  $event) {
  38.         // Check that the current request is a "MASTER_REQUEST"
  39.         // Ignore any sub-request
  40.         if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
  41.             return;
  42.         }
  43.         // Check token authentication availability
  44.         
  45.         if ($this->security->getToken()) {
  46.             $user $this->security->getToken()->getUser();
  47.             if($user instanceof \App\Entity\Admin){
  48.                 //$this->requestStack->getCurrentRequest()->getRequestUri()
  49.                 //$hoteUrl = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost();
  50.                 //$this->requestStack->getCurrentRequest()->getUri();
  51.                 
  52.                 $allMenus $this->menuRepository->findAll();
  53.                 $tabLink = [];
  54.                 foreach ($allMenus as $value) {
  55.                     $tabLink[] = $value->getLink();
  56.                 }
  57.                 $currentUrl $this->requestStack->getCurrentRequest()->getRequestUri();
  58.                 $currentUrl ltrim($currentUrl"/");
  59.                 
  60.                 if(in_array($currentUrl$tabLink) && $currentUrl != ""){
  61.                     $menuRequest $this->menuRepository->findOneBy(['link'=>$currentUrl]); 
  62.                     $entrepriseAuthorized $menuRequest->getEntreprises();
  63.                     $entrepriseConnect $this->session->get('entreprise_session_id');
  64.                     
  65.                     if(!is_null($entrepriseConnect)){
  66.                         $found false;
  67.                         foreach ($entrepriseAuthorized as $value) {
  68.                             if($value->getId() == $entrepriseConnect){
  69.                                 return;
  70.                             }
  71.                         }   
  72.                         printf("Vous n'etes pas autorisé à acceder à cette page");
  73.                         die();
  74.                         $url $this->router->generate("page_503");
  75.                         $response = new RedirectResponse($url);
  76.                         $event->setResponse($response);
  77.                         //return new RedirectResponse('page_503');
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.     }
  83. }