src/EventSubscriber/CheckAccessGlobalSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. class CheckAccessGlobalSubscriber implements EventSubscriberInterface
  11. {
  12.     private $manager;
  13.     private $allModules = array("App\Controller\MessageController""App\Controller\UserController""App\Controller\CompanyController""App\Controller\ProjectController""App\Controller\OrderController""App\Controller\ContractController""App\Controller\SupplierController""App\Controller\TimesheetController""App\Controller\DailyReportController""App\Controller\TimetableController""App\Controller\ServiceController""App\Controller\BilanController""App\Controller\ProductController","App\Controller\ClientController""App\Controller\PunchController");
  14.     /** @var  TokenStorageInterface */
  15.     protected $tokenStorage;
  16.     protected $authChecker;
  17.     public function __constructEntityManagerInterface $managerTokenStorageInterface $tokenStorageAuthorizationCheckerInterface $authChecker) {
  18.         $this->manager $manager;
  19.         $this->tokenStorage $tokenStorage;
  20.         $this->authChecker $authChecker;
  21.     }
  22.     public function onControllerEvent(ControllerEvent $event)
  23.     {
  24.         if (($this->tokenStorage->getToken() == null) OR ($this->authChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') === false OR $this->authChecker->isGranted("ROLE_ADMIN"))) return;
  25.         /** @var User */
  26.         $user $this->tokenStorage->getToken()->getUser();
  27.         $modules $user->getCompany()->getModules();
  28.         $user_modules $user->getModules();
  29.         $controller $event->getController();
  30.         // when a controller class defines multiple action methods, the controller
  31.         // is returned as [$controllerInstance, 'methodName']
  32.         if (is_array($controller)) {
  33.             $controller $controller[0];
  34.         }
  35.          //Get out if ur company is not active
  36.          if ($user AND $user->getCompany()->getIsActive() == false) {
  37.             $this->tokenStorage->setToken(null);
  38.         }
  39.         $controllerInstance get_class($controller);
  40.         if (!in_array($controllerInstance,$this->allModules) OR $controllerInstance == "App\Controller\MessageController") return;
  41.         
  42.         
  43.         //client and product are sub controllers of service
  44.         $controllerInstance = (in_array($controllerInstance, array("App\Controller\ProductController","App\Controller\ClientController"))) ? "App\Controller\ServiceController" $controllerInstance;
  45.         //Punch is sub controller of timetable
  46.         $controllerInstance $controllerInstance == "App\Controller\PunchController" "App\Controller\TimetableController" $controllerInstance;
  47.         $is_manager $this->authChecker->isGranted("ROLE_MANAGER");
  48.         $globalModules = array("App\Controller\HomeController");
  49.         if ($is_manager) {
  50.             if (is_array($globalModules) AND is_array($modules) AND !in_array($controllerInstance$globalModules) AND !in_array($controllerInstance$modules)) {
  51.                 throw new AccessDeniedHttpException("Ce module n'est pas disponnible!");
  52.             }
  53.         } else {
  54.             if (is_array($globalModules) AND is_array($modules) AND is_array($user_modules) AND !in_array($controllerInstance$globalModules) AND (!in_array($controllerInstance$modules) OR !in_array($controllerInstance$user_modules))) {
  55.                 throw new AccessDeniedHttpException("Ce module n'est pas disponnible!");
  56.             }
  57.         }
  58.         
  59.        
  60.     }
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             ControllerEvent::class => 'onControllerEvent',
  65.         ];
  66.     }
  67. }