src/Controller/UserManagement/ResettingController.php line 25

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\UserManagement;
  4. use App\Entity\UserManagement\User;
  5. use App\Form\UserManagement\ResetPasswordType;
  6. use App\Form\UserManagement\UserEmailType;
  7. use App\Repository\UserManagement\ResettingRepository;
  8. use App\Service\UserManagement\ResettingService;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. final class ResettingController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/password/reset", methods={"GET|POST"}, name="password_reset")
  20.      */
  21.     public function passwordReset(ResettingService $serviceRequest $request): Response
  22.     {
  23.         $form $this->createForm(UserEmailType::class, []);
  24.         $form->handleRequest($request);
  25.         if ($form->isSubmitted() && $form->isValid()) {
  26.             $service->sendResetPasswordLink($request);
  27.             return $this->render('UserManagement/Resetting/password_reset_check.html.twig', [
  28.                 'email' => $request->get('user_email')['email'],
  29.             ]);
  30.         }
  31.         return $this->render('UserManagement/Resetting/password_reset.html.twig', [
  32.             'form' => $form->createView(),
  33.         ]);
  34.     }
  35.     /**
  36.      * @Route("/password/reset/{token}", methods={"GET|POST"}, name="password_reset_confirm")
  37.      */
  38.     public function passwordResetConfirm(ResettingRepository $repositoryRequest $requeststring $tokenTranslatorInterface $translator): Response
  39.     {
  40.         /** @var User $user */
  41.         $user $repository->findOneBy(['confirmation_token' => $token]);
  42.         if (!$user instanceof UserInterface) {
  43.             // Token not found.
  44.             return new RedirectResponse($this->generateUrl('app_login'));
  45.         } elseif (!$user->isPasswordRequestNonExpired($user::TOKEN_TTL)) {
  46.             // Token has expired.
  47.             $this->addFlash('error'$translator->trans('message.token_expired'));
  48.             return new RedirectResponse($this->generateUrl('password_reset'));
  49.         }
  50.         $form $this->createForm(ResetPasswordType::class, $user);
  51.         $form->handleRequest($request);
  52.         if ($form->isSubmitted() && $form->isValid()) {
  53.             $repository->setPassword($user$user->getPassword());
  54.             $this->addFlash('success'$translator->trans('message.password_has_been_reset'));
  55.             return $this->redirectToRoute('app_login');
  56.         }
  57.         return $this->render('UserManagement/Resetting/password_change.html.twig', [
  58.             'form' => $form->createView(),
  59.         ]);
  60.     }
  61. }