src/EventSubscriber/Location/LocationPreWriteSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Location;
  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\HttpKernel\Event\ViewEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use ApiPlatform\Core\EventListener\EventPriorities;
  12. use App\Services\VendorService;
  13. use App\Exception\AccessDeniedException;
  14. use App\Exception\NotFoundException;
  15. use App\Entity\App\Location;
  16. use App\Entity\App\User;
  17. use App\Entity\App\Role;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class LocationPreWriteSubscriber implements EventSubscriberInterface
  20. {
  21.     private $tokenStorage;
  22.     private $authorizationChecker;
  23.     private $vendorService;
  24.     private $entityManager;
  25.     private $utilsService;
  26.     private $translator;
  27.     public function __construct(
  28.         TokenStorageInterface $tokenStorage,
  29.         AuthorizationCheckerInterface $checker,
  30.         VendorService $vendorService,
  31.         EntityManagerInterface $entityManager,
  32.         UtilsService $utilsService,
  33.         TranslatorInterface $translator
  34.     ) {
  35.         $this->tokenStorage $tokenStorage;
  36.         $this->authorizationChecker $checker;
  37.         $this->vendorService $vendorService;
  38.         $this->entityManager $entityManager;
  39.         $this->utilsService $utilsService;
  40.         $this->translator $translator;
  41.     }
  42.     public function onKernelView(ViewEvent $event)
  43.     {
  44.         if ($this->utilsService->isAPublicRequest($event)) {
  45.             return;
  46.         }
  47.         $location $event->getControllerResult();
  48.         $request $event->getRequest();
  49.         $method $request->getMethod();
  50.         $userCurrent $this->tokenStorage->getToken()->getUser();
  51.         if (!($location instanceof Location) ||
  52.             (Request::METHOD_POST !== $method && Request::METHOD_PUT && Request::METHOD_DELETE !== $method)
  53.         )
  54.             return;
  55.         if (Request::METHOD_DELETE === $method) {
  56.             $entityExist = [];
  57.             if (count($location->getTickets()->toArray()) > 0) {
  58.                 $entityExist[] = $this->translator->trans('tickets');
  59.             }
  60.             if (count($location->getVendorStaff()->toArray()) > 0) {
  61.                 $entityExist[] = $this->translator->trans('users');
  62.             }
  63.             if (count($location->getMaintenanceElements()->toArray()) > 0) {
  64.                 $entityExist[] = $this->translator->trans('maintenanceElement.plural');
  65.             }
  66.             if (count($location->getResources()->toArray()) > 0) {
  67.                 $entityExist[] = $this->translator->trans('resource.plural');
  68.             }
  69.             if (count($location->getTasks()->toArray()) > 0) {
  70.                 $entityExist[] = $this->translator->trans('task.plural');
  71.             }
  72.             if (count($entityExist) > 0) {
  73.                 throw new AccessDeniedException(
  74.                     sprintf('%s: %s'$this->translator->trans('Action not allowed. The location is linked to'), implode(', '$entityExist))
  75.                 ,417);
  76.             }
  77.         }
  78.         if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN'))
  79.             return;
  80.         if(!$userCurrent instanceof User)
  81.             throw new NotFoundException($this->translator->trans('User current not found'));
  82.         $controlAccess = [Role::ROLE_ADMIN];
  83.         if (!$this->vendorService->isUserRoleInToVendor($location->getVendor(), $userCurrent$controlAccess)) {
  84.             $controlAccessTranslator = [];
  85.             foreach ($controlAccess as $roleName) {
  86.                 $controlAccessTranslator[] = $this->translator->trans($roleName);
  87.             }
  88.             throw new AccessDeniedException(
  89.                 $this->translator->trans('access_allowed_only_for') . (implode(', '$controlAccessTranslator))
  90.             );
  91.         }
  92.     }
  93.     public static function getSubscribedEvents()
  94.     {
  95.         return [
  96.             KernelEvents::VIEW => ['onKernelView'EventPriorities::PRE_WRITE]
  97.         ];
  98.     }
  99. }