src/EventSubscriber/User/UserPreWriteSubscriber.php line 60

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 18/03/2019
  6.  * Time: 09:09 AM
  7.  */
  8. namespace App\EventSubscriber\User;
  9. use App\Services\UtilsService;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ViewEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use ApiPlatform\Core\EventListener\EventPriorities;
  18. use App\Repository\App\VendorRepository;
  19. use App\Repository\App\RoleRepository;
  20. use App\Exception\AccessDeniedException;
  21. use App\Exception\NotFoundException;
  22. use App\Services\VendorService;
  23. use App\Entity\App\User;
  24. class UserPreWriteSubscriber implements EventSubscriberInterface
  25. {
  26.     private $tokenStorage;
  27.     private $authorizationChecker;
  28.     private $vendorRepository;
  29.     private $roleRepository;
  30.     private $vendorService;
  31.     private $entityManager;
  32.     private $utilsService;
  33.     public function __construct(
  34.         TokenStorageInterface $tokenStorage,
  35.         AuthorizationCheckerInterface $checker,
  36.         VendorRepository $vendorRepository,
  37.         RoleRepository $roleRepository,
  38.         VendorService $vendorService,
  39.         EntityManagerInterface $entityManager,
  40.         UtilsService $utilsService)
  41.     {
  42.         $this->tokenStorage $tokenStorage;
  43.         $this->authorizationChecker $checker;
  44.         $this->vendorService $vendorService;
  45.         $this->vendorRepository $vendorRepository;
  46.         $this->roleRepository $roleRepository;
  47.         $this->entityManager $entityManager;
  48.         $this->utilsService $utilsService;
  49.     }
  50.     /**
  51.      * @param ViewEvent $event
  52.      * @throws AccessDeniedException
  53.      * @throws NotFoundException
  54.      */
  55.     public function onKernelView(ViewEvent $event)
  56.     {
  57.         if ($this->utilsService->isAPublicRequest($event)) {
  58.             return;
  59.         }
  60.         $user $event->getControllerResult();
  61.         $method $event->getRequest()->getMethod();
  62.         $userCurrent $this->tokenStorage->getToken()->getUser();
  63.         if (!($user instanceof User) || Request::METHOD_DELETE !== $method)
  64.             return;
  65.         if (!$userCurrent instanceof User)
  66.             throw new NotFoundException('User not found');
  67.         if (!$userCurrent === $user) {
  68.             throw new AccessDeniedException('You can not delete yourself');
  69.         }
  70.         if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN'))
  71.         {
  72.             $this->onDeleteCascade($user);
  73.             return;
  74.         }
  75.         throw new AccessDeniedException('Action Denied');
  76.     }
  77.     /**
  78.      * @param User $user
  79.      */
  80.     protected function onDeleteCascade(User $user)
  81.     {
  82.         foreach ($user->getVendorStaff() as $vendorStaff) {
  83.             $this->entityManager->remove($vendorStaff);
  84.         }
  85.         $this->entityManager->flush();
  86.     }
  87.     public static function getSubscribedEvents()
  88.     {
  89.         return [
  90.             KernelEvents::VIEW => ['onKernelView'EventPriorities::PRE_WRITE]
  91.         ];
  92.     }
  93. }