src/EventSubscriber/Company/CompanyPreWriteSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 10/05/2019
  6.  * Time: 01:53 PM
  7.  */
  8. namespace App\EventSubscriber\Company;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  11. use Symfony\Component\HttpKernel\Event\ViewEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use ApiPlatform\Core\EventListener\EventPriorities;
  18. use App\Services\VendorService;
  19. use App\Services\UtilsService;
  20. use App\Exception\AccessDeniedException;
  21. use App\Exception\NotFoundException;
  22. use App\Entity\App\company;
  23. use App\Entity\App\User;
  24. use App\Entity\App\Role;
  25. class CompanyPreWriteSubscriber implements EventSubscriberInterface
  26. {
  27.     private $tokenStorage;
  28.     private $authorizationChecker;
  29.     private $vendorService;
  30.     private $entityManager;
  31.     private $utilsService;
  32.     private $translator;
  33.     public function __construct(
  34.         TokenStorageInterface $tokenStorage,
  35.         AuthorizationCheckerInterface $checker,
  36.         VendorService $vendorService,
  37.         EntityManagerInterface $entityManager,
  38.         UtilsService $utilsService,
  39.         TranslatorInterface $translator
  40.     ){
  41.         $this->tokenStorage $tokenStorage;
  42.         $this->authorizationChecker $checker;
  43.         $this->vendorService $vendorService;
  44.         $this->entityManager $entityManager;
  45.         $this->utilsService $utilsService;
  46.         $this->translator $translator;
  47.     }
  48.     public function onKernelView(ViewEvent $event)
  49.     {
  50.         if ($this->utilsService->isAPublicRequest($event)) {
  51.             return;
  52.         }
  53.         $company $event->getControllerResult();
  54.         $request $event->getRequest();
  55.         $method $request->getMethod();
  56.         $userCurrent $this->tokenStorage->getToken()->getUser();
  57.         if (!($company instanceof Company) ||
  58.             (Request::METHOD_POST !== $method && Request::METHOD_PUT && Request::METHOD_DELETE !== $method)
  59.         )
  60.             return;
  61.         if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN'))
  62.             return;
  63.         if(!$userCurrent instanceof User)
  64.             throw new NotFoundException($this->translator->trans('User current not found'));
  65.         $controlAccess = [Role::ROLE_ADMIN];
  66.         if (!$this->vendorService->isUserRoleInToVendor($company->getVendor(), $userCurrent$controlAccess)) {
  67.             $controlAccessTranslator = [];
  68.             foreach ($controlAccess as $roleName) {
  69.                 $controlAccessTranslator[] = $this->translator->trans($roleName);
  70.             }
  71.             throw new AccessDeniedException(
  72.                 $this->translator->trans('access_allowed_only_for') . (implode(', '$controlAccessTranslator))
  73.             );
  74.         }
  75.     }
  76.     public static function getSubscribedEvents()
  77.     {
  78.         return [
  79.             KernelEvents::VIEW => ['onKernelView'EventPriorities::PRE_WRITE]
  80.         ];
  81.     }
  82. }