src/EventSubscriber/Task/TaskPreWriteSubscriber.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Task;
  3. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  4. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ViewEvent;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use ApiPlatform\Core\EventListener\EventPriorities;
  12. use App\Repository\App\Task\WorkerBudgetRepository;
  13. use App\Repository\App\Task\TaskRepository;
  14. use App\Repository\App\RoleRepository;
  15. use App\Exception\AccessDeniedException;
  16. use App\Exception\NotFoundException;
  17. use App\Services\VendorService;
  18. use App\Services\UtilsService;
  19. use App\Entity\App\Task\TaskStatusLog;
  20. use App\Entity\App\Task\Task;
  21. use App\Entity\App\User;
  22. use App\Entity\App\Role;
  23. use App\Entity\Chat\Chat;
  24. class TaskPreWriteSubscriber implements EventSubscriberInterface
  25. {
  26.     private $translator;
  27.     private $tokenStorage;
  28.     private $authorizationChecker;
  29.     private $entityManager;
  30.     private $vendorService;
  31.     private $utilsService;
  32.     private $roleRepository;
  33.     private $taskRepository;
  34.     private $workerBudgetRepository;
  35.     public function __construct(
  36.         TranslatorInterface $translator,
  37.         TokenStorageInterface $tokenStorage,
  38.         AuthorizationCheckerInterface $checker,
  39.         EntityManagerInterface $entityManager,
  40.         VendorService $vendorService,
  41.         UtilsService $utilsService,
  42.         RoleRepository $roleRepository,
  43.         TaskRepository $taskRepository,
  44.         WorkerBudgetRepository $workerBudgetRepository
  45.     ) {
  46.         $this->translator $translator;
  47.         $this->tokenStorage $tokenStorage;
  48.         $this->authorizationChecker $checker;
  49.         $this->entityManager $entityManager;
  50.         $this->vendorService $vendorService;
  51.         $this->utilsService $utilsService;
  52.         $this->roleRepository $roleRepository;
  53.         $this->taskRepository $taskRepository;
  54.         $this->workerBudgetRepository $workerBudgetRepository;
  55.     }
  56.     /**
  57.      * @param ViewEvent $event
  58.      * @throws AccessDeniedException
  59.      * @throws NotFoundException
  60.      * @throws \Doctrine\ORM\NonUniqueResultException
  61.      */
  62.     public function onKernelView(ViewEvent $event)
  63.     {
  64.         if ($this->utilsService->isAPublicRequest($event)) {
  65.             return;
  66.         }
  67.         $task $event->getControllerResult();
  68.         $request $event->getRequest();
  69.         $method $request->getMethod();
  70.         $userCurrent $this->tokenStorage->getToken()->getUser();
  71.         if (!($task instanceof Task) ||
  72.             (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method && Request::METHOD_DELETE !== $method)
  73.         )
  74.             return;
  75.         if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN'))
  76.             return;
  77.         if(!($userCurrent instanceof User))
  78.             throw new NotFoundException($this->translator->trans('User current not found'));
  79.         $authorization false;
  80.         $vendor $task->getTicket()->getVendor();
  81.         $controlAccess = [Role::ROLE_ADMINRole::ROLE_TASKMASTER];
  82.         if ($this->vendorService->isUserRoleInToVendor($vendor$userCurrent$controlAccess)) {
  83.             $authorization true;
  84.         }
  85.         if (Request::METHOD_POST === $method) {
  86.             $state TaskStatusLog::STATUS_TASK_IN_PROCESS;
  87.             $task->setState($state);
  88.             $chat = new Chat();
  89.             $chat->setType(Chat::TYPE_TASK);
  90.             $this->entityManager->persist($chat);
  91.             $task->setChat($chat);
  92.         }
  93.         // restricion de capataz: solo puede editar cuando fue asignado a la tarea
  94.         //        if (Request::METHOD_PUT === $method && !$authorization) {
  95.         //            $controlAccess = [Role::ROLE_ADMIN, Role::ROLE_TASKMASTER];
  96.         //            $role[] = $this->roleRepository->findOneBy(['name' => Role::ROLE_TASKMASTER]);
  97.         //            if ($this->workerBudgetRepository->findGroupByRoles($role, $task, $userCurrent->getId()))
  98.         //                $authorization = true;
  99.         //        }
  100.         if (Request::METHOD_PUT === $method && !$authorization) {
  101.             $controlAccess = [Role::ROLE_ADMINRole::ROLE_TASKMASTERRole::ROLE_OPERATOR];
  102.             $role[] = $this->roleRepository->findOneBy(['name' => Role::ROLE_OPERATOR]);
  103.             //operator authorization to edit the task if it belongs to this
  104.             if ($this->workerBudgetRepository->findGroupByRoles($role$task$userCurrent->getId())) {
  105.                 $authorization true;
  106.             }
  107.         }
  108.         if (!$authorization) {
  109.             $controlAccessTranslator = [];
  110.             foreach ($controlAccess as $roleName) {
  111.                 $controlAccessTranslator[] = $this->translator->trans($roleName);
  112.             }
  113.             throw new AccessDeniedException(
  114.                 $this->translator->trans('access_allowed_only_for') . (implode(', '$controlAccessTranslator))
  115.             );
  116.         }
  117.         $task->changeDateStart false;
  118.         $task->changeDateEnd false;
  119.         $task->oldMaintenanceElements = [];
  120.         $task->oldConcepts = [];
  121.         
  122.         if (Request::METHOD_PUT === $method) {
  123.             $oldTask $this->taskRepository->findRangeDate($task->getId());
  124.             $task->oldMaintenanceElements $this->taskRepository->findMaintenanceElementByTask($task);
  125.             $task->oldConcepts $this->taskRepository->findConceptsByTask($task);
  126.             if ($oldTask['dateStart'] && $task->getDateStart() &&
  127.                 $oldTask['dateStart']->format('Y-m-d') !== $task->getDateStart()->format('Y-m-d')
  128.             ) {
  129.                 $task->changeDateStart true;
  130.             }
  131.             if ($oldTask['dateEnd'] && $task->getDateEnd() &&
  132.                 $oldTask['dateEnd']->format('Y-m-d') !== $task->getDateEnd()->format('Y-m-d')
  133.             ) {
  134.                 $task->changeDateEnd true;
  135.             }
  136.         }
  137.         return;
  138.     }
  139.     /**
  140.      * @param Task $task
  141.      * @return Task
  142.      * @throws \Exception
  143.      */
  144.     protected function setTimezoneDate(Task &$task)
  145.     {
  146.         if ($task->getDateStart() && !is_null($task->getDateStart())) {
  147.             $date = new \DateTime();
  148.             $date->setTimestamp($task->getDateStart()->getTimestamp());
  149.             $date->setTimezone(new \DateTimeZone('UTC'));
  150.             $task->setDateStart($date);
  151.         }
  152.         if ($task->getDateEnd() && !is_null($task->getDateEnd())) {
  153.             $date = new \DateTime();
  154.             $date->setTimestamp($task->getDateEnd()->getTimestamp());
  155.             $date->setTimezone(new \DateTimeZone('UTC'));
  156.             $task->setDateEnd($date);
  157.         }
  158.         return $task;
  159.     }
  160.     public static function getSubscribedEvents()
  161.     {
  162.         return [
  163.             KernelEvents::VIEW => ['onKernelView'EventPriorities::PRE_WRITE]
  164.         ];
  165.     }
  166. }