src/Infrastructure/EventSubscriber/ApiTokenSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Infrastructure\EventSubscriber;
  3. use App\Http\Controller\Api\V2\TokenAuthenticatedController;
  4. use App\Database\Domain\Entity\User\User;
  5. use App\Database\Domain\Repository\AccessTokenRepository;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  13. class ApiTokenSubscriber implements EventSubscriberInterface
  14. {
  15.     private ContainerInterface $container;
  16.     private AccessTokenRepository $accessTokenRepository;
  17.     private ?string $redirectResponseUrl null;
  18.     public function __construct(
  19.         ContainerInterface $container,
  20.         AccessTokenRepository $accessTokenRepository
  21.     ) {
  22.         $this->container $container;
  23.         $this->accessTokenRepository $accessTokenRepository;
  24.     }
  25.     public function onKernelController(ControllerEvent $event)
  26.     {
  27.         $controller $event->getController();
  28.         if (is_array($controller)) {
  29.             $controller $controller[0];
  30.         }
  31.         // TODO define and add TokenAuthenticatedController to needed controllers
  32.         // if ($controller instanceof TokenAuthenticatedController) {
  33.             $request $event->getRequest();
  34.             if ($request->query->has('referral_code')) {
  35.                 $request->getSession()->set('referral_code'$request->query->get('referral_code'));
  36.             }
  37.             if (!preg_match('/^\/api\/v2\/*/'$request->getRequestUri())) {
  38.                 $token $this->container->get('security.token_storage')->getToken();
  39.                 if ($token) {
  40.                     $user $token->getUser();
  41.                     $cookies $request->cookies;
  42.                     if ($user instanceof User && (!$cookies->has('token') || !$cookies->has('token-expires-at'))) {
  43.                         $this->redirectResponseUrl $this->container->get('router')->generate('user_logout');
  44.                     } elseif ($cookies->has('token') && $cookies->has('token-expires-at')) {
  45.                         $accessToken $this->accessTokenRepository->findByToken($cookies->get('token'));
  46.                         if (null !== $accessToken && $accessToken->isValid()) {
  47.                             $newToken = new UsernamePasswordToken($accessToken->getUser(), null'main'$accessToken->getUser()->getRoles());
  48.                             $session $this->container->get('session');
  49.                             $this->container->get('security.token_storage')->setToken($newToken);
  50.                             $session->set('_security_main'serialize($newToken));
  51.                             if ($session->get('login_url')) {
  52.                                 $this->redirectResponseUrl $session->get('login_url');
  53.                                 $session->remove('login_url');
  54.                             }
  55.                         }
  56.                     }
  57.                 }
  58.             }
  59.         // }
  60.     }
  61.     public function onKernelResponse(ResponseEvent $event)
  62.     {
  63.         if (null !== $this->redirectResponseUrl) {
  64.             $event->setResponse(
  65.                 new RedirectResponse($this->redirectResponseUrl)
  66.             );
  67.         }
  68.     }
  69.     public static function getSubscribedEvents(): array
  70.     {
  71.         return [
  72.             KernelEvents::CONTROLLER => 'onKernelController',
  73.             KernelEvents::RESPONSE => 'onKernelResponse',
  74.         ];
  75.     }
  76. }