<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\ForgotPasswordType;
use App\Form\ResetPasswordType;
use App\Repository\UserRepository;
use App\Security\EmailVerifier;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
use DateTime;
class ForgotPasswordController extends AbstractController
{
private EmailVerifier $emailVerifier;
private $entityManager;
private $userRepository;
private $verifyEmail;
public function __construct(EmailVerifier $emailVerifier, EntityManagerInterface $entityManager, VerifyEmailHelperInterface $verifyEmail )
{
$this->entityManager = $entityManager;
$this->verifyEmail = $verifyEmail;
$this->emailVerifier = $emailVerifier;
}
#[Route('/forgot/password', name: 'forgot_password')]
public function sendRecoveryLink(Request $request): Response
{
$form = $this->createForm(ForgotPasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted()){
$user = $this->getDoctrine()->getManager()->getRepository(User::class)->findOneBy([
'email' => $form['email']->getData()
]
);
$this->addFlash('success', 'Un email vous a été envoyé pour redéfinir votre mot de passe.');
if ($user) {
//envoie du mail
$this->emailVerifier->sendEmailForgotPassword($user);
}
return $this->redirectToRoute('login');
}
return $this->render('acoa/security/forgot_password.html.twig', [
'form' => $form->createView()
]);
}
#[Route('/change', name: 'change_password_route')]
public function verifyUserEmail(Request $request, UserRepository $userRepository, UserPasswordHasherInterface $encoder): Response
{
$id = $request->get('id'); // retrieve the user id from the url
// Verify the user id exists and is not null
if (null === $id) {
return $this->redirectToRoute('login');
}
$user = $userRepository->find($id);
// Ensure the user exists in persistence
if (null === $user) {
return $this->redirectToRoute('login');
}
// Do not get the User's Id or Email Address from the Request object
try {
$this->verifyEmail->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getEmail());
} catch (VerifyEmailExceptionInterface $e) {
$this->addFlash('verify_email_error', $e->getReason());
return $this->redirectToRoute('app_register');
}
$form = $this->createForm(ResetPasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Mark your user as verified. e.g. switch a User::verified property to true
$data = $form->getData();
$password = $encoder->hashPassword($user, $data->getPassword());
$user->setPassword($password);
$user->setIsVerified(1);
$this->entityManager->persist($user);
$this->entityManager->flush();
$this->addFlash('success', 'Votre mot de passe a bien été modifié.');
return $this->redirectToRoute('login');
}
return $this->render('acoa/security/change_password.html.twig', [
'form' => $form->createView()
]);
}
}