src/EventSubscriber/Ticket/IncidenceTypePreWriteSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Ticket;
  3. use App\Services\UtilsService;
  4. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  5. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use ApiPlatform\Core\EventListener\EventPriorities;
  11. use App\Exception\AccessDeniedException;
  12. use App\Exception\NotFoundException;
  13. use App\Services\VendorService;
  14. use App\Entity\App\Ticket\IncidenceType;
  15. use App\Entity\App\User;
  16. use App\Entity\App\Role;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class IncidenceTypePreWriteSubscriber implements EventSubscriberInterface
  19. {
  20.     private $tokenStorage;
  21.     private $authorizationChecker;
  22.     private $vendorService;
  23.     private $utilsService;
  24.     private $translator;
  25.     public function __construct(
  26.         TokenStorageInterface $tokenStorage,
  27.         AuthorizationCheckerInterface $checker,
  28.         VendorService $vendorService,
  29.         UtilsService $utilsService,
  30.         TranslatorInterface $translator
  31.     )
  32.     {
  33.         $this->tokenStorage $tokenStorage;
  34.         $this->authorizationChecker $checker;
  35.         $this->vendorService $vendorService;
  36.         $this->utilsService $utilsService;
  37.         $this->translator $translator;
  38.     }
  39.     /**
  40.      * @param ViewEvent $event
  41.      * @throws AccessDeniedException
  42.      * @throws NotFoundException
  43.      */
  44.     public function onKernelView(ViewEvent $event)
  45.     {
  46.         //Don't change the place of this if
  47.         if ($this->utilsService->isAPublicRequest($event)) {
  48.             return;
  49.         }
  50.         $incidenceType $event->getControllerResult();
  51.         $request $event->getRequest();
  52.         $method $request->getMethod();
  53.         $userCurrent $this->tokenStorage->getToken()->getUser();
  54.         if (!($incidenceType instanceof IncidenceType) ||
  55.             (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method && Request::METHOD_DELETE !== $method))
  56.             return;
  57.         if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN'))
  58.             return;
  59.         if (!$userCurrent instanceof User)
  60.             throw new NotFoundException($this->translator->trans('User not found'));
  61.         if (!$this->vendorService->isUserRoleInToVendor($incidenceType->getVendor(), $userCurrent, [Role::ROLE_ADMIN]))
  62.             throw new AccessDeniedException($this->translator->trans('Access denied'));
  63.     }
  64.     public static function getSubscribedEvents()
  65.     {
  66.         return [
  67.             KernelEvents::VIEW => ['onKernelView'EventPriorities::PRE_WRITE]
  68.         ];
  69.     }
  70. }