src/Social/UserBundle/EventListener/PageLoadListener.php line 63

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSUserBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Social\UserBundle\EventListener;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Firebase\JWT\ExpiredException;
  13. use Social\FrontendBundle\Service\Mailer;
  14. use Social\UserBundle\Entity\User;
  15. use Symfony\Component\EventDispatcher\EventDispatcher;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  20. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  21. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  22. use Symfony\Component\HttpFoundation\RedirectResponse;
  23. use Firebase\JWT\JWT;
  24. use Firebase\JWT\Key;
  25. use Social\InternalBundle\Service\LocationService;
  26. /**
  27.  * Class EmailConfirmationListener
  28.  *
  29.  * @package Social\UserBundle\EventListener
  30.  */
  31. class PageLoadListener implements EventSubscriberInterface
  32. {
  33.     private $entityManager;
  34.     private $tokenStorage;
  35.     private $locationService;
  36.     public function __construct(
  37.         EntityManagerInterface $entityManager
  38.         TokenStorage $tokenStorage,
  39.         LocationService $locationService
  40.     ) {
  41.         $this->entityManager $entityManager;
  42.         $this->tokenStorage $tokenStorage;
  43.         $this->locationService $locationService;
  44.     }
  45.     /**
  46.      * @return string[]
  47.      */
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             KernelEvents::class => 'onKernelController',
  52.         ];
  53.     }
  54.     public function onKernelController(FilterControllerEvent $event)
  55.     {
  56.         $request $event->getRequest();
  57.         if (!$request) {
  58.             return;
  59.         }
  60.         
  61.         $key $_ENV['APP_MAILS_JWT_PASSWORD'];
  62.         $jwt $request->query->get('login_token');
  63.         if (!$jwt) {
  64.             return;
  65.         }
  66.         try {
  67.             $decoded =  JWT::decode($jwt, new Key($key'HS256'));
  68.         } catch (ExpiredException $exception) {
  69.             return;
  70.         }
  71.         if(!property_exists($decoded"id")) {
  72.             return;
  73.         }
  74.         $password $decoded->id;
  75.         if (!$password) {
  76.             return;
  77.         }
  78.         $em $this->entityManager;
  79.         $userRepository $em->getRepository(User::class);
  80.         $user $userRepository->find($password);
  81.         if (!$user) {
  82.             return;
  83.         }
  84.         $token = new UsernamePasswordToken($user$user->getId(), "main"$user->getRoles());
  85.         $this->tokenStorage->setToken($token);
  86.         $historyApiip $user->getHistoryApiipReference();
  87.         if (empty($historyApiip)) {
  88.             $this->locationService->generateLocationHistory($request->getClientIp(), $user);
  89.         } else if (empty($user->getNewLocation()) && !empty($historyApiip->getNewLocation())) {
  90.             $user->setNewLocation($historyApiip->getNewLocation());
  91.             $em->persist($user);
  92.             $em->flush();
  93.         }
  94.         // Fire the login event
  95.         try {
  96.             $event = new InteractiveLoginEvent($request$token);
  97.             $dispatcher = new EventDispatcher();
  98.             $dispatcher->dispatch("security.interactive_login"$event);
  99.             $domain $request->getSchemeAndHttpHost();
  100.             $newUrl $domain$this->strip_param_from_url($request->getRequestUri(), 'login_token');
  101.             if ('?' == substr($newUrl, -1)) {
  102.                 $newUrl =  substr($newUrl0, -1);
  103.             }
  104.             header("location: ".$newUrl);
  105.         } catch (\Exception $exception) {
  106.         }
  107.         return;
  108.     }
  109.     private function strip_param_from_url$url$param ) {
  110.         $base_url strtok($url'?');              // Get the base url
  111.         $parsed_url parse_url($url);              // Parse it
  112.         if (!isset($parsed_url['query'])) {
  113.             return $base_url;
  114.         }
  115.         $query $parsed_url['query'];              // Get the query string
  116.         parse_str$query$parameters );           // Convert Parameters into array
  117.         unset( $parameters[$param] );               // Delete the one you want
  118.         $new_query http_build_query($parameters); // Rebuilt query string
  119.         return $base_url.'?'.$new_query;            // Finally url is ready
  120.     }
  121. }