<?php
/**
* Copyright (C) SUEZ Smart Solutions - All Rights Reserved
* On’Connect Gateway Management, 2018
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* For the full copyright and license information, please report to the LICENSE CONTRACT.
*/
namespace TSMS\TsmsBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use TSMS\CoreBundle\Entity\User;
class TermsOfUseSubscriber implements EventSubscriberInterface
{
/**
* @var RouterInterface
*/
private $router;
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @param RouterInterface $router
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(
RouterInterface $router,
TokenStorageInterface $tokenStorage
) {
$this->router = $router;
$this->tokenStorage = $tokenStorage;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return array(
'kernel.request' => 'onKernelRequest',
);
}
/**
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (!$this->tokenStorage->getToken()) {
return;
}
$currentUser = $this->tokenStorage->getToken()->getUser();
if (!($currentUser instanceof User)) {
return;
}
if (User::EXTERNAL_USER_TYPE !== $currentUser->getType() || $currentUser->hasAcceptedTermsOfService()) {
return;
}
$redirectUrl = $this->router->generate('show_terms_of_use');
$acceptUrl = $this->router->generate('accept_terms_of_use');
// To prevent an infinite loop
if (in_array($event->getRequest()->getRequestUri(), [$redirectUrl, $acceptUrl])) {
return;
}
$event->setResponse(new RedirectResponse($redirectUrl));
}
}