src\Controller\ResetPasswordController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ResetPassword;
  4. use App\Entity\User;
  5. use App\Form\ResetPasswordType;
  6. use App\Mails\Mailer;
  7. use App\Repository\PubliciteRepository;
  8. use App\Repository\ServiceRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  14. class ResetPasswordController extends AbstractController
  15. {
  16.     private $em;
  17.     public function __construct(EntityManagerInterface $em)
  18.     {
  19.         $this->em=$em;
  20.     }
  21.     /**
  22.      * @Route("/password/forgot", name="reset_password")
  23.      */
  24.     public function index(Request $request,PubliciteRepository $publiciteRepository,ServiceRepository $serviceRepository)
  25.     {
  26.         $verif=$request->request->get('control_');
  27.         $email=$request->request->get('email');
  28.         if($verif == 'forgot'){
  29.             if($email != null){
  30.                 $user=$this->em->getRepository(User::class)->findOneBy(['email'=>$email,'status'=>true]);
  31.                 if($user){
  32.                     $oldRequest=$user->getResetPasswords();
  33.                     foreach ($oldRequest as $item) {
  34.                         $item->setActive(false);
  35.                     }
  36.                     $token=time().uniqid();
  37.                     $token=sha1($token);
  38.                     $expires=new \DateTime();
  39.                     $expires->modify('+1 hours');
  40.                     $resetRequest=new ResetPassword();
  41.                     $resetRequest->setActive(true)
  42.                     ->setToken($token)
  43.                     ->setExpiresAt($expires);
  44.                     $user->addResetPassword($resetRequest);
  45.                     $this->em->persist($resetRequest);
  46.                     $this->em->flush();
  47.                     //sending mail
  48.                     (new Mailer())->send($user->getEmail(),"Changement de mot de passe sur Orishas-finance",$this->renderView('mails/change_password.html.twig',[
  49.                         'token'=>$token,
  50.                         ])
  51.                     );
  52.                     $this->addFlash('success',"Un message de restauration de mot de passe est envoyé à votre adresse mail. Veuillez consulter vos messges pour continuer.");
  53.                     return $this->redirectToRoute('reset_password');
  54.                 }else{
  55.                     $this->addFlash('error',"Ce compte n'existe pas ou est désactivé.");
  56.                 }
  57.             }else{
  58.                 $this->addFlash('error',"Veuillez saisir une adresse Email valide.");
  59.             }
  60.         }
  61.         return $this->render('site/auth/forgot.html.twig', [
  62.             'email'=>$email,
  63.             'services' => $serviceRepository->FindAll(),
  64.             'publicites' => $publiciteRepository->FindAll(),
  65.         ]);
  66.     }
  67.     /**
  68.      * @Route("/password/new/{token}", name="new_password")
  69.      */
  70.     public function new_password(ResetPassword $password,Request $requestUserPasswordEncoderInterface $passwordEncoder,PubliciteRepository $publiciteRepository,ServiceRepository $serviceRepository)
  71.     {
  72.         if($password->getValidatedAt()){
  73.             return $this->redirectToRoute('app_login');
  74.         }
  75.         else if(!$password->getActive() || $password->getValidatedAt() || $password->getExpiresAt() < new \DateTime()){
  76.             $this->addFlash('error',"Lien expiré. Veuillez effectuer une nouvelle demande.");
  77.             return $this->redirectToRoute('reset_password');
  78.         }else{
  79.             $user $this->em->getRepository(User::class)->findOneBy(['id'=>$password->getUser()->getId()]);
  80.             $form $this->createForm(ResetPasswordType::class, $user);
  81.             $form->handleRequest($request);
  82.             if ($form->isSubmitted() && $form->isValid()) {
  83.                 $user->setPassword(
  84.                     $passwordEncoder->encodePassword(
  85.                         $user,
  86.                         $form->get('plainPassword')->getData()
  87.                     )
  88.                 );
  89.                 $password->setValidatedAt(new \DateTime());
  90.                 $this->em->flush();
  91.                 return $this->redirectToRoute('app_login');
  92.             }
  93.         }
  94.         return $this->render('site/auth/new_password.html.twig', [
  95.             'form' => $form->createView(),
  96.         ]);
  97.     }
  98. }