src/Controller/LoginController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\UpdatePasswordType;
  4. use App\Repository\UserRepository;
  5. use App\Form\ResettingPasswordType;
  6. use Symfony\Component\Mime\Address;
  7. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
  17. class LoginController extends BaseController
  18. {
  19.     private MailerInterface $mailer;
  20.     use TargetPathTrait;
  21.     public function __construct(MailerInterface $mailer)
  22.     {
  23.         $this->mailer $mailer;
  24.     }
  25.     #[Route('/login'name'login')]
  26.     public function index(AuthenticationUtils $authenticationUtilsRequest $request): Response
  27.     {
  28.         if ($this->getUser()) {
  29.             if (!$this->getUser()->getCompleted()) {
  30.                 return $this->redirectToRoute('app_profil');
  31.             }elseif ($this->isGranted('ROLE_ADMIN') || $this->isGranted('ROLE_RD')) {
  32.                 return $this->redirectToRoute('admin_dashboard');
  33.             }else{
  34.                 return $this->redirectToRoute('front');
  35.             }
  36.         }
  37.         $error $authenticationUtils->getLastAuthenticationError();
  38.         $lastUsername $authenticationUtils->getLastUsername();
  39.         return $this->render('login/index.html.twig', [
  40.             'last_username' => $lastUsername,
  41.             'error'         => $error,
  42.         ]);
  43.     }
  44.     #[Route('/reset_password'name'reset_password')]
  45.     public function reset_password(TokenGeneratorInterface $tokenGenerator,UserRepository $userRepository,Request $request,AuthenticationUtils $authenticationUtils): Response
  46.     {
  47.         $form $this->createForm(ResettingPasswordType::class);
  48.         $form->handleRequest($request);
  49.         if ($form->isSubmitted()) {
  50.             $user $userRepository->findOneBy(['email' => $form->get('email')->getData()]);
  51.             if ($user) {
  52.                 $user->setTokenReset($tokenGenerator->generateToken());
  53.             }else {
  54.                 $this->addFlash('EmailIncorrect''Adresse email est incorrect !');
  55.                 return $this->redirectToRoute('reset_password');
  56.             }
  57.             $userRepository->add($user);
  58.             // generate a signed url and email it to the user
  59.             $protocol stripos($_SERVER['SERVER_PROTOCOL'],'https') === 'https://' 'http://';
  60.             
  61.             $email = new TemplatedEmail();
  62.             $email->from(new Address($this->getParameter('EmailAdmin'), 'IGPPP'))
  63.             ->to($user->getEmail())
  64.             ->subject('RĂ©initialisation de mot de passe')
  65.             ->htmlTemplate('email/email_reset_password.html.twig')
  66.             ->context([
  67.                 'signedUrl' => $protocol.$_SERVER["HTTP_HOST"]. "/reset/" $user->getTokenReset()
  68.             ]);
  69.             $this->mailer->send($email);
  70.             return $this->redirectToRoute('login');
  71.         }
  72.         $lastUsername $authenticationUtils->getLastUsername();
  73.         return $this->render('login/reset_password.html.twig', [
  74.             'last_username' => $lastUsername,
  75.             'form' => $form->createView(),
  76.         ]);
  77.     }
  78.     #[Route('/reset/{token}'name'change_password')]
  79.     public function change_password($token,UserPasswordHasherInterface $userPasswordHasher,UserRepository $userRepository,Request $request): Response
  80.     {
  81.         $form $this->createForm(UpdatePasswordType::class);
  82.         $form->handleRequest($request);
  83.         $user $userRepository->findOneBy(['TokenReset' => $token]);
  84.         if (!$user) {
  85.             $this->addFlash('EmailIncorrect''Token incorrect !');
  86.             return $this->redirectToRoute('login');
  87.         }
  88.         if ($form->isSubmitted()) {
  89.             // generate a signed url and email it to the user
  90.             //dd($form->get('password')->getData(), $form->get('confirmpassword')->getData());
  91.             if (strcmp($form->get('password')->getData(), $form->get('confirmpassword')->getData()) == 0) {
  92.                 $user->setPassword(
  93.                     $userPasswordHasher->hashPassword(
  94.                             $user,
  95.                             $form->get('password')->getData()
  96.                         )
  97.                     );
  98.                 $userRepository->add($user);
  99.             }else {
  100.                 $this->addFlash('PasswordError''Les mots de passe ne sont pas identiques !');
  101.                 return $this->redirect('/reset/' $token);
  102.             }
  103.             return $this->redirectToRoute('login');
  104.         }
  105.         return $this->render('login/update_password.html.twig', [
  106.             'form' => $form->createView(),
  107.         ]);
  108.     }
  109.     #[Route('/logout'name'app_logout')]
  110.     public function logout(){
  111.         return $this->redirectToRoute('login');
  112.     }
  113. }