Deprecated : Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58
Deprecated : Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler
<?php
namespace App\Controller ;
use App\Entity\User ;
use App\Form\ChangePasswordFormType ;
use App\Form\ResetPasswordRequestFormType ;
use App\Service\NotificationService ;
use Doctrine\ORM\EntityManagerInterface ;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController ;
use Symfony\Component\HttpFoundation\RedirectResponse ;
use Symfony\Component\HttpFoundation\Request ;
use Symfony\Component\HttpFoundation\Response ;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface ;
use Symfony\Component\Routing\Annotation\Route ;
use Symfony\Contracts\Translation\TranslatorInterface ;
use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait ;
use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface ;
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface ;
#[Route ( '{_locale<%app.supported_locales%>}/reinitialisation-mot-de-passe' )]
class ResetPasswordController extends AbstractController
{
use ResetPasswordControllerTrait ;
private EntityManagerInterface $entityManager ;
private ResetPasswordHelperInterface $resetPasswordHelper ;
private NotificationService $notificationService ;
/**
* @param EntityManagerInterface $entityManager
* @param ResetPasswordHelperInterface $resetPasswordHelper
* @param NotificationService $notificationService
*/
public function __construct ( EntityManagerInterface $entityManager , ResetPasswordHelperInterface $resetPasswordHelper , NotificationService $notificationService )
{
$this -> entityManager = $entityManager ;
$this -> resetPasswordHelper = $resetPasswordHelper ;
$this -> notificationService = $notificationService ;
}
/**
* Display & process form to request a password reset.
*/
#[ Route ( '' , name : 'app_forgot_password_request' )]
public function request ( Request $request ): Response
{
$form = $this -> createForm ( ResetPasswordRequestFormType ::class);
$form -> handleRequest ( $request );
if ($form -> isSubmitted () && $form -> isValid ()) {
return $this -> processSendingPasswordResetEmail (
$form -> get ( 'email' )-> getData ()
);
}
return $this -> render ( 'reset_password/request.html.twig' , [
'requestForm' => $form -> createView (),
]);
}
/**
* Confirmation page after a user has requested a password reset.
*/
#[ Route ( '/verification-email' , name : 'app_check_email' )]
public function checkEmail (): Response
{
if (null === ( $resetToken = $this -> getTokenObjectFromSession ())) {
$resetToken = $this -> resetPasswordHelper -> generateFakeResetToken ();
}
return $this -> render ( 'reset_password/check_email.html.twig' , [
'resetToken' => $resetToken ,
]);
}
/**
* Validates and process the reset URL that the user clicked in their email.
*/
#[ Route ( '/reinitialisation/{token}' , name : 'app_reset_password' )]
public function reset ( Request $request , UserPasswordHasherInterface $passwordHasher , TranslatorInterface $translator , string $token = null ): Response
{
if ($token ) {
$this -> storeTokenInSession ( $token );
return $this -> redirectToRoute ( 'app_reset_password' );
}
$token = $this -> getTokenFromSession ();
if (null === $token ) {
throw $this -> createNotFoundException ( 'No reset password token found in the URL or in the session.' );
}
try {
/** @var User|null $user */
$user = $this -> resetPasswordHelper -> validateTokenAndFetchUser ( $token );
} catch (ResetPasswordExceptionInterface $e ) {
$this -> addFlash ( 'reset_password_error' , sprintf (
'%s - %s' ,
$translator -> trans ( ResetPasswordExceptionInterface :: MESSAGE_PROBLEM_VALIDATE , [], 'ResetPasswordBundle' ),
$translator -> trans ( $e -> getReason (), [], 'ResetPasswordBundle' )
));
return $this -> redirectToRoute ( 'app_forgot_password_request' );
}
$form = $this -> createForm ( ChangePasswordFormType ::class);
$form -> handleRequest ( $request );
if ($form -> isSubmitted () && $form -> isValid ()) {
$this -> resetPasswordHelper -> removeResetRequest ( $token );
$encodedPassword = $passwordHasher -> hashPassword (
$user ,
$form -> get ( 'plainPassword' )-> getData ()
);
$user -> setRegisterToken ( NULL );
$user -> setPassword ( $encodedPassword );
$this -> entityManager -> flush ();
$this -> cleanSessionAfterReset ();
return $this -> redirectToRoute ( 'app_login' );
}
return $this -> render ( 'reset_password/reset.html.twig' , [
'resetForm' => $form -> createView (),
]);
}
private function processSendingPasswordResetEmail ( string $emailFormData ): RedirectResponse
{
$user = $this -> entityManager -> getRepository ( User ::class)-> findOneBy ([
'email' => $emailFormData ,
]);
if (!$user ) {
return $this -> redirectToRoute ( 'app_check_email' );
}
try {
$resetToken = $this -> resetPasswordHelper -> generateResetToken ( $user );
} catch (ResetPasswordExceptionInterface ) {
return $this -> redirectToRoute ( 'app_check_email' );
}
$this -> notificationService -> sendForgotPasswordEmail ( $user , $resetToken );
$this -> setTokenObjectInSession ( $resetToken );
return $this -> redirectToRoute ( 'app_check_email' );
}
}