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 02b55b7 commit 415fc9a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/Action/CheckEmailAction.php
Expand Up @@ -19,6 +19,9 @@
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;

/**
* @deprecated
*/
final class CheckEmailAction
{
private readonly Environment $twig;
Expand Down
72 changes: 55 additions & 17 deletions src/Action/RequestResetAction.php
Expand Up @@ -22,13 +22,17 @@
use Nucleos\UserBundle\Util\TokenGenerator;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
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 @@ -51,6 +55,11 @@ final class RequestResetAction

private readonly UserProviderInterface $userProvider;

private readonly TranslatorInterface $translator;

/**
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Environment $twig,
FormFactoryInterface $formFactory,
Expand All @@ -60,7 +69,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,25 +81,29 @@ 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);
$form = $this->createForm();
$form->handleRequest($request);

Check warning on line 90 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->createForm(); - $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;
}
if ($form->isSubmitted() && $form->isValid()) {

Check warning on line 92 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->createForm(); $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);

$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',
])
;
$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 +130,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 +141,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 +173,30 @@ 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();
}

private function createForm(): FormInterface
{
return $this->formFactory
->create(RequestPasswordFormType::class, null, [

Check warning on line 193 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 @@ @@ } private function createForm() : FormInterface { - return $this->formFactory->create(RequestPasswordFormType::class, null, ['action' => $this->router->generate('nucleos_user_resetting_request'), 'method' => 'POST'])->add('save', SubmitType::class, ['label' => 'resetting.request.submit']); + return $this->formFactory->create(RequestPasswordFormType::class, null, ['method' => 'POST'])->add('save', SubmitType::class, ['label' => 'resetting.request.submit']); } }
'action' => $this->router->generate('nucleos_user_resetting_request'),
'method' => 'POST',
])
->add('save', SubmitType::class, [

Check warning on line 197 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 @@ @@ } private function createForm() : FormInterface { - return $this->formFactory->create(RequestPasswordFormType::class, null, ['action' => $this->router->generate('nucleos_user_resetting_request'), 'method' => 'POST'])->add('save', SubmitType::class, ['label' => 'resetting.request.submit']); + return $this->formFactory->create(RequestPasswordFormType::class, null, ['action' => $this->router->generate('nucleos_user_resetting_request'), 'method' => 'POST'])->add('save', SubmitType::class, []); } }
'label' => 'resetting.request.submit',
])
;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/resetting.php
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 415fc9a

Please sign in to comment.