src/EventSubscriber/MaintenanceElement/MaintenanceElementPreSerializerSubscriber.php line 54

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