<?PHP
namespace App\EventSubscriber;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use App\Entity\Destination;
use App\Entity\DossierEtat;
use App\Entity\VoyageCategorie;
use App\Entity\DossierType;
use App\Entity\VoyageEtat;
use App\Service\UserService;
use App\Service\NotificationService;
use App\Service\HistoriquePageService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController;
use Symfony\Component\HttpKernel\Controller\ErrorController;
use Symfony\Component\HttpFoundation\RedirectResponse;
class KernelSubscriber implements EventSubscriberInterface{
private $notificationService;
private $historiquePageService;
private $userService;
private $tokenStorage;
private $em;
public function __construct(NotificationService $notificationService, HistoriquePageService $historiquePageService, UserService $userService, TokenStorageInterface $tokenStorage, EntityManagerInterface $em){
$this->notificationService = $notificationService;
$this->historiquePageService = $historiquePageService;
$this->userService = $userService;
$this->tokenStorage = $tokenStorage;
$this->em = $em;
}
public function onKernelRequest(GetResponseEvent $event)
{
return;
}
public function onKernelController(FilterControllerEvent $event)
{
// Params
$controller = $event->getController();
$request = $event->getRequest();
$session = $request->getSession();
$token = $request->query->get('token');
$editorial = $request->query->get('editorial');
$dolibarr = $request->query->get('dolibarr');
// Récupération User de Dolibarr
if ($token && $this->tokenStorage->getToken() && $this->tokenStorage->getToken()->getUser() == "anon.")
{
$session->set('token', $token);
$this->handleDolibarrUser($token);
$redirectUrl = str_replace("token=" . $token, "login=1", $request->getRequestUri());
$event->setController(function() use ($redirectUrl) {
return new RedirectResponse($redirectUrl);
});
} else if($token)
{
$session->set('token', $token);
$session->set('destinations', null);
$session->set('dossierDestinations', null);
$session->set('etats', null);
$session->set('dossierEtats', null);
}
// Si Pas ErrorController
if
(
$controller
&& !$controller instanceof ErrorController
&& !$controller instanceof ProfilerController
)
{
// Interfaces
if (
isset($controller[1])
&& $controller[1]
&& strpos(get_class($controller[0]), "api") === false
&& strpos(get_class($controller[0]), "Api") === false
&& strpos($controller[1], "api") === false
&& strpos($controller[1], "toolbarAction") === false
)
{
// Path en session
$currentUrl = $session->get('currentUrl');
if
(
$token
&& !$editorial
&& strpos($currentUrl, "editorial") === false
&& strpos($currentUrl, "api") === false
&& strpos($currentUrl, "admin") === false
&& strpos($currentUrl, "import") === false
&& strpos($currentUrl, "export") === false
&& strpos($currentUrl, "download") === false
&& strpos($currentUrl, "preview") === false
&& strpos($currentUrl, "compte") === false
&& strpos($currentUrl, "document") === false
&& strpos($currentUrl, "iframe") === false
&& strpos($currentUrl, "vol") === false
&& strpos($currentUrl, "company") === false
&& strpos($currentUrl, "dossiers/tiers") === false
&& strpos($currentUrl, "dossiers/contact") === false
&& strpos($currentUrl, "dolibarr=1") === false
)
{
/*
$event->setController(function() use ($currentUrl) {
return new RedirectResponse($currentUrl);
});
*/
} else
{
$this->setUrlInSession($event);
}
if($event->isMasterRequest())
{
// Passer notifications en session
$this->setUserNotificationInSession($event);
// Passer page courante en base (pour "sur ma page")
$this->setUserCurrentPage($event);
// Passer les users connectés sur la page en session (pour "sur ma page")
$this->setCurrentPageUsersInSession($event);
if (!$session->get('destinations'))
{
// Destinations pour menet
$this->setDestinations($event);
}
}
// API
} else
{
/*
if (strpos($controller[1], "periodes") !== false)
{
$time_pre = microtime(true);
var_dump($time_pre);
}
*/
}
} else if(
$controller
&& $controller instanceof ErrorController
&& !$controller instanceof ProfilerController
)
{
/*
if (strpos($controller[1], "periodes") !== false)
{
$time_pre = microtime(true);
var_dump($time_pre);
}
*/
}
return;
}
public function setDestinations($event) {
$request = $event->getRequest();
$session = $request->getSession();
if ($this->tokenStorage->getToken()) {
$user = $this->tokenStorage->getToken()->getUser();
$destinationsList = $this->userService->getUserDestinations($user);
} else {
$destinationsList = $this->em->getRepository(Destination::class)->findBy(['actif' => true]);
}
$voyageDestinationsCategories = $this->em->getRepository(VoyageCategorie::class)->findBy(['actif' => true]);
$dossierDestinationsCategories = $this->em->getRepository(DossierType::class)->findBy(['actif' => true]);
$destinations = [];
foreach($voyageDestinationsCategories as $categorie) {
$destinations[$categorie->getId()]['voyages'] = $destinationsList;
$destinations[$categorie->getId()]['categorie'] = $categorie;
}
$dossierDestinations = [];
foreach($dossierDestinationsCategories as $categorie) {
$dossierDestinations[$categorie->getId()]['voyages'] = $destinationsList;
$dossierDestinations[$categorie->getId()]['categorie'] = $categorie;
}
$request = $event->getRequest();
$session = $request->getSession();
$session->set('destinations', $destinations);
$session->set('dossierDestinations', $dossierDestinations);
$voyageDestinationsEtats = $this->em->getRepository(VoyageEtat::class)->findBy(['actif' => true]);
$dossierDestinationsEtats = $this->em->getRepository(DossierEtat::class)->findBy(['actif' => true]);
$destinationsEtats = [];
foreach($voyageDestinationsEtats as $etat) {
$destinationsEtats[$etat->getId()]['voyages'] = $destinationsList;
$destinationsEtats[$etat->getId()]['etat'] = $etat;
}
$dossierEtats = [];
foreach($dossierDestinationsEtats as $etat) {
$dossierEtats[$etat->getId()]['voyages'] = $destinationsList;
$dossierEtats[$etat->getId()]['etat'] = $etat;
}
$session->set('etats', $destinationsEtats);
$session->set('dossierEtats', $dossierEtats);
}
public function setUrlInSession($event) {
$request = $event->getRequest();
$session = $request->getSession();
$token = $request->query->get('token');
$currentUrl = str_replace("token=" . $token, "login=1", $request->getRequestUri());
$session->set('currentUrl', $currentUrl);
}
public function handleDolibarrUser($token)
{
$hash = base64_decode($token);
$hash = ltrim($hash, "LM%");
$hash = rtrim($hash, "-VV$");
$userId = filter_var($hash, FILTER_SANITIZE_NUMBER_INT);
if (is_numeric($userId)) {
$session = new Session();
$this->userService->checkDolibarrUser($userId);
$session->set('userId', $userId);
$session->set('token', $token);
$session->set('destinations', null);
$session->set('dossierDestinations', null);
$session->set('etats', null);
$session->set('dossierEtats', null);
}
}
public function setUserNotificationInSession($event)
{
if ($this->tokenStorage->getToken()) {
$user = $this->tokenStorage->getToken()->getUser();
$request = $event->getRequest();
$session = $request->getSession();
$notifications = $this->notificationService->findUserNotificationsForPanel($user);
$session->set('userNotifications', $notifications);
$notifications = $this->notificationService->findUserViewedNotificationsForPanel($user);
$session->set('userViewedNotifications', $notifications);
}
}
public function setUserCurrentPage($event)
{
if ($this->tokenStorage->getToken()) {
$user = $this->tokenStorage->getToken()->getUser();
$request = $event->getRequest();
$session = $request->getSession();
$hisoriquePage = $this->historiquePageService->updateCurrentPage($user, $request);
$session->set('hisoriquePage', $hisoriquePage);
}
}
public function setCurrentPageUsersInSession($event)
{
if ($this->tokenStorage->getToken()) {
$user = $this->tokenStorage->getToken()->getUser();
$request = $event->getRequest();
$url = $request->getPathInfo();
$session = $request->getSession();
$connectedUsers = $this->historiquePageService->getCurrentPageConnectedUsers($user, $url);
$session->set('connectedUsers', $connectedUsers);
}
}
public static function getSubscribedEvents(){
return [
KernelEvents::REQUEST => [['onKernelRequest', 1]],
KernelEvents::CONTROLLER => [['onKernelController', 20]],
];
}
}
?>