src/EventSubscriber/RequireProfileSettingsSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventSubscriber;
  4. use Harmonizely\Model\UserInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Harmonizely\Core\Exception\AccountDisabledException;
  12. final class RequireProfileSettingsSubscriber implements EventSubscriberInterface
  13. {
  14.     private $tokenStorage;
  15.     private $router;
  16.     public function __construct(RouterInterface $routerTokenStorageInterface $tokenStorage)
  17.     {
  18.         $this->router $router;
  19.         $this->tokenStorage $tokenStorage;
  20.     }
  21.     /**
  22.      * {@inheritdoc}
  23.      */
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             KernelEvents::REQUEST => ['redirect'],
  28.         ];
  29.     }
  30.     public function redirect(GetResponseEvent $event)
  31.     {
  32.         if (null !== ($token $this->tokenStorage->getToken()) && ($user $token->getUser()) instanceof UserInterface) {
  33.             $request $event->getRequest();
  34.             $routeName $request->get('_route');
  35.             if (null === $routeName) {
  36.                 return;
  37.             }
  38.             $skipRoutes = [
  39.                 'hrm_welcome_index',
  40.                 'terms_acceptance_index',
  41.                 'consent_index',
  42.                 'consent_agree',
  43.                 'consent_disagree',
  44.                 'consent_disagreed',
  45.             ];
  46.             if (in_array($routeName$skipRoutestrue)) {
  47.                 return;
  48.             }
  49.             if ($user->isRequireConfirmEmail()) {
  50.                 $confirmEmailRoutes = [
  51.                     'resend_user_verification_code',
  52.                     'fos_user_registration_check_email',
  53.                     'validate_user_verification_code',
  54.                 ];
  55.                 if (!in_array($routeName$confirmEmailRoutes)) {
  56.                     if ($request->headers->has('X-AUTH-TOKEN')) {
  57.                         throw new AccountDisabledException('User email is not confirmed', [], 406);
  58.                     } else {
  59.                         $event->setResponse(new RedirectResponse($this->router->generate('fos_user_registration_check_email')));
  60.                     }
  61.                 }
  62.             }
  63.             if (null === $user->getConsent()) {
  64.                 $event->setResponse(new RedirectResponse($this->router->generate('consent_index')));
  65.             }
  66.             if (null === $user->getLocale() || null === $user->getTimeFormat()) {
  67.                 $event->setResponse(new RedirectResponse($this->router->generate('hrm_welcome_index'), 302));
  68.             }
  69.         }
  70.     }
  71. }