src/Handler/AuthenticationHandler.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Handler;
  3. use App\Entity\User;
  4. use App\Entity\UtilisateurInformation;
  5. use Symfony\Component\HttpFoundation\Session\Session;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Doctrine\ORM\EntityManager;
  12. use App\Entity\RefRole;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use Doctrine\Persistence\ManagerRegistry;
  15. /**
  16.  * Description of AuthenticationHandler
  17.  *
  18.  * @author Maxime Dequant <maxime.dequant@gfi.fr>
  19.  */
  20. class AuthenticationHandler implements AuthenticationSuccessHandlerInterface
  21. {
  22.     private $em;
  23.     protected $session;
  24.     public function __construct(ManagerRegistry $doctrine,SessionInterface $session)
  25.     {
  26.         $this->em $doctrine->getManager();
  27.         $this->session $session;
  28.     }
  29.     public function hasPasswordExpired(\DateTime $passwordUpdateDate) {
  30.         $interval date_interval_create_from_date_string("90 days");
  31.         $passwordUpdateDate $passwordUpdateDate->format('Y-m-d H:i:s');
  32.         $expirationDate date_add(new \DateTime($passwordUpdateDate), $interval);
  33.         $dateNow = (new \DateTime())->setTimezone(new \DateTimeZone('Europe/Paris'));
  34.         return $expirationDate $dateNow;
  35.     }
  36.     function onAuthenticationSuccess(Request $requestTokenInterface $token)
  37.     {
  38.         $user $token->getUser();
  39.         $dateNow = new \DateTime();
  40.         $userFederation $user->getIdFederation();
  41.         $userFederationActive is_null($userFederation) || $userFederation->getFederationActive();
  42.         if ($user->getUtilisateurActif() == false) {
  43.             $this->session->set('disabled_user'true);
  44.             return new RedirectResponse('login');
  45.         } else if (!$userFederationActive) {
  46.             $this->session->set('disabled_fede'true);
  47.             return new RedirectResponse('login');
  48.         } else if ((is_null($user->getDateFinDroits()) || $user->getDateFinDroits() >= $dateNow)) {
  49.             $hasPasswordExpired $this->hasPasswordExpired($user->getDateMajMdp());
  50.             $this->session->set('has_password_expired'$hasPasswordExpired);
  51.            
  52.             $lien $this->redirectUser($user$dateNow);
  53.             return new RedirectResponse($lien);
  54.         } else {
  55.             $this->session->set('expired'true);
  56.             return new RedirectResponse('login');
  57.         }
  58.     }
  59.     private function redirectUser($user\DateTime $dateNow)
  60.     {
  61.         $url null;
  62.         if($this->session->get('has_password_expired'false)) {
  63.             return '/reinitialize';
  64.         }
  65.         if ($dateNow $user->getDateBlocage()) {
  66.             $user_bdd $this->em->find(UtilisateurInformation::class, $user->getIdInformationPersonnelle());
  67.             $user_bdd->setTentative(0)->setDateBlocage(NULL);
  68.             $this->em->flush();
  69.             // if ($user->getUtilisateur()->getDateConnexion() == NULL && $user->getUtilisateur()->getFirstPwd() != true) {
  70.               
  71.             //     return $url = '/v2/first_connection';
  72.             // } else
  73.              if ($user->getJetonMdp() != null) {
  74.                 return $url '/v2/update_password/' $user->getJetonMdp();
  75.             }
  76.             $role $user->getUtilisateur()->getIdRole();
  77.             $url = match ($role) {
  78.                 RefRole::ROLE_DS_ADMINRefRole::ROLE_DS_INVITERefRole::ROLE_DRRefRole::ROLE_FEDERefRole::ROLE_FEDE_INVITERefRole::ROLE_DS_SIGNATAIRERefRole::ROLE_DR_SIGNATAIRE => '/v2/gerer-cts/actualites/liste-actualites',
  79.                 RefRole::ROLE_LECTEUR => '/v2/gerer-cts/gerer-lettres-de-missions/projets-lettres-missions',
  80.                 RefRole::ROLE_GCRRefRole::ROLE_GCS => '/v2/gerer-cts/gerer-conges/en-attente',
  81.                 RefRole::ROLE_CTS => '/v2/mon-activite/actualites',
  82.                 default => '/v2/mon-activite/actualites',
  83.             };
  84.         } else {
  85.             return $url '/v2/login';
  86.         }
  87.         return $url;
  88.     }
  89. }