src/Controller/SecurityController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\OperateursCentres;
  4. use App\Manager\Mail\SendMailManager;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Manager\Admincentre\Template\TemplateListManager;
  9. use App\Service\Metier\CentreServicesParametresSM;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. class SecurityController extends AbstractController
  17. {
  18.     private CentreServicesParametresSM $centreServicesParametresSM;
  19.     public function __construct(
  20.         CentreServicesParametresSM $centreServicesParametresSM
  21.     ){
  22.         $this->centreServicesParametresSM $centreServicesParametresSM;
  23.     }
  24.     
  25.     /**
  26.      * @Route("/", name="app_redirect_to_login")
  27.      */
  28.     public function redirectToLogin(AuthenticationUtils $authenticationUtils): Response
  29.     {
  30.         return $this->redirectToRoute('app_login');
  31.     }
  32.     /**
  33.      * @Route("/login", name="app_login")
  34.      */
  35.     public function login(AuthenticationUtils $authenticationUtils): Response
  36.     {
  37.         // if ($this->getUser()) {
  38.         //    $this->redirectToRoute('target_path');
  39.         // }
  40.         
  41.         $error $authenticationUtils->getLastAuthenticationError();
  42.         $lastUsername $authenticationUtils->getLastUsername();
  43.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  44.     }
  45.     /**
  46.      * @Route("/logout", name="app_logout")
  47.      */
  48.     public function logout(UrlGeneratorInterface $urlGenerator)
  49.     {
  50.         throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
  51.     }
  52.     /**
  53.      * @Route("/mot-de-passe-oublie", name="app_forgotten_password")
  54.      */
  55.     public function forgottenPassword(Request $requestTokenGeneratorInterface $tokenGeneratorManagerRegistry $doctrine): Response
  56.     {
  57.         if ($request->isMethod('POST')) {
  58.             $email $request->request->get('email');
  59.             $entityManager $doctrine->getManager();
  60.             $user $entityManager->getRepository(OperateursCentres::class)->findOneBy(['email' => $email]);
  61.             
  62.             if ($user === null || $user->getEtatArchivage()) {
  63.                 $this->addFlash('danger''Impossible de réinitialiser votre mot de passe. Veuillez réessayer ou contacter l\'administrateur.');
  64.                 return $this->redirectToRoute('app_forgotten_password');
  65.             }
  66.             $token $tokenGenerator->generateToken();
  67.             $user->setResetToken($token);
  68.             $entityManager->flush();
  69.             $url $this->generateUrl('app_reset_password', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL);
  70.             $template TemplateListManager::getTemplateByCodeAndIdCentre($entityManagerTemplateListManager::RESET_MDP_INCALL$user->getCentre()->getIdCentre());
  71.             $params['idTemplate'] = $template->getIdSendiblue();
  72.             $params['data'] = array(
  73.                 'token'=> $url
  74.             );
  75.             $apiKey $this->centreServicesParametresSM->getBrevoApiKey();
  76.             $responseSend SendMailManager::sendinBlueEmailInCallWithParam($user->getEmail(), $apiKey$params);
  77.             if (!isset($responseSend['statut']) || $responseSend['statut'] !== 'Success') {
  78.                 $this->addFlash('danger'"L'email n'a pas pu être envoyé en raison d'un incident technique. Merci de réessayer l'envoi. Si l'incident persiste, veuillez contacter le support technique inCall.");
  79.                 return $this->redirectToRoute('app_forgotten_password');
  80.             }
  81.             return $this->render('security/mail-sent.html.twig');
  82.         }
  83.         return $this->render('security/forgotten_password.html.twig');
  84.     }
  85.     /**
  86.      * @Route("/reset_password/{token}", name="app_reset_password")
  87.      */
  88.     public function resetPassword(Request $requeststring $tokenUserPasswordHasherInterface $passwordHasherManagerRegistry $doctrine)
  89.     {
  90.         if ($request->isMethod('POST')) {
  91.             $entityManager $doctrine->getManager();
  92.             $user $entityManager->getRepository(OperateursCentres::class)->findOneBy(['resetToken' => $token]);
  93.             
  94.             if ($user === null) {
  95.                 $this->addFlash('danger''Token Inconnu');
  96.                 return $this->redirectToRoute('app_reset_password', ['token' => $token]);
  97.             }
  98.             $user->setResetToken(null);
  99.             $user->setPassword($passwordHasher->hashPassword($user$request->request->get('password')));
  100.             $entityManager->flush();
  101.             return $this->render('security/success_changed_password.html.twig', ['token' => $token]);
  102.         } else {
  103.             return $this->render('security/reset_password.html.twig', ['token' => $token]);
  104.         }
  105.     }
  106. }