src/EventListener/RequestListener.php line 38

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Mediterranean Develup Solutions.
  4.  * User: Carlos.Rojas
  5.  * Date: 13/06/2017
  6.  * Time: 13:50
  7.  */
  8. namespace App\EventListener;
  9. use App\Entity\User;
  10. use Doctrine\ORM\EntityManager;
  11. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  12. use Symfony\Component\BrowserKit\Response;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  15. use Symfony\Component\HttpKernel\Exception\HttpException;
  16. use Symfony\Component\HttpKernel\HttpKernel;
  17. use Symfony\Component\HttpKernel\HttpKernelInterface;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpKernel\Event\RequestEvent;
  20. use Symfony\Component\Routing\RouterInterface;
  21. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  22. class RequestListener
  23. {
  24.     protected $em;
  25.     private $router;
  26.     function __construct(EntityManager $emRouterInterface $router)
  27.     {
  28.         $this->em $em;
  29.         $this->router $router;
  30.     }
  31.     public function onKernelRequest(RequestEvent $event)
  32.     {
  33.         $session = new Session();
  34.         $token_session $session->get('access_token');
  35.         $email $session->get('_security.last_username');
  36.         if (!is_null($token_session) && ($email != 'cjdevelup_admin@develup.solutions')){
  37.             $repository $this->em->getRepository(User::class);
  38.             $usuario $repository->findOneByEmail($email);
  39.             $token_database $usuario->getAccessKey();
  40.             /*
  41.              * Si el token almacenado en sesion es distinto al token que esta en ese momento en
  42.              * la base de datos significa que alguien (en otra sesion) ha echo login con ese
  43.              * usuario.
  44.              */
  45.             if ($token_session !== $token_database){
  46.                 $session->set('access_token'null);
  47.                 $url $this->router->generate('kickout');
  48.                 $event->setResponse(new RedirectResponse($url));
  49.             }
  50.         }
  51.     }
  52. }