src/EventSubscriber/User/UserPreSerializerSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: MEDINA
  5.  * Date: 29/03/2019
  6.  * Time: 04:37 PM
  7.  */
  8. namespace App\EventSubscriber\User;
  9. use App\Entity\App\VendorStaff;
  10. use App\Services\UtilsService;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpKernel\Event\ViewEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use ApiPlatform\Core\EventListener\EventPriorities;
  19. use App\Repository\App\VendorRepository;
  20. use App\Repository\App\UserRepository;
  21. use App\Services\VendorService;
  22. use App\Entity\App\Vendor;
  23. use App\Entity\App\User;
  24. use App\Entity\App\Role;
  25. use Symfony\Contracts\Translation\TranslatorInterface;
  26. class UserPreSerializerSubscriber  implements EventSubscriberInterface
  27. {
  28.     private $tokenStorage;
  29.     private $authorizationChecker;
  30.     private $entityManager;
  31.     private $userRepository;
  32.     private $vendorRepository;
  33.     private $vendorService;
  34.     private $utilsService;
  35.     private $translator;
  36.     public function __construct(
  37.         TokenStorageInterface $tokenStorage,
  38.         AuthorizationCheckerInterface $checker,
  39.         UserRepository $userRepository,
  40.         VendorRepository $vendorRepository,
  41.         VendorService $vendorService,
  42.         EntityManagerInterface $entityManager,
  43.         UtilsService $utilsService,
  44.         TranslatorInterface $translator)
  45.     {
  46.         $this->tokenStorage $tokenStorage;
  47.         $this->authorizationChecker $checker;
  48.         $this->entityManager $entityManager;
  49.         $this->userRepository $userRepository;
  50.         $this->vendorRepository $vendorRepository;
  51.         $this->vendorService $vendorService;
  52.         $this->utilsService $utilsService;
  53.         $this->translator $translator;
  54.     }
  55.     /**
  56.      * @param ViewEvent $event
  57.      * @throws \Exception
  58.      */
  59.     public function onKernelView(ViewEvent $event)
  60.     {
  61.         if ($this->utilsService->isAPublicRequest($event)) {
  62.             return;
  63.         }
  64.         $request $event->getRequest();
  65.         $user $event->getControllerResult();
  66.         $route $request->attributes->get('_route');
  67.         $routesItem = array(
  68.             'api_users_post_collection',
  69.             'api_users_get_item',
  70.             'api_users_put_item'
  71.         );
  72.         $routesCollection = array(
  73.             'api_users_get_collection',
  74.             'api_users_get_taskmasters_collection',
  75.             'api_users_get_operators_collection'
  76.         );
  77.         $ownRoutes = array(
  78.             'api_users_get_item',
  79.             'api_users_put_item'
  80.         );
  81.         $routesTask = array(
  82.             'api_users_get_taskmasters_collection',
  83.             'api_users_get_operators_collection'
  84.         );
  85.         if (!in_array($routearray_merge($routesItem$routesCollection)))
  86.             return;
  87.         $vendor null;
  88.         $vendorId $request->query->get('vendorStaff_vendor');
  89.         $data json_decode($request->getContent(), true);
  90.         if (isset($data['vendor']) &&
  91.             ('api_users_post_collection' == $route || 'api_users_put_item' == $route)
  92.         )
  93.             $vendorId $data['vendor'];
  94.         if ($vendorId) {
  95.             $vendor $this->vendorRepository->find($vendorId);
  96.             if (!($vendor instanceof Vendor))
  97.                 throw new \Exception('vendor not found');
  98.         }
  99.         if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN'))
  100.             return;
  101.         $locale $request->getLocale();
  102.         if ($this->tokenStorage->getToken()) {
  103.             $userCurrent $this->tokenStorage->getToken()->getUser();
  104.             if ($userCurrent instanceof User) {
  105.                 $locale $userCurrent->getLocale();
  106.             }
  107.         }
  108.         $authorization false;
  109.         $userCurrent $this->tokenStorage->getToken()->getUser();
  110.         if (is_null($vendor)) {
  111.             switch (true) {
  112.                 case (in_array($route$routesCollection)):
  113.                     $authorization true;
  114.                     break;
  115.                 case (in_array($route$ownRoutes)):
  116.                     if ($user->getId() == $userCurrent->getId()) {
  117.                         $authorization true;
  118.                     }
  119.                     break;
  120.                 default:
  121.                     $authorization false;
  122.             }
  123.         }
  124.         $controlAccess = [Role::ROLE_ADMIN];
  125.         if ($vendor instanceof Vendor && $userCurrent instanceof User) {
  126.             if(in_array($route$routesTask)) {
  127.                 $controlAccess = [Role::ROLE_ADMINRole::ROLE_TASKMASTER];
  128.             }
  129.             if ($this->vendorService->isUserRoleInToVendor($vendor$userCurrent$controlAccess))
  130.                 $authorization true;
  131.             if (in_array($route$ownRoutes) &&
  132.                 $user->getId() == $userCurrent->getId()) {
  133.                 $authorization true;
  134.             }
  135.         }
  136.         if (!$authorization && is_null($vendor) && $userCurrent instanceof User && in_array($route$ownRoutes)) {
  137.             $vendorUserCurrent $this->vendorService->getVendorsOfUserByRole($userCurrentRole::ROLE_ADMIN);
  138.             foreach ($user->getVendorStaff() as $userVendorStaff) {
  139.                 if ($userVendorStaff instanceof VendorStaff && in_array($userVendorStaff->getVendor(), $vendorUserCurrent)) {
  140.                     $authorization true;
  141.                     break;
  142.                 }
  143.             }
  144.         }
  145.         if (!$authorization) {
  146.             $controlAccessTranslator = [];
  147.             foreach ($controlAccess as $roleName) {
  148.                 $controlAccessTranslator[] = $this->translator->trans($roleName, [], null$locale);
  149.             }
  150.             $response = new Response();
  151.             $response->setContent(
  152.                 json_encode(
  153.                     [
  154.                         'detail' => $this->translator->trans('access_allowed_only_for', [], null$locale) . (implode(
  155.                                 ', ',
  156.                                 $controlAccessTranslator
  157.                             ))
  158.                     ]
  159.                 )
  160.             );
  161.             $response->setStatusCode(Response::HTTP_NOT_ACCEPTABLE);
  162.             $event->setResponse($response);
  163.             return;
  164.         }
  165.     }
  166.     public static function getSubscribedEvents()
  167.     {
  168.         return [
  169.             KernelEvents::VIEW => ['onKernelView'EventPriorities::PRE_SERIALIZE]
  170.         ];
  171.     }
  172. }