src/Listener/RequestListener.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Controller\RestController;
  4. use App\Entity\Customer;
  5. use App\Entity\Device;
  6. use App\Entity\Employee;
  7. use App\Entity\Collaborator;
  8. use App\Entity\Manager;
  9. use App\Repository\CustomerRepository;
  10. use App\Repository\EmployeeRepository;
  11. use App\Repository\CollaboratorRepository;
  12. use App\Repository\ManagerRepository;
  13. use App\Service\Security;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Event\RequestEvent;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. class RequestListener extends RestController
  21. {
  22.     private CustomerRepository $customerRepository;
  23.     private EmployeeRepository $employeeRepository;
  24.     private CollaboratorRepository $collaboratorRepository;
  25.     private ManagerRepository $managerRepository;
  26.     public function __construct(
  27.         EntityManagerInterface $em,
  28.         CustomerRepository     $customerRepository,
  29.         EmployeeRepository     $employeeRepository,
  30.         CollaboratorRepository $collaboratorRepository,
  31.         TranslatorInterface    $translator,
  32.         ManagerRepository      $managerRepository
  33.     )
  34.     {
  35.         $this->em $em;
  36.         $this->customerRepository $customerRepository;
  37.         $this->employeeRepository $employeeRepository;
  38.         $this->collaboratorRepository $collaboratorRepository;
  39.         $this->translator $translator;
  40.         $this->managerRepository $managerRepository;
  41.     }
  42.     private function getDevice(Request $request): Device
  43.     {
  44.         $headers $request->headers;
  45.         $deviceId $headers->get('deviceId''robot');
  46.         $deviceType $headers->get('deviceType''-1');
  47.         $deviceModel $headers->get('deviceModel'null);
  48.         $deviceVersion $headers->get('deviceVersion'null);
  49.         $deviceOs $headers->get('deviceOs'null);
  50.         $deviceNetwork $headers->get('deviceNetwork'null);
  51.         $device $this->em->getRepository(Device::class)->findOneBy(
  52.             ['device_id' => $deviceId'type' => $deviceType]
  53.         );
  54.         if (!$device instanceof Device) {
  55.             $device = new Device();
  56.             $device->setDeviceId($deviceId);
  57.             $device->setType((int)$deviceType);
  58.             $device->setModel($deviceModel);
  59.             $device->setOs($deviceOs);
  60.             $this->em->persist($device);
  61.             $this->em->flush();
  62.         }
  63.         $device->setVersion($deviceVersion);
  64.         $device->setNetwork($deviceNetwork);
  65.         $request->attributes->set('device'$device);
  66.         return $device;
  67.     }
  68.     /**
  69.      * @throws \Exception
  70.      */
  71.     public function onKernelRequest(RequestEvent $event): ?RequestEvent
  72.     {
  73.         $request $event->getRequest();
  74.         $route $request->attributes->get('_route');
  75.         $route explode('_'$route)[0];
  76.         switch ($route) {
  77.             case 'auth':
  78.                 $this->getDevice($request);
  79.                 break;
  80.             case 'api':
  81.                 $token = (string)$request->headers->get('token'null);
  82.                 if (!Security::validateToken($token)) {
  83.                     return $event->setResponse($this->json(
  84.                         ['message' => $this->translator->trans('Token inválido o expirado')],
  85.                         Response::HTTP_UNAUTHORIZED
  86.                     ));
  87.                 }
  88.                 $data Security::decodeToken($token);
  89.                 $customer $this->customerRepository->findOneBy(['hash' => $data['user_id']]);
  90.                 if (!$customer instanceof Customer) {
  91.                     return $event->setResponse($this->json(
  92.                         ['message' => $this->translator->trans('Cliente no encontrado')],
  93.                         Response::HTTP_UNAUTHORIZED
  94.                     ));
  95.                 }
  96.                 if (!is_null($request->headers->get('deviceId'))) {
  97.                     $device $this->getDevice($request);
  98.                     if ($device->getRefreshTokenExpiration() && $device->getRefreshTokenExpiration() < new \DateTime()) {
  99.                         $event->setResponse(new JsonResponse(['message' => 'Invalid or expired token'], 401));
  100.                     }
  101.                     $device->setCustomer($customer);
  102.                     $this->em->flush();
  103.                 }
  104.                 $request->attributes->set('customer'$customer);
  105.                 break;
  106.             case 'cms':
  107.                 $token = (string)$request->headers->get('token'null);
  108.                 if (!Security::validateToken($token)) {
  109.                     return $event->setResponse($this->json(
  110.                         ['message' => $this->translator->trans('Token inválido o expirado')],
  111.                         Response::HTTP_UNAUTHORIZED
  112.                     ));
  113.                 }
  114.                 $data Security::decodeToken($token);
  115.                 $employee $this->employeeRepository->findOneBy(['hash' => $data['user_id']]);
  116.                 if (!$employee instanceof Employee) {
  117.                     return $event->setResponse($this->json(
  118.                         ['message' => $this->translator->trans('Empleado no encontrado')],
  119.                         Response::HTTP_UNAUTHORIZED
  120.                     ));
  121.                 }
  122.                 $request->attributes->set('employee'$employee);
  123.                 break;
  124.             case 'col':
  125.                 $token = (string)$request->headers->get('token'null);
  126.                 if (!Security::validateToken($token)) {
  127.                     return $event->setResponse($this->json(
  128.                         ['message' => $this->translator->trans('Token inválido o expirado')],
  129.                         Response::HTTP_UNAUTHORIZED
  130.                     ));
  131.                 }
  132.                 $data Security::decodeToken($token);
  133.                 $collaborator $this->collaboratorRepository->findOneBy(['hash' => $data['user_id']]);
  134.                 if (!$collaborator instanceof Collaborator) {
  135.                     return $event->setResponse($this->json(
  136.                         ['message' => $this->translator->trans('Proveedor no encontrado')],
  137.                         Response::HTTP_UNAUTHORIZED
  138.                     ));
  139.                 }
  140.                 $request->attributes->set('collaborator'$collaborator);
  141.                 break;
  142.             case 'ent':
  143.                 $token = (string)$request->headers->get('token'null);
  144.                 if (!Security::validateToken($token)) {
  145.                     return $event->setResponse($this->json(
  146.                         ['message' => $this->translator->trans('Token inválido o expirado')],
  147.                         Response::HTTP_UNAUTHORIZED
  148.                     ));
  149.                 }
  150.                 $data Security::decodeToken($token);
  151.                 $manager $this->managerRepository->findOneBy(['hash' => $data['user_id']]);
  152.                 if (!$manager instanceof Manager) {
  153.                     return $event->setResponse($this->json(
  154.                         ['message' => $this->translator->trans('Manager no encontrado')],
  155.                         Response::HTTP_UNAUTHORIZED
  156.                     ));
  157.                 }
  158.                 $request->attributes->set('manager'$manager);
  159.                 break;
  160.         }
  161.         return null;
  162.     }
  163. }