<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\User;
use App\Service\LogBoService;
use App\Repository\UserRepository;
use App\Utils\TokenGenerator;
use App\Form\ResetPasswordType;
use Doctrine\Persistence\ManagerRegistry;
use Carbon\CarbonImmutable;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use App\Form\ResetPasswordGestionnaireType;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use App\Utils\MailerHelper;
class ResetPasswordController extends AbstractController
{
const EXISTE="existe";
const NON_EXISTE="non_existe";
private $userRepository;
private $tokenGenerator;
private $resetPasswordType;
private $entityManager;
private $carbon;
private $parameterBag;
private $passwordHasher;
private $mailerHelper;
private $logBoService;
public function __construct(MailerHelper $mailerHelper,
UserPasswordHasherInterface $passwordHasher,
ParameterBagInterface $parameterBag,
CarbonImmutable $carbon,
UserRepository $userRepository,
TokenGenerator $tokenGenerator,
ResetPasswordType $resetPasswordType,
ManagerRegistry $doctrine,
LogBoService $logBoService
)
{
$this->userRepository=$userRepository;
$this->tokenGenerator= $tokenGenerator;
$this->resetPasswordType=$resetPasswordType;
$this->entityManager = $doctrine->getManager();
$this->carbon = $carbon;
$this->parameterBag = $parameterBag;
$this->passwordHasher=$passwordHasher;
$this->mailerHelper = $mailerHelper;
$this->logBoService = $logBoService;
}
#[Route('/reset/password', name: 'app_reset_password')]
public function index(Request $request): Response
{
$form=$this->createForm(ResetPasswordType::class);
$form->handleRequest($request);
if($form->isSubmitted() and $form->isValid())
{
$data=$form->getData();
$user=$this->userRepository->findOneBy(['email'=>$data['email']]);
if(!$user instanceof User)
{
$error=self::NON_EXISTE;
}else{
$validateCode = $this->parameterBag->get('validateCode');
$token = $this->tokenGenerator->getRandomSecureToken(200);
$user->setResetToken($token);
$user->setResetTokenAt(CarbonImmutable::now()->addMinutes($validateCode));
$this->entityManager->persist($user);
$this->entityManager->flush();
$this->mailerHelper->sendEmail($user->getEmail(),$token);
$error=self::EXISTE;
}
}
return $this->render('reset_password/index.html.twig', [
'controller_name' => 'ResetPasswordController',
'error'=>(isset($error)) ? $error : null,
'form'=>$form->createView()
]);
}
#[Route('/reset/handle', name: 'app_handle_password')]
public function handlePassword(Request $request)
{
$token=$request->query->get('token');
$user=$this->userRepository->findOneBy(['resetToken'=>$token]);
if($token == null)
{
$this->addFlash('success', 'Utilisateur non trouvé');
return $this->redirectToRoute('app_error_password');
}
if(!$user instanceof User)
{
$this->addFlash('success', 'Utilisateur non trouvé');
return $this->redirectToRoute('app_error_password');
}
if (CarbonImmutable::now()->greaterThan($user->getResetTokenAt())) {
$this->addFlash('success', 'Lien expiré');
return $this->redirectToRoute('app_error_password');
}
$form=$this->createForm(ResetPasswordGestionnaireType::class);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$resetPassword=$form->getData();
$plainPassword=$resetPassword['password'];
$hashedPassword = $this->passwordHasher->hashPassword(
$user,
$plainPassword
);
$user->setPassword($hashedPassword);
$user->setResetToken(null);
$this->entityManager->persist($user);
$this->entityManager->flush();
$this->logBoService->saveLog($user,'Reset mot de passe ');
return $this->redirectToRoute('app_succes_password');
}
return $this->render('reset_password/change.html.twig',[
'form'=>$form->createView()
]);
}
#[Route('/reset/password/succes', name: 'app_succes_password')]
public function succesPassword()
{
return $this->render('reset_password/succes.html.twig');
}
#[Route('/reset/password/error', name: 'app_error_password')]
public function errorPassword()
{
return $this->render('reset_password/error.html.twig');
}
}