<?php
namespace App\EventListener;
use App\Repository\DatedocumentRepository;
use App\Repository\UserRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class SecurityListener implements EventSubscriberInterface
{
private $session, $dateDocRepo, $userRepo;
public function __construct(SessionInterface $session, DatedocumentRepository $dateDocRepo, UserRepository $userRepo)
{
$this->session = $session;
$this->dateDocRepo = $dateDocRepo;
$this->userRepo = $userRepo;
}
public function onKernelRequest(RequestEvent $event)
{
$serializedToken = $event->getRequest()->getSession()->get('_security_main');
$token = unserialize($serializedToken);
if ($token)
{
$user = $token->getUser();
$userFound = $this->userRepo->findOneById($user->getId());
$dateAccepted = null;
$plateform = 1;
switch ($userFound->getRoles()[0]) {
case 'ROLE_PHARMACIE':
$plateform = 3;
break;
case 'ROLE_GROSSISTE':
$plateform = 4;
break;
case 'ROLE_PRESCRIPTEUR':
$plateform = 5;
break;
default:
$plateform = 1;
break;
}
$dateModifCGU = $this->dateDocRepo->findDateModificationCGU($plateform);
//dd($plateform);
if (!is_null($dateModifCGU)) {
$dateModifCGU = $dateModifCGU[0]['datemodification']->format('d-m-Y');
}
if (!is_null($userFound->isCguaccepted())) {
$dateAccepted = $userFound->isCguaccepted()->format('d-m-Y');
}
if($dateAccepted < $dateModifCGU) {
$this->session->set('cgu_session', true);
}else{
$this->session->set('cgu_session', false);
}
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}