src/TSMS/TsmsBundle/EventListener/TermsOfUseSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. /**
  3. * Copyright (C) SUEZ Smart Solutions - All Rights Reserved
  4. * On’Connect Gateway Management, 2018
  5. * Unauthorized copying of this file, via any medium is strictly prohibited
  6. * Proprietary and confidential
  7. * For the full copyright and license information, please report to the LICENSE CONTRACT.
  8. */
  9. namespace TSMS\TsmsBundle\EventListener;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use TSMS\CoreBundle\Entity\User;
  17. class TermsOfUseSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var RouterInterface
  21.      */
  22.     private $router;
  23.     /**
  24.      * @var TokenStorageInterface
  25.      */
  26.     private $tokenStorage;
  27.     /**
  28.      * @param RouterInterface       $router
  29.      * @param TokenStorageInterface $tokenStorage
  30.      */
  31.     public function __construct(
  32.         RouterInterface $router,
  33.         TokenStorageInterface $tokenStorage
  34.     ) {
  35.         $this->router       $router;
  36.         $this->tokenStorage $tokenStorage;
  37.     }
  38.     /**
  39.      * @return array
  40.      */
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return array(
  44.             'kernel.request' => 'onKernelRequest',
  45.         );
  46.     }
  47.     /**
  48.      * @param GetResponseEvent $event
  49.      */
  50.     public function onKernelRequest(GetResponseEvent $event)
  51.     {
  52.         if (!$this->tokenStorage->getToken()) {
  53.             return;
  54.         }
  55.         $currentUser $this->tokenStorage->getToken()->getUser();
  56.         if (!($currentUser instanceof User)) {
  57.             return;
  58.         }
  59.         if (User::EXTERNAL_USER_TYPE !== $currentUser->getType() || $currentUser->hasAcceptedTermsOfService()) {
  60.             return;
  61.         }
  62.         $redirectUrl $this->router->generate('show_terms_of_use');
  63.         $acceptUrl   $this->router->generate('accept_terms_of_use');
  64.         // To prevent an infinite loop
  65.         if (in_array($event->getRequest()->getRequestUri(), [$redirectUrl$acceptUrl])) {
  66.             return;
  67.         }
  68.         $event->setResponse(new RedirectResponse($redirectUrl));
  69.     }
  70. }