Skip to content

Commit

Permalink
Deprecate CheckEmailAction in favor of flash messages
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Sep 9, 2023
1 parent 81a4159 commit 5b168a7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/Action/CheckEmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;

/**
* @deprecated
*/
final class CheckEmailAction
{
private readonly Environment $twig;
Expand Down
48 changes: 38 additions & 10 deletions src/Action/RequestResetAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,6 +53,7 @@ final class RequestResetAction
private readonly int $retryTtl;

private readonly UserProviderInterface $userProvider;
private TranslatorInterface $translator;

public function __construct(
Environment $twig,
Expand All @@ -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;
Expand All @@ -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, [

Check warning on line 85 in src/Action/RequestResetAction.php

View workflow job for this annotation

GitHub Actions / run / Mutation Tests (8.2)

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ } public function __invoke(Request $request) : Response { - $form = $this->formFactory->create(RequestPasswordFormType::class, null, ['action' => $this->router->generate('nucleos_user_resetting_request'), 'method' => 'POST'])->add('save', SubmitType::class, ['label' => 'resetting.request.submit']); + $form = $this->formFactory->create(RequestPasswordFormType::class, null, ['method' => 'POST'])->add('save', SubmitType::class, ['label' => 'resetting.request.submit']); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $response = $this->process($request);
'action' => $this->router->generate('nucleos_user_resetting_request'),
Expand All @@ -90,6 +90,23 @@ public function __invoke(Request $request): Response
'label' => 'resetting.request.submit',
])
;
$form->handleRequest($request);

Check warning on line 93 in src/Action/RequestResetAction.php

View workflow job for this annotation

GitHub Actions / run / Mutation Tests (8.2)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function __invoke(Request $request) : Response { $form = $this->formFactory->create(RequestPasswordFormType::class, null, ['action' => $this->router->generate('nucleos_user_resetting_request'), 'method' => 'POST'])->add('save', SubmitType::class, ['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 ($form->isSubmitted() && $form->isValid()) {

Check warning on line 95 in src/Action/RequestResetAction.php

View workflow job for this annotation

GitHub Actions / run / Mutation Tests (8.2)

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ { $form = $this->formFactory->create(RequestPasswordFormType::class, null, ['action' => $this->router->generate('nucleos_user_resetting_request'), 'method' => 'POST'])->add('save', SubmitType::class, ['label' => 'resetting.request.submit']); $form->handleRequest($request); - if ($form->isSubmitted() && $form->isValid()) { + 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) {
$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(),
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
}
1 change: 1 addition & 0 deletions src/Resources/config/resetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5b168a7

Please sign in to comment.