src/Core/EventSubscriber/RedirectLoggedUserSubscriber.php line 54

  1. <?php
  2. /**
  3.  * Copyright (c) 2019 TECLA Consulting Group oü.
  4.  * All rights reserved.
  5.  *
  6.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  7.  * All rights reserved. The methods and
  8.  * techniques described herein are considered trade secrets
  9.  * and/or confidential. Reproduction or distribution, in whole
  10.  * or in part, is forbidden except by express written permission
  11.  * of TECLA Consulting Group oü.
  12.  *
  13.  * @author    Matúš Sýkorjak <matus@tecla.no>
  14.  * @copyright 2019 TECLA Consulting Group oü
  15.  */
  16. namespace App\Core\EventSubscriber;
  17. use App\Core\Model\UserInterface;
  18. use JetBrains\PhpStorm\ArrayShape;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpKernel\Event\RequestEvent;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. use Symfony\Component\Routing\RouterInterface;
  24. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  25. class RedirectLoggedUserSubscriber implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var TokenStorageInterface
  29.      */
  30.     private $tokenStorage;
  31.     /**
  32.      * @var RouterInterface
  33.      */
  34.     private $router;
  35.     public function __construct(TokenStorageInterface $tokenStorageRouterInterface $router)
  36.     {
  37.         $this->tokenStorage $tokenStorage;
  38.         $this->router $router;
  39.     }
  40.     #[ArrayShape([KernelEvents::REQUEST => 'string'])]
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             KernelEvents::REQUEST => 'onKernelRequest',
  45.         ];
  46.     }
  47.     public function onKernelRequest(RequestEvent $event)
  48.     {
  49.         if (true === $this->isUserLogged() && true === $event->isMainRequest()) {
  50.             $currentRoute $event->getRequest()->attributes->get('_route');
  51.             if (true === $this->isAuthenticatedUserOnAnonymousPage($currentRoute)) {
  52.                 $response = new RedirectResponse($this->router->generate('app_homepage'));
  53.                 $event->setResponse($response);
  54.             }
  55.         }
  56.     }
  57.     private function isUserLogged()
  58.     {
  59.         $token $this->tokenStorage->getToken();
  60.         if (null === $token) {
  61.             return false;
  62.         }
  63.         $user $token->getUser();
  64.         return $user instanceof UserInterface;
  65.     }
  66.     private function isAuthenticatedUserOnAnonymousPage($currentRoute)
  67.     {
  68.         return \in_array(
  69.             $currentRoute,
  70.             [
  71.                 'app_login',
  72.                 'app_register',
  73.                 'app_register_confirm',
  74.                 'app.core.http.registration.email_sent',
  75.                 'app.core.http.registration.token_expired',
  76.                 'app_forgotten_password',
  77.                 'app.core.http.forgotten_password.email_sent',
  78.                 'app.core.http.forgotten_password.changed',
  79.                 'app_forgotten_password_reset',
  80.                 'app_forgotten_password_token_expired',
  81.             ]
  82.         );
  83.     }
  84. }