<?php
namespace App\Controller;
use App\Entity\ResetPassword;
use App\Entity\User;
use App\Form\ResetPasswordType;
use App\Mails\Mailer;
use App\Repository\PubliciteRepository;
use App\Repository\ServiceRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class ResetPasswordController extends AbstractController
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em=$em;
}
/**
* @Route("/password/forgot", name="reset_password")
*/
public function index(Request $request,PubliciteRepository $publiciteRepository,ServiceRepository $serviceRepository)
{
$verif=$request->request->get('control_');
$email=$request->request->get('email');
if($verif == 'forgot'){
if($email != null){
$user=$this->em->getRepository(User::class)->findOneBy(['email'=>$email,'status'=>true]);
if($user){
$oldRequest=$user->getResetPasswords();
foreach ($oldRequest as $item) {
$item->setActive(false);
}
$token=time().uniqid();
$token=sha1($token);
$expires=new \DateTime();
$expires->modify('+1 hours');
$resetRequest=new ResetPassword();
$resetRequest->setActive(true)
->setToken($token)
->setExpiresAt($expires);
$user->addResetPassword($resetRequest);
$this->em->persist($resetRequest);
$this->em->flush();
//sending mail
(new Mailer())->send($user->getEmail(),"Changement de mot de passe sur Orishas-finance",$this->renderView('mails/change_password.html.twig',[
'token'=>$token,
])
);
$this->addFlash('success',"Un message de restauration de mot de passe est envoyé à votre adresse mail. Veuillez consulter vos messges pour continuer.");
return $this->redirectToRoute('reset_password');
}else{
$this->addFlash('error',"Ce compte n'existe pas ou est désactivé.");
}
}else{
$this->addFlash('error',"Veuillez saisir une adresse Email valide.");
}
}
return $this->render('site/auth/forgot.html.twig', [
'email'=>$email,
'services' => $serviceRepository->FindAll(),
'publicites' => $publiciteRepository->FindAll(),
]);
}
/**
* @Route("/password/new/{token}", name="new_password")
*/
public function new_password(ResetPassword $password,Request $request, UserPasswordEncoderInterface $passwordEncoder,PubliciteRepository $publiciteRepository,ServiceRepository $serviceRepository)
{
if($password->getValidatedAt()){
return $this->redirectToRoute('app_login');
}
else if(!$password->getActive() || $password->getValidatedAt() || $password->getExpiresAt() < new \DateTime()){
$this->addFlash('error',"Lien expiré. Veuillez effectuer une nouvelle demande.");
return $this->redirectToRoute('reset_password');
}else{
$user = $this->em->getRepository(User::class)->findOneBy(['id'=>$password->getUser()->getId()]);
$form = $this->createForm(ResetPasswordType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form->get('plainPassword')->getData()
)
);
$password->setValidatedAt(new \DateTime());
$this->em->flush();
return $this->redirectToRoute('app_login');
}
}
return $this->render('site/auth/new_password.html.twig', [
'form' => $form->createView(),
]);
}
}