diff --git a/src/Action/CheckEmailAction.php b/src/Action/CheckEmailAction.php index bc84f8c9..a903f4d1 100644 --- a/src/Action/CheckEmailAction.php +++ b/src/Action/CheckEmailAction.php @@ -19,6 +19,9 @@ use Symfony\Component\Routing\RouterInterface; use Twig\Environment; +/** + * @deprecated + */ final class CheckEmailAction { private readonly Environment $twig; diff --git a/src/Action/RequestResetAction.php b/src/Action/RequestResetAction.php index 3a30556a..95bfc336 100644 --- a/src/Action/RequestResetAction.php +++ b/src/Action/RequestResetAction.php @@ -25,10 +25,13 @@ use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; +use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Security\Core\Exception\UserNotFoundException; use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; +use Symfony\Contracts\Translation\TranslatorInterface; use Twig\Environment; final class RequestResetAction @@ -50,6 +53,7 @@ final class RequestResetAction private readonly int $retryTtl; private readonly UserProviderInterface $userProvider; + private TranslatorInterface $translator; public function __construct( Environment $twig, @@ -60,7 +64,8 @@ public function __construct( TokenGenerator $tokenGenerator, UserProviderInterface $userProvider, ResettingMailer $mailer, - int $retryTtl + int $retryTtl, + TranslatorInterface $translator ) { $this->twig = $twig; $this->formFactory = $formFactory; @@ -71,16 +76,11 @@ public function __construct( $this->userProvider = $userProvider; $this->mailer = $mailer; $this->retryTtl = $retryTtl; + $this->translator = $translator; } public function __invoke(Request $request): Response { - $response = $this->process($request); - - if (null !== $response) { - return $response; - } - $form = $this->formFactory ->create(RequestPasswordFormType::class, null, [ 'action' => $this->router->generate('nucleos_user_resetting_request'), @@ -90,6 +90,23 @@ public function __invoke(Request $request): Response 'label' => 'resetting.request.submit', ]) ; + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $response = $this->process($request); + + $this->getFlashBag($request) + ?->add('success', $this->translator->trans('resetting.check_email', [ + '%tokenLifetime%' => ceil($this->retryTtl / 3600), + ], 'NucleosUserBundle')) + ; + + if (null !== $response) { + return $response; + } + + return new RedirectResponse($this->router->generate('nucleos_user_resetting_request')); + } return new Response($this->twig->render('@NucleosUser/Resetting/request.html.twig', [ 'form' => $form->createView(), @@ -116,7 +133,7 @@ private function process(Request $request): ?Response } if (!$user instanceof UserInterface) { - return new RedirectResponse($this->router->generate('nucleos_user_resetting_check_email')); + return null; } $event = new GetResponseNullableUserEvent($user, $request); @@ -127,7 +144,7 @@ private function process(Request $request): ?Response } if ($user->isPasswordRequestNonExpired($this->retryTtl)) { - return new RedirectResponse($this->router->generate('nucleos_user_resetting_check_email')); + return null; } $event = new GetResponseUserEvent($user, $request); @@ -159,6 +176,17 @@ private function process(Request $request): ?Response return $event->getResponse(); } - return new RedirectResponse($this->router->generate('nucleos_user_resetting_check_email')); + return null; + } + + private function getFlashBag(Request $request): ?FlashBagInterface + { + $session = $request->hasSession() ? $request->getSession() : null; + + if (!$session instanceof Session) { + return null; + } + + return $session->getFlashBag(); } } diff --git a/src/Resources/config/resetting.php b/src/Resources/config/resetting.php index 51e12be0..a773274a 100644 --- a/src/Resources/config/resetting.php +++ b/src/Resources/config/resetting.php @@ -52,6 +52,7 @@ new Reference('security.user_providers'), new Reference('nucleos_user.mailer'), new Parameter('nucleos_user.resetting.retry_ttl'), + new Reference('translator'), ]) ->set(ResetAction::class)