src/EventSubscriber/TokenSubscriber.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Util\SecurityUtil;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Exception;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\Templating\EngineInterface;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Twig\Environment;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class TokenSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var Environment
  18.      */
  19.     protected $twig;
  20.     /**
  21.      * @var EntityManagerInterface
  22.      */
  23.     protected $entityManager;
  24.     /**
  25.      * @var SecurityUtil
  26.      */
  27.     protected $securityUtil;
  28.     /**
  29.      * @var EngineInterface
  30.      */
  31.     protected $templating;
  32.     private TranslatorInterface $translator;
  33.     /**
  34.      * TokenSubscriber constructor.
  35.      * @param Environment $twig
  36.      * @param EntityManagerInterface $entityManager
  37.      * @param SecurityUtil $securityUtil
  38.      * @param TranslatorInterface $translator
  39.      */
  40.     public function __construct(Environment $twig,
  41.                                 EntityManagerInterface $entityManager,
  42.                                 SecurityUtil $securityUtil,
  43.                                 TranslatorInterface $translator
  44.     )
  45.     {
  46.         $this->twig $twig;
  47.         $this->entityManager $entityManager;
  48.         $this->securityUtil $securityUtil;
  49.         //$this->templating = $templating;
  50.         $this->translator $translator;
  51.     }
  52.     /**
  53.      * @param RequestEvent $event
  54.      * @return void|bool
  55.      * @throws Exception
  56.      */
  57.     public function onKernelRequest(RequestEvent $event)
  58.     {
  59.         $request $event->getRequest();
  60.         $controller $event->getRequest()->attributes->get("_controller");
  61.         $systemPart explode('\\'$controller);
  62.         $methodPart explode('::'$controller);
  63.         $contentType $event->getRequest()->headers->get('Content-Type');
  64.         if (isset($systemPart[2])) {
  65.             $this->twig->addGlobal('_ENV'$_ENV);
  66.             if ($systemPart[2] == 'Pages' ) {
  67.                 if($methodPart[1] != 'forgotPasswordPage' &&
  68.                     $methodPart[1] != 'passwordResetPage' &&
  69.                     $methodPart[1] != 'passwordResetSuccessPage' &&
  70.                     $methodPart[1] != 'passwordCreationPage' &&
  71.                     $methodPart[1] != 'passwordCreationSuccessPage' &&
  72.                     $methodPart[1] != 'webManifest' &&
  73.                     $methodPart[1] != 'serviceWorkerJs' &&
  74.                     $methodPart[1] != 'translateJs' &&
  75.                     $methodPart[1] != 'registerPage' &&
  76.                     $methodPart[1] != 'registerSuccessPage'
  77.                     ){
  78.                         try {
  79.                             $accessToken $event->getRequest()->cookies->get('_token');
  80.                             $event->getRequest()->headers->set('Authorization''Bearer ' $accessToken);
  81.                             $tokenData $this->securityUtil->validateToken($event->getRequest());
  82.                             $this->twig->addGlobal('tokenData'$tokenData);
  83.                             $event->getRequest()->attributes->set('tokenData'$tokenData);
  84.                             $request->setLocale("tr");
  85.                             //$this->translator->setLocale($request->getLocale());
  86.                         } catch (Exception $e) {
  87.                             if ($methodPart[1] == 'logout') {
  88.                                 $event->setResponse($response = new RedirectResponse("/"));
  89.                             } else {
  90.                                 $viewResponse $this->twig->render('login.html.twig', []);
  91.                                 $event->setResponse(new Response($viewResponse));
  92.                             }
  93.                         }
  94.                 }
  95.             } else if ($systemPart[2] == 'Api') {
  96.                 if ($methodPart[1] != 'loginAction' &&
  97.                     $methodPart[1] != 'sendPasswordResetLinkAction' &&
  98.                     $methodPart[1] != 'resetPasswordAction' &&
  99.                     $methodPart[1] != 'registerCompanyAction'
  100.                    ) {
  101.                     try {
  102.                         $tokenData $this->securityUtil->validateToken($event->getRequest());
  103.                         $this->twig->addGlobal('tokenData'$tokenData);
  104.                         $event->getRequest()->attributes->set('tokenData'$tokenData);
  105.                     } catch (Exception $e) {
  106.                         $event->setResponse(
  107.                             new JsonResponse(
  108.                                 json_decode($e->getMessage(), true),
  109.                                 $e->getCode()
  110.                             )
  111.                         );
  112.                     }
  113.                 }
  114.             }
  115.         }
  116.     }
  117.     public static function getSubscribedEvents(): array
  118.     {
  119.         return [
  120.             RequestEvent::class => 'onKernelRequest',
  121.         ];
  122.     }
  123. }