src/Controller/ResetPasswordController.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use App\Entity\User;
  8. use App\Service\LogBoService;
  9. use App\Repository\UserRepository;
  10. use App\Utils\TokenGenerator;
  11. use App\Form\ResetPasswordType;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Carbon\CarbonImmutable;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use App\Form\ResetPasswordGestionnaireType;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use App\Utils\MailerHelper;
  18. class ResetPasswordController extends AbstractController
  19. {
  20.     const EXISTE="existe";
  21.     const NON_EXISTE="non_existe";
  22.     private $userRepository;
  23.     private $tokenGenerator;
  24.     private $resetPasswordType;
  25.     private $entityManager;
  26.     private $carbon;
  27.     private  $parameterBag;
  28.     private $passwordHasher;
  29.     private $mailerHelper;
  30.     private $logBoService;
  31.     public function __construct(MailerHelper $mailerHelper,
  32.                                 UserPasswordHasherInterface $passwordHasher,
  33.                                 ParameterBagInterface $parameterBag,
  34.                                 CarbonImmutable $carbon,
  35.                                 UserRepository $userRepository,
  36.                                 TokenGenerator $tokenGenerator,
  37.                                 ResetPasswordType $resetPasswordType,
  38.                                 ManagerRegistry $doctrine,
  39.                                 LogBoService $logBoService
  40.     )
  41.     {
  42.         $this->userRepository=$userRepository;
  43.         $this->tokenGenerator$tokenGenerator;
  44.         $this->resetPasswordType=$resetPasswordType;
  45.         $this->entityManager $doctrine->getManager();
  46.         $this->carbon $carbon;
  47.         $this->parameterBag $parameterBag;
  48.         $this->passwordHasher=$passwordHasher;
  49.         $this->mailerHelper $mailerHelper;
  50.         $this->logBoService $logBoService;
  51.     }
  52.     #[Route('/reset/password'name'app_reset_password')]
  53.     public function index(Request $request): Response
  54.     {
  55.       
  56.      
  57.         $form=$this->createForm(ResetPasswordType::class);
  58.         $form->handleRequest($request);
  59.         if($form->isSubmitted() and $form->isValid())
  60.         {
  61.             $data=$form->getData();
  62.       
  63.             $user=$this->userRepository->findOneBy(['email'=>$data['email']]);
  64.            
  65.             if(!$user instanceof User
  66.             {
  67.                 $error=self::NON_EXISTE;
  68.             }else{
  69.                 $validateCode $this->parameterBag->get('validateCode');
  70.                 $token $this->tokenGenerator->getRandomSecureToken(200);
  71.                 $user->setResetToken($token);
  72.                 $user->setResetTokenAt(CarbonImmutable::now()->addMinutes($validateCode));
  73.                 $this->entityManager->persist($user);
  74.                 $this->entityManager->flush();
  75.                 $this->mailerHelper->sendEmail($user->getEmail(),$token);
  76.                 $error=self::EXISTE;
  77.             }
  78.         }
  79.        
  80.         return $this->render('reset_password/index.html.twig', [
  81.             'controller_name' => 'ResetPasswordController',
  82.             'error'=>(isset($error)) ? $error null,
  83.             'form'=>$form->createView()
  84.         ]);
  85.     }
  86.     #[Route('/reset/handle'name'app_handle_password')]
  87.     public function handlePassword(Request $request)
  88.     {
  89.         $token=$request->query->get('token');
  90.         $user=$this->userRepository->findOneBy(['resetToken'=>$token]);
  91.         if($token == null)
  92.         {
  93.             $this->addFlash('success''Utilisateur non trouvé');
  94.             return $this->redirectToRoute('app_error_password');
  95.         }
  96.         
  97.         if(!$user instanceof User
  98.         {
  99.             $this->addFlash('success''Utilisateur non trouvé');
  100.             return $this->redirectToRoute('app_error_password');
  101.         }
  102.         if (CarbonImmutable::now()->greaterThan($user->getResetTokenAt())) {
  103.             $this->addFlash('success''Lien expiré');
  104.             return $this->redirectToRoute('app_error_password');
  105.         }
  106.        $form=$this->createForm(ResetPasswordGestionnaireType::class);
  107.        $form->handleRequest($request);
  108.        
  109.        if($form->isSubmitted() && $form->isValid())
  110.        {
  111.             $resetPassword=$form->getData();
  112.             $plainPassword=$resetPassword['password'];
  113.             $hashedPassword $this->passwordHasher->hashPassword(
  114.                 $user,
  115.                 $plainPassword
  116.             );
  117.             $user->setPassword($hashedPassword);
  118.             $user->setResetToken(null);
  119.             $this->entityManager->persist($user);
  120.             $this->entityManager->flush();
  121.            $this->logBoService->saveLog($user,'Reset mot de passe ');
  122.            return $this->redirectToRoute('app_succes_password');
  123.        }
  124.        return $this->render('reset_password/change.html.twig',[
  125.             'form'=>$form->createView()        
  126.        ]);
  127.     }
  128.     #[Route('/reset/password/succes'name'app_succes_password')]
  129.     public function succesPassword()
  130.     {
  131.         return $this->render('reset_password/succes.html.twig');
  132.     }
  133.     #[Route('/reset/password/error'name'app_error_password')]
  134.     public function errorPassword()
  135.     {
  136.         return $this->render('reset_password/error.html.twig');   
  137.     }
  138.     
  139. }