src/EventListener/LocaleRewriteListener.php line 53

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: guillaume
  5.  * Date: 28/03/2019
  6.  * Time: 17:23
  7.  */
  8. namespace App\EventListener;
  9. use App\Entity\Subscription;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bridge\Doctrine\RegistryInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\Routing\RouterInterface;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\Routing\RouteCollection;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  22. use Symfony\Component\Security\Core\Security;
  23. use Symfony\Contracts\Translation\TranslatorInterface;
  24. class LocaleRewriteListener implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var Symfony\Component\Routing\RouterInterface
  28.      */
  29.     private $router;
  30.     /**
  31.      * @var routeCollection \Symfony\Component\Routing\RouteCollection
  32.      */
  33.     private $routeCollection;
  34.     private $container;
  35.     private $doctrine;
  36.     private $authorizationChecker;
  37.     private $tokenStorage;
  38.     protected $security;
  39.     protected $session;
  40.     protected $translator;
  41.     public function __construct(ContainerInterface $containerEntityManagerInterface $doctrineRouterInterface $routerAuthorizationCheckerInterface $authorizationCheckerTokenStorageInterface $tokenStorageSecurity $securityTranslatorInterface $translatorSessionInterface $session)
  42.     {
  43.         $this->router $router;
  44.         $this->routeCollection $router->getRouteCollection();
  45.         $this->container $container;
  46.         $this->doctrine $doctrine;
  47.         $this->authorizationChecker $authorizationChecker;
  48.         $this->tokenStorage $tokenStorage;
  49.         $this->security $security;
  50.         $this->session $session;
  51.         $this->translator $translator;
  52.     }
  53.     public function onKernelRequest(RequestEvent $event)
  54.     {
  55.         $token $this->tokenStorage->getToken();
  56.         if ($token) {
  57.             $account $token->getUser();
  58.             if ($account && !is_string($account)) {
  59.                 if ($account->getUser()) {
  60.                     $request $event->getRequest();
  61.                     $request->setLocale($account->getUser()->getLanguage());
  62.                     $this->translator->setLocale($request->getLocale());
  63.                     if (!$this->security->isGranted("ROLE_PREVIOUS_ADMIN")) {
  64.                         $routesExceptions = ['default_logout''default_logout_abo''default_login'"default_homepage"];
  65.                         $request $event->getRequest();
  66.                         $routeName $request->get('_route');
  67.                         $noSubscription false;
  68.                         if (!$account->getUser()->getCurrentSubscription()) {
  69.                             $noSubscription true;
  70.                         } else {
  71.                             if (!$account->getUser()->getCurrentSubscription()->isActive()) {
  72.                                 $noSubscription true;
  73.                             } else {
  74.                                 if (!$account->getUser()->getCurrentSubscription()->getEndDate()) {
  75.                                     $noSubscription true;
  76.                                 } else {
  77.                                     $now = new \DateTime("now");
  78.                                     if ($now >= $account->getUser()->getCurrentSubscription()->getEndDate()) {
  79.                                         $noSubscription true;
  80.                                     }
  81.                                 }
  82.                             }
  83.                         }
  84.                         if ($noSubscription) {
  85.                             if (!in_array($routeName$routesExceptions)) {
  86.                                 $url $this->router->generate("default_logout_abo");
  87.                                 $event->setResponse(new RedirectResponse($url));
  88.                             }
  89.                         }
  90.                         $lastLoginLog $account->getLoginLog();
  91.                         if ($lastLoginLog) {
  92.                             $now = new \DateTime("now");
  93.                             if ($now->getTimestamp() - $lastLoginLog->getEndDate()->getTimestamp() <= 3600) {
  94.                                 $lastLoginLog->setEndDate($now);
  95.                                 $this->doctrine->flush();
  96.                             } else {
  97.                                 $this->tokenStorage->setToken(null);
  98.                                 $this->session->invalidate();
  99.                                 $this->session->getFlashBag()->add("danger""Votre session utilisateur a expirée. Veuillez vous reconnecter.");
  100.                                 $url $this->router->generate("default_homepage");
  101.                                 $event->setResponse(new RedirectResponse($url));
  102.                             }
  103.                         }
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.         /*$token = $this->tokenStorage->getToken();
  109.         if ($token) {
  110.             $account = $token->getUser();
  111.             if ($account && !is_string($account)) {
  112.                 if ($account->getUser() && !$this->security->isGranted('ROLE_PREVIOUS_ADMIN')) {
  113.                 }
  114.             }
  115.         }*/
  116.     }
  117.     public static function getSubscribedEvents()
  118.     {
  119.         return array(
  120.             KernelEvents::REQUEST => array(array('onKernelRequest'1)),
  121.         );
  122.     }
  123. }