src/Controller/SecurityController.php line 111
<?phpnamespace App\Controller;use App\Event\AppEvents;use App\Event\UserResetPasswordEvent;use App\Model\Form\ChangePasswordForm;use App\Model\Form\ResetPasswordForm;use App\Repository\UserRepository;use DateTime;use App\Entity\User;use Doctrine\ORM\EntityManagerInterface;use Ecommerce121\UtilBundle\Controller\ControllerBase;use Ecommerce121\UtilBundle\Controller\ControllerUtil;use Symfony\Component\HttpKernel\Attribute\AsController;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Session\Session;use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Attribute\Route;use Twig\Error\LoaderError;use Twig\Error\RuntimeError;use Twig\Error\SyntaxError;#[AsController]class SecurityController extends ControllerBase{public function __construct(ControllerUtil $controllerUtil,private readonly AuthenticationUtils $authenticationUtils,private readonly UserPasswordHasherInterface $passwordHasher,private readonly EntityManagerInterface $entityManager,private readonly UserRepository $userRepository) {parent::__construct($controllerUtil);}/*** @throws SyntaxError* @throws RuntimeError* @throws LoaderError*/#[Route('/login', name: 'app_login')]public function loginAction(): Response{if ($this->getUser() instanceof User) {$dashboardPath = $this->generateUrl('app_home');return $this->redirect($dashboardPath);}// Pass the last username and error to the templatereturn $this->render('Security/login.html.twig', ['last_username' => $this->authenticationUtils->getLastUsername(),'error' => $this->authenticationUtils->getLastAuthenticationError(),]);}#[Route('/loginCheck', name: 'app_login_check')]public function loginCheckAction(){throw new \RuntimeException('This should never be reached!');}#[Route('/logout', name: 'app_logout')]public function logoutAction(){throw new \RuntimeException('This should never be reached!');}/*** @throws SyntaxError* @throws RuntimeError* @throws LoaderError*/#[Route('/resetPassword', name: 'app_reset_password')]public function resetPasswordAction(Request $request): Response{$form = $this->createForm(ResetPasswordForm::class);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {$email = $form->get('email')->getData();$user = $this->userRepository->findOneBy(['email' => $email]);if ($user instanceof User) {$user->setForgetPasswordValidUntil(new DateTime('+7 days'));$user->setForgetPasswordCode(uniqid());$this->entityManager->persist($user);$this->entityManager->flush();$this->dispatchEvent(AppEvents::USER_RESET_PASSWORD, new UserResetPasswordEvent($user));}}return $this->render('Security/resetPassword.html.twig', ['form' => $form->createView(),'formIsValid' => $form->isSubmitted() && $form->isValid(),]);}/*** @throws SyntaxError* @throws RuntimeError* @throws LoaderError*/#[Route('/changePassword/{code}', name: 'app_change_password')]public function changePasswordAction(string $code, Request $request): Response{return $this->handlePasswordChange($code, $request, 'resetPassword');}/*** @throws SyntaxError* @throws RuntimeError* @throws LoaderError*/#[Route('/update-password/{code}', name: 'app_update_user_password')]public function updateUserPassword(string $code, Request $request): Response{return $this->handlePasswordChange($code, $request, 'updatePassword');}/*** @throws SyntaxError* @throws RuntimeError* @throws LoaderError*/private function handlePasswordChange(string $code, Request $request, string $passwordAction): Response{$this->forward404Unless($code);$user = $this->userRepository->findOneBy(['forgetPasswordCode' => $code]);$oldPassword = $user?->getPassword();$session = $request->getSession();$homeUrl = $this->generateUrl('app_home');$updatePassUrl = $this->generateUrl('app_update_user_password', ['code' => $code]);if (!$user instanceof User) {/** @var Session $session */$session = $request->getSession();$session->getFlashBag()->add('message_password', 'Your code has been already used or expired.');return $this->redirect($homeUrl);}$form = $this->createForm(ChangePasswordForm::class);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {$newPassword = $form['newPassword']->getData();if ($this->passwordHasher->isPasswordValid($user, $newPassword)) {$session->getFlashBag()->add('message_password', 'Please use a different password. Old and new passwords are the same.');return $this->redirect($updatePassUrl);}$encodedPassword = $this->passwordHasher->hashPassword($user, $newPassword);$user->changePassword($encodedPassword);$user->setForgetPasswordCode(null);$user->setForgetPasswordValidUntil(null);$user->setPasswordValidUntil(new \DateTime('+90 days'));$this->entityManager->persist($user);$this->entityManager->flush();$session->getFlashBag()->add('message_password', 'Your password has been updated.');return $this->redirect($homeUrl);}return $this->render('Security/changePassword.html.twig', ['form' => $form->createView(),'code' => $code,'user' => $user,'updatePassword' => ($passwordAction === 'updatePassword'),]);}}