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

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