src/EventSubscriber/UserSecurityCheckSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\ApiController;
  4. use App\Controller\AuthController;
  5. use App\Controller\ClientController;
  6. use App\Controller\FrontendController;
  7. use App\Entity\User;
  8. use Gregwar\CaptchaBundle\Controller\CaptchaController;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpKernel\Event\KernelEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
  17. class UserSecurityCheckSubscriber implements EventSubscriberInterface
  18. {
  19.     public const SESSION_KEY 'auth/securityCheckPassed';
  20.     /** @var UrlGeneratorInterface */
  21.     private $urlGenerator;
  22.     /** @var User */
  23.     private $user;
  24.     /**
  25.      * UserSecurityCheckSubscriber constructor.
  26.      * @param UrlGeneratorInterface $urlGenerator
  27.      * @param TokenStorageInterface $tokenStorage
  28.      * @param Security $security
  29.      */
  30.     public function __construct(UrlGeneratorInterface $urlGeneratorTokenStorageInterface $tokenStorage)
  31.     {
  32.         $this->urlGenerator $urlGenerator;
  33.         try {
  34.             $token $tokenStorage->getToken();
  35.             if ($token instanceof PostAuthenticationGuardToken) {
  36.                 $this->user $token->getUser();
  37.             }
  38.         } catch (\Exception $e) {
  39.             $this->user null;
  40.         }
  41.     }
  42.     public function onKernelController(KernelEvent $event) {
  43.         $request $event->getRequest();
  44.         $controller $event->getController();
  45.         $controllerClassName null;
  46.         if (is_array($controller)) {
  47.             $controllerClassName get_class($controller[0]);
  48.         } else {
  49.             $controllerClassName get_class($controller);
  50.         }
  51.         $isLoggedIn $this->user !== null;
  52.         $isFullUser $isLoggedIn && $this->user->hasRole('FULL_USER');
  53.         $isSecurityCheckPassed $request->getSession()->get(self::SESSION_KEY) === true;
  54.         $protectedControllers = [ClientController::class, ApiController::class];
  55.         $allowedControllers = [FrontendController::class, CaptchaController::class];
  56.         $isControllerAllowed = !in_array($controllerClassName$protectedControllers);
  57.         $isEmailConfirmed $isLoggedIn && $this->user->getIsEmailConfirmed();
  58.         if (in_array($controllerClassName$allowedControllers)) {
  59.             return;
  60.         }
  61. //        if ($isLoggedIn && !$isEmailConfirmed) {
  62. //            if ($controllerClassName !== AuthController::class ||
  63. //                $controller[1] !== 'loginSecurityEmail'
  64. //            ) {
  65. //                $event->setController(function () {
  66. //                    return new RedirectResponse($this->urlGenerator->generate('login_security_email'));
  67. //                });
  68. //            }
  69. //
  70. //            return;
  71. //        }
  72.         if ($isFullUser && !$isControllerAllowed && !$isSecurityCheckPassed && $isEmailConfirmed) {
  73.             $event->setController(function() {
  74.                 return new RedirectResponse($this->urlGenerator->generate('login_security_check'));
  75.             });
  76.         }
  77.     }
  78.     public static function getSubscribedEvents()
  79.     {
  80.         return [
  81.             KernelEvents::CONTROLLER => 'onKernelController',
  82.         ];
  83.     }
  84. }