src/Listener/RequestListener.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Controller\RestController;
  4. use App\Entity\Admin;
  5. use App\Entity\Headquarter;
  6. use App\Entity\User;
  7. use App\Entity\UserDevice;
  8. use App\Repository\AdminRepository;
  9. use App\Repository\HeadquarterRepository;
  10. use App\Repository\UserDeviceRepository;
  11. use App\Repository\UserRepository;
  12. use App\Service\Security;
  13. use App\Util\AudienceUtil;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class RequestListener extends RestController
  19. {
  20.     /**
  21.      * @throws \Exception
  22.      */
  23.     public function onKernelRequest(RequestEvent $event): ?Response
  24.     {
  25.         $request $event->getRequest();
  26.         $route $request->attributes->get('_route');
  27.         $route explode('_'$route)[0];
  28.         switch ($route) {
  29.             case 'external':
  30.                 $api_key substr($request->headers->get('Authorization'''), 7);
  31.                 $headquarter $this->em->getRepository(Headquarter::class)->findOneBy([
  32.                     'external_api_key' => $api_key,
  33.                     'status' => Headquarter::STATUS_PUBLISHED
  34.                 ]);
  35.                 if (!$headquarter instanceof Headquarter || is_null($headquarter->getExternalApiConfig())) {
  36.                     $event->setResponse($this->json(
  37.                         ['message' => $this->translator->trans('auth.headquarter_not_found')],
  38.                         Response::HTTP_UNAUTHORIZED
  39.                     ));
  40.                     return null;
  41.                 }
  42.                 $request->attributes->set('headquarter'$headquarter);
  43.                 break;
  44.             case 'fingerprint':
  45.                 $api_key = (string)$request->headers->get('apiKey'null);
  46.                 $headquarter $this->em->getRepository(Headquarter::class)->findOneBy([
  47.                     'api_key' => $api_key,
  48.                     'status' => Headquarter::STATUS_PUBLISHED
  49.                 ]);
  50.                 if (!$headquarter instanceof Headquarter) {
  51.                     $headquarter $this->em->getRepository(Headquarter::class)->findOneBy([
  52.                         'status' => Headquarter::STATUS_PUBLISHED,
  53.                         'is_generic' => true,
  54.                     ]);
  55.                 }
  56.                 if (!$headquarter instanceof Headquarter) {
  57.                     $event->setResponse($this->json(
  58.                         ['message' => $this->translator->trans('auth.headquarter_not_found')],
  59.                         Response::HTTP_UNAUTHORIZED
  60.                     ));
  61.                     return null;
  62.                 }
  63.                 $fingerPrintEnabled $headquarter->getWebConfig()['fingerprintEnabled'] ?? false;
  64.                 if (!$fingerPrintEnabled) {
  65.                     $event->setResponse($this->json(
  66.                         ['message' => $this->translator->trans('auth.headquarter_not_found')],
  67.                         Response::HTTP_UNAUTHORIZED
  68.                     ));
  69.                 }
  70.                 $request->attributes->set('headquarter'$headquarter);
  71.                 break;
  72.             case 'auth':
  73.                 $api_key = (string)$request->headers->get('apiKey'null);
  74.                 $headquarter $this->em->getRepository(Headquarter::class)->findOneBy([
  75.                     'api_key' => $api_key,
  76.                     'is_generic' => false,
  77.                     'status' => Headquarter::STATUS_PUBLISHED
  78.                 ]);
  79.                 if (!$headquarter instanceof Headquarter) {
  80.                     $event->setResponse($this->json(
  81.                         ['message' => $this->translator->trans('auth.headquarter_not_found')],
  82.                         Response::HTTP_UNAUTHORIZED
  83.                     ));
  84.                     return null;
  85.                 }
  86.                 $request->attributes->set('headquarter'$headquarter);
  87.                 break;
  88.             case 'headquarter':
  89.                 $api_key = (string)$request->headers->get('apiKey'null);
  90.                 $headquarter $this->em->getRepository(Headquarter::class)->findOneBy([
  91.                     'api_key' => $api_key,
  92.                     'is_generic' => false,
  93.                     'status' => Headquarter::STATUS_PUBLISHED
  94.                 ]);
  95.                 if (!$headquarter instanceof Headquarter) {
  96.                     $event->setResponse($this->json(
  97.                         ['message' => $this->translator->trans('auth.headquarter_not_found')],
  98.                         Response::HTTP_UNAUTHORIZED
  99.                     ));
  100.                     return null;
  101.                 }
  102.                 $request->attributes->set('headquarter'$headquarter);
  103.                 break;
  104.             case 'api':
  105.                 $api_key = (string)$request->headers->get('apiKey'null);
  106.                 $headquarter $this->em->getRepository(Headquarter::class)->findOneBy([
  107.                     'api_key' => $api_key,
  108.                     'status' => Headquarter::STATUS_PUBLISHED
  109.                 ]);
  110.                 if (!$headquarter instanceof Headquarter) {
  111.                     $event->setResponse($this->json(
  112.                         ['message' => $this->translator->trans('auth.headquarter_not_found')],
  113.                         Response::HTTP_UNAUTHORIZED
  114.                     ));
  115.                     return null;
  116.                 }
  117.                 $token = (string)$request->headers->get('token'null);
  118.                 $deviceId = (string)$request->headers->get('deviceId'null);
  119.                 $deviceType = (int)$request->headers->get('deviceType'null);
  120.                 $deviceModel = (string)$request->headers->get('deviceModel'null);
  121.                 $deviceOs = (string)$request->headers->get('deviceOs'null);
  122.                 $deviceVersion = (string)$request->headers->get('deviceVersion'null);
  123.                 $deviceNetwork = (string)$request->headers->get('deviceNetwork'null);
  124.                 if (!Security::validateToken($token)) {
  125.                     $event->setResponse($this->json(
  126.                         ['message' => $this->translator->trans('auth.invalid_or_expired_token')],
  127.                         Response::HTTP_UNAUTHORIZED
  128.                     ));
  129.                     return null;
  130.                 }
  131.                 $this->decode_token Security::decodeToken($token);
  132.                 $user $this->em->getRepository(User::class)->findOneBy([
  133.                     'hash' => $this->decode_token['user_id'] ?? null,
  134.                     'status' => User::STATUS_PUBLISHED
  135.                 ]);
  136.                 if (!$user instanceof User) {
  137.                     $event->setResponse($this->json(
  138.                         ['message' => $this->translator->trans('auth.user_not_found')],
  139.                         Response::HTTP_UNAUTHORIZED
  140.                     ));
  141.                     return null;
  142.                 }
  143.                 $device $this->em->getRepository(UserDevice::class)->findOneBy([
  144.                     'user' => $user,
  145.                     'device_id' => $deviceId,
  146.                     'type' => $deviceType
  147.                 ]);
  148.                 if (!$device instanceof UserDevice) {
  149.                     $event->setResponse($this->json(
  150.                         ['message' => $this->translator->trans('auth.device_not_found')],
  151.                         Response::HTTP_UNAUTHORIZED
  152.                     ));
  153.                     return null;
  154.                 }
  155.                 $device->setModel($deviceModel);
  156.                 $device->setOs($deviceOs);
  157.                 $device->setVersion($deviceVersion);
  158.                 $device->setNetwork($deviceNetwork);
  159.                 $this->em->persist($device);
  160.                 $user->setLastVisitDate(new \DateTime('now'));
  161.                 $this->em->persist($user);
  162.                 $this->em->flush();
  163.                 $request->attributes->set('headquarter'$user->getHeadquarter());
  164.                 $request->attributes->set('user'$user);
  165.                 $request->attributes->set('device'$device);
  166.                 $request->attributes->set('audiences_or_users', [
  167.                     'audiences' => AudienceUtil::getBy($headquarter$user),
  168.                     'users' => [$user->getId()]
  169.                 ]);
  170.                 break;
  171.             case 'cms':
  172.                 $token = (string)$request->headers->get('token'null);
  173.                 if (!Security::validateToken($token)) {
  174.                     $event->setResponse($this->json(
  175.                         ['message' => $this->translator->trans('auth.invalid_or_expired_token')],
  176.                         Response::HTTP_UNAUTHORIZED
  177.                     ));
  178.                     return null;
  179.                 }
  180.                 $this->decode_token Security::decodeToken($token);
  181.                 $admin $this->em->getRepository(Admin::class)->findOneBy([
  182.                     'hash' => $this->decode_token['user_id'] ?? null
  183.                 ]);
  184.                 if (!$admin instanceof Admin) {
  185.                     $event->setResponse($this->json(
  186.                         ['message' => $this->translator->trans('auth.user_not_found')],
  187.                         Response::HTTP_UNAUTHORIZED
  188.                     ));
  189.                     return null;
  190.                 }
  191.                 $request->attributes->set('headquarter'$admin->getHeadquarter());
  192.                 $request->attributes->set('admin'$admin);
  193.                 break;
  194.             case 'super':
  195.                 $token = (string)$request->headers->get('token'null);
  196.                 if (!Security::validateToken($token)) {
  197.                     return $event->setResponse($this->json(
  198.                         ['message' => $this->translator->trans('auth.invalid_or_expired_token')],
  199.                         Response::HTTP_UNAUTHORIZED
  200.                     ));
  201.                 }
  202.                 break;
  203.         }
  204.         return null;
  205.     }
  206. }