<?php
namespace App\Handler;
use App\Entity\User;
use App\Entity\UtilisateurInformation;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Doctrine\ORM\EntityManager;
use App\Entity\RefRole;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Doctrine\Persistence\ManagerRegistry;
/**
* Description of AuthenticationHandler
*
* @author Maxime Dequant <maxime.dequant@gfi.fr>
*/
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface
{
private $em;
protected $session;
public function __construct(ManagerRegistry $doctrine,SessionInterface $session)
{
$this->em = $doctrine->getManager();
$this->session = $session;
}
public function hasPasswordExpired(\DateTime $passwordUpdateDate) {
$interval = date_interval_create_from_date_string("90 days");
$passwordUpdateDate = $passwordUpdateDate->format('Y-m-d H:i:s');
$expirationDate = date_add(new \DateTime($passwordUpdateDate), $interval);
$dateNow = (new \DateTime())->setTimezone(new \DateTimeZone('Europe/Paris'));
return $expirationDate < $dateNow;
}
function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$user = $token->getUser();
$dateNow = new \DateTime();
$userFederation = $user->getIdFederation();
$userFederationActive = is_null($userFederation) || $userFederation->getFederationActive();
if ($user->getUtilisateurActif() == false) {
$this->session->set('disabled_user', true);
return new RedirectResponse('login');
} else if (!$userFederationActive) {
$this->session->set('disabled_fede', true);
return new RedirectResponse('login');
} else if ((is_null($user->getDateFinDroits()) || $user->getDateFinDroits() >= $dateNow)) {
$hasPasswordExpired = $this->hasPasswordExpired($user->getDateMajMdp());
$this->session->set('has_password_expired', $hasPasswordExpired);
$lien = $this->redirectUser($user, $dateNow);
return new RedirectResponse($lien);
} else {
$this->session->set('expired', true);
return new RedirectResponse('login');
}
}
private function redirectUser($user, \DateTime $dateNow)
{
$url = null;
if($this->session->get('has_password_expired', false)) {
return '/reinitialize';
}
if ($dateNow > $user->getDateBlocage()) {
$user_bdd = $this->em->find(UtilisateurInformation::class, $user->getIdInformationPersonnelle());
$user_bdd->setTentative(0)->setDateBlocage(NULL);
$this->em->flush();
// if ($user->getUtilisateur()->getDateConnexion() == NULL && $user->getUtilisateur()->getFirstPwd() != true) {
// return $url = '/v2/first_connection';
// } else
if ($user->getJetonMdp() != null) {
return $url = '/v2/update_password/' . $user->getJetonMdp();
}
$role = $user->getUtilisateur()->getIdRole();
$url = match ($role) {
RefRole::ROLE_DS_ADMIN, RefRole::ROLE_DS_INVITE, RefRole::ROLE_DR, RefRole::ROLE_FEDE, RefRole::ROLE_FEDE_INVITE, RefRole::ROLE_DS_SIGNATAIRE, RefRole::ROLE_DR_SIGNATAIRE => '/v2/gerer-cts/actualites/liste-actualites',
RefRole::ROLE_LECTEUR => '/v2/gerer-cts/gerer-lettres-de-missions/projets-lettres-missions',
RefRole::ROLE_GCR, RefRole::ROLE_GCS => '/v2/gerer-cts/gerer-conges/en-attente',
RefRole::ROLE_CTS => '/v2/mon-activite/actualites',
default => '/v2/mon-activite/actualites',
};
} else {
return $url = '/v2/login';
}
return $url;
}
}