src/Social/UserBundle/Listener/RegistrationListener.php line 68

Open in your IDE?
  1. <?php
  2. namespace Social\UserBundle\Listener;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Social\InternalBundle\Entity\PackagesList;
  5. use Symfony\Bridge\Monolog\Logger;
  6. use Social\UserBundle\Entity\User;
  7. use Symfony\Bundle\FrameworkBundle\Routing\Router;
  8. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. /**
  14.  * Class RegistrationListener
  15.  *
  16.  * @package Social\UserBundle\Listener
  17.  */
  18. class RegistrationListener
  19. {
  20.     /**
  21.      * @var TokenStorageInterface $security_context
  22.      */
  23.     private $security_context;
  24.     /**
  25.      * @var Router $router
  26.      */
  27.     private $router;
  28.     /**
  29.      * @var Logger $logger
  30.      */
  31.     private $logger;
  32.     /**
  33.      * @var ContainerInterface $sc
  34.      */
  35.     private $sc;
  36.     /**
  37.      * @var AuthorizationCheckerInterface $authorizationChecker
  38.      */
  39.     protected $authorizationChecker;
  40.     /**
  41.      * @var EntityManagerInterface
  42.      */
  43.     private $entityManager;
  44.     /**
  45.      * RegistrationListener constructor.
  46.      *
  47.      * @param TokenStorageInterface $security_context
  48.      * @param Router $router
  49.      * @param AuthorizationCheckerInterface $authorizationChecker
  50.      * @param ContainerInterface $sc
  51.      * @param EntityManagerInterface $entityManager
  52.      */
  53.     public function __construct(
  54.         TokenStorageInterface $security_context,
  55.         Router $router,
  56.         AuthorizationCheckerInterface $authorizationChecker,
  57.         ContainerInterface $sc,
  58.         EntityManagerInterface $entityManager
  59.     ) {
  60.         $this->security_context     $security_context;
  61.         $this->router               $router;
  62.         $this->sc                   $sc;
  63.         $this->logger               $sc->get('logger');
  64.         $this->authorizationChecker $authorizationChecker;
  65.         $this->entityManager $entityManager;
  66.     }
  67.     /**
  68.      * @param GetResponseEvent $event
  69.      *
  70.      * @return GetResponseEvent
  71.      */
  72.     public function onKernelRequest(GetResponseEvent $event)
  73.     {
  74.         if (!$this->security_context->getToken()) {
  75.             return $event;
  76.         }
  77.         $user  $this->security_context->getToken()->getUser();
  78.         $route $event->getRequest()->get('_route');
  79.         if (!$route) {
  80.             return $event;
  81.         }
  82.         if (!$user instanceof User) {
  83.             return $event;
  84.         }
  85. //        if ($event->getRequest()->isXmlHttpRequest()) {
  86. //            return $event;
  87. //        }
  88.         if (in_array(
  89.                 $route,
  90.                 [
  91.                     'social_channel_authentication',
  92.                 ]
  93.             ) || strpos($route'_imagine') !== false
  94.         ) {
  95.             return $event;
  96.         }
  97.         if ($user->isEnabled() == 0) {
  98.             $this->security_context->setToken(null);
  99.         }
  100.         if ($user->getFromLandingPage() && $user->isProfileCompleted() == false) {
  101.             $now = new \DateTime();
  102.             $createdDiff $now->diff($user->getCreatedAt());
  103.             $minutesDiff $createdDiff->i;
  104.             if ($minutesDiff <= 5) {
  105.                 return $event;
  106.             }
  107.         }
  108.         $userStepsNotCompleted = [
  109.             => [
  110.                 'routes_allowed' => [
  111.                     'social_user_signup_step2',
  112.                 ],
  113.                 'route_redirect' => 'social_user_signup_step2',
  114.             ],
  115.             => [
  116.                 'routes_allowed' => [
  117.                     'social_user_signup_step3',
  118.                     'social_frontend_search_location',
  119.                     'social_frontend_search_country',
  120.                     'social_frontend_upload_photo',
  121.                 ],
  122.                 'route_redirect' => 'social_user_signup_step3',
  123.             ],
  124.             => [
  125.                 'routes_allowed' => [
  126.                     'social_user_signup_step4',
  127.                 ],
  128.                 'route_redirect' => 'social_user_signup_step4',
  129.             ],
  130.             => [
  131.                 'routes_allowed' => [
  132.                     'fos_user_registration_check_email',
  133.                     'social_registration_resend_confirmation_email',
  134.                     'fos_user_registration_confirm',
  135.                     'social_support',
  136.                     'social_account',
  137.                 ],
  138.                 'route_redirect' => 'fos_user_registration_check_email',
  139.             ],
  140.             => [
  141.                 'routes_allowed' => [
  142.                     'fos_user_registration_check_email',
  143.                     'social_registration_resend_confirmation_email',
  144.                     'fos_user_registration_confirm',
  145.                     'social_support',
  146.                 ],
  147.                 'route_redirect' => 'fos_user_registration_check_email',
  148.             ],
  149.         ];
  150.         if ($user->getExtendPeriodSignupConfirmation() && $user->isProfileCompleted() == false) {
  151.             if ($user->isExtendedPeriodSignupConfirmationValid()) {
  152.                 return $event;
  153.             } else {
  154.                 if ($user->isProfileCompleted() == false) {
  155.                     foreach ($userStepsNotCompleted as $step => $userStepNotCompleted) {
  156.                         if ($user->getLastRegistrationStep() == $step) {
  157.                             if (in_array($route$userStepNotCompleted['routes_allowed'])) {
  158.                                 return $event;
  159.                             }
  160.                             $event->setResponse(new RedirectResponse($this->router->generate($userStepNotCompleted['route_redirect'])));
  161.                             return $event;
  162.                         }
  163.                     }
  164.                 }
  165.             }
  166.         }
  167.         $packageName $user->getPackageName();
  168.         /** @var PackagesList $packageList */
  169.         $packageList $this->entityManager->getRepository(PackagesList::class)->findOneBy(['name' => $packageName]);
  170.         if ($packageList->getValue() > && $user->getHasAgreedToWaiveRights() == false && !in_array($route,
  171.                 ['social_confirm_package_usage''social_terms_conditions''social_accept_package_usage''social_frontend_search_location''social_frontend_check_location_exist''social_user_signup_step3'])) {
  172.             $event->setResponse(new RedirectResponse($this->router->generate('social_confirm_package_usage')));
  173.         }
  174.         /**
  175.          * on frontend, allow only ROLE_USER or ROLE_PREVIOUS_ADMIN
  176.          */
  177.         $isAdminRoute strpos($route'admin') !== false;
  178.         if ($this->authorizationChecker->isGranted('ROLE_SONATA_ADMIN')) {
  179.             if ($isAdminRoute == false && $route != 'social_channel_authentication' && !in_array($route,
  180.                     ['social_frontend_impersonate'])) {
  181.                 $event->setResponse(new RedirectResponse($this->router->generate('sonata_admin_dashboard')));
  182.             }
  183.         }
  184.         return $event;
  185.     }
  186. }