Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add form type for reset password request #1489

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions src/Action/RequestAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@

use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Sonata\UserBundle\Form\Type\ResetPasswordRequestFormType;
use Sonata\UserBundle\Mailer\MailerInterface;
use Sonata\UserBundle\Model\UserManagerInterface;
use Sonata\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
Expand All @@ -33,29 +39,72 @@ final class RequestAction

private TemplateRegistryInterface $templateRegistry;

private FormFactoryInterface $formFactory;

private UserManagerInterface $userManager;

private MailerInterface $mailer;

private TokenGeneratorInterface $tokenGenerator;

private int $retryTtl;

public function __construct(
Environment $twig,
UrlGeneratorInterface $urlGenerator,
AuthorizationCheckerInterface $authorizationChecker,
Pool $adminPool,
TemplateRegistryInterface $templateRegistry
TemplateRegistryInterface $templateRegistry,
FormFactoryInterface $formFactory,
UserManagerInterface $userManager,
MailerInterface $mailer,
TokenGeneratorInterface $tokenGenerator,
int $retryTtl
) {
$this->twig = $twig;
$this->urlGenerator = $urlGenerator;
$this->authorizationChecker = $authorizationChecker;
$this->adminPool = $adminPool;
$this->templateRegistry = $templateRegistry;
$this->formFactory = $formFactory;
$this->userManager = $userManager;
$this->mailer = $mailer;
$this->tokenGenerator = $tokenGenerator;
$this->retryTtl = $retryTtl;
}

public function __invoke(): Response
public function __invoke(Request $request): Response
{
if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
return new RedirectResponse($this->urlGenerator->generate('sonata_admin_dashboard'));
}

$form = $this->formFactory->create(ResetPasswordRequestFormType::class);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$username = $form->get('username')->getData();
$user = $this->userManager->findUserByUsernameOrEmail($username);

if (null !== $user && $user->isEnabled() && !$user->isPasswordRequestNonExpired($this->retryTtl) && $user->isAccountNonLocked()) {
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}

$this->mailer->sendResettingEmailMessage($user);
$user->setPasswordRequestedAt(new \DateTime());
$this->userManager->save($user);
}

return new RedirectResponse($this->urlGenerator->generate('sonata_user_admin_resetting_check_email', [
'username' => $username,
]));
}

return new Response($this->twig->render('@SonataUser/Admin/Security/Resetting/request.html.twig', [
'base_template' => $this->templateRegistry->getTemplate('layout'),
'admin_pool' => $this->adminPool,
'form' => $form->createView(),
]));
}
}
1 change: 0 additions & 1 deletion src/Action/ResetAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public function __invoke(Request $request, string $token): Response

$form = $this->formFactory->create(ResettingFormType::class);
$form->setData($user);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
Expand Down
72 changes: 0 additions & 72 deletions src/Action/SendEmailAction.php

This file was deleted.

46 changes: 46 additions & 0 deletions src/Form/Type/ResetPasswordRequestFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;

/**
* @author Jordi Sala <jordism91@gmail.com>
*/
final class ResetPasswordRequestFormType extends AbstractType
{
/**
* @param array<string, mixed> $options
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('username', TextType::class, [
'constraints' => [
new NotBlank(),
],
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'translation_domain' => 'SonataUserBundle',
]);
}
}
8 changes: 1 addition & 7 deletions src/Resources/config/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Sonata\UserBundle\Action\LogoutAction;
use Sonata\UserBundle\Action\RequestAction;
use Sonata\UserBundle\Action\ResetAction;
use Sonata\UserBundle\Action\SendEmailAction;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;

Expand All @@ -34,12 +33,7 @@
new ReferenceConfigurator('security.authorization_checker'),
new ReferenceConfigurator('sonata.admin.pool'),
new ReferenceConfigurator('sonata.admin.global_template_registry'),
])

->set('sonata.user.action.send_email', SendEmailAction::class)
->public()
->args([
new ReferenceConfigurator('router'),
new ReferenceConfigurator('form.factory'),
new ReferenceConfigurator('sonata.user.manager.user'),
new ReferenceConfigurator('sonata.user.mailer'),
new ReferenceConfigurator('sonata.user.util.token_generator'),
Expand Down
6 changes: 5 additions & 1 deletion src/Resources/config/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* file that was distributed with this source code.
*/

use Sonata\UserBundle\Form\Type\ResetPasswordRequestFormType;
use Sonata\UserBundle\Form\Type\ResettingFormType;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

Expand All @@ -23,5 +24,8 @@
->tag('form.type', ['alias' => 'sonata_user_resetting'])
->args([
'%sonata.user.user.class%',
]);
])

->set('sonata.user.form.type.reset_password_request', ResetPasswordRequestFormType::class)
->tag('form.type', ['alias' => 'sonata_user_reset_password_request']);
};
3 changes: 1 addition & 2 deletions src/Resources/config/routing/admin_resetting.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<routes xmlns="http://symfony.com/schema/routing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="sonata_user_admin_resetting_request" path="/request" methods="GET" controller="sonata.user.action.request"/>
<route id="sonata_user_admin_resetting_send_email" path="/send-email" methods="POST" controller="sonata.user.action.send_email"/>
<route id="sonata_user_admin_resetting_request" path="/request" methods="GET POST" controller="sonata.user.action.request"/>
<route id="sonata_user_admin_resetting_check_email" path="/check-email" methods="GET" controller="sonata.user.action.check_email"/>
<route id="sonata_user_admin_resetting_reset" path="/reset/{token}" methods="GET POST" controller="sonata.user.action.reset"/>
</routes>
13 changes: 8 additions & 5 deletions src/Resources/views/Admin/Security/Resetting/request.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,24 @@ file that was distributed with this source code.
<div class="login-box-body">
{% block sonata_user_reset_request_form %}
<p class="login-box-msg">{{ 'resetting.request.submit'|trans({}, 'SonataUserBundle') }}</p>
<form action="{{ path('sonata_user_admin_resetting_send_email') }}" method="post" role="form">

{{ form_start(form) }}
<div class="form-group has-feedback">
<input type="text" class="form-control" id="username" name="username" required="required"
placeholder="{{ 'resetting.request.username'|trans({}, 'SonataUserBundle')|replace({':': ''}) }}"/>
{{ form_widget(form.username, {'attr': {
'class': 'form-control',
'placeholder': 'resetting.request.username'|trans({}, 'SonataUserBundle')|replace({':': ''})
}}) }}
<span class="fas fa-user form-control-feedback"></span>
{{ form_errors(form.username) }}
</div>

<div class="row">
<div class="col-xs-12">
<button type="submit" name="submit" class="btn btn-primary btn-block btn-flat">
{{ 'resetting.request.submit'|trans({}, 'SonataUserBundle') }}
</button>
</div>
</div>
</form>
{{ form_end(form) }}

<a href="{{ path('sonata_user_admin_security_login') }}">
{{ 'title_user_authentication'|trans({}, 'SonataUserBundle') }}
Expand Down
Loading