Skip to content

Commit

Permalink
Form improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
codedmonkey committed Jul 16, 2024
1 parent eb8e89c commit cdfb384
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 111 deletions.
6 changes: 3 additions & 3 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ security:
form_login:
login_path: /?routeName=dashboard_login
check_path: dashboard_login
remember_me:
secret: '%kernel.secret%'
lifetime: 604800 # 1 week in seconds
logout:
target: dashboard
switch_user: true

# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#the-firewall

# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true

# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
Expand Down
8 changes: 4 additions & 4 deletions src/Controller/Dashboard/DashboardAccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use CodedMonkey\Conductor\Doctrine\Entity\User;
use CodedMonkey\Conductor\Doctrine\Repository\UserRepository;
use CodedMonkey\Conductor\Form\AccountType;
use CodedMonkey\Conductor\Form\ChangePasswordType;
use CodedMonkey\Conductor\Form\AccountFormType;
use CodedMonkey\Conductor\Form\ChangePasswordFormType;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
Expand Down Expand Up @@ -35,8 +35,8 @@ public function __construct(
#[IsGranted('ROLE_USER')]
public function account(Request $request, #[CurrentUser] User $user): Response
{
$accountForm = $this->createForm(AccountType::class, $user);
$passwordForm = $this->createForm(ChangePasswordType::class);
$accountForm = $this->createForm(AccountFormType::class, $user);
$passwordForm = $this->createForm(ChangePasswordFormType::class);

$accountForm->handleRequest($request);

Expand Down
8 changes: 4 additions & 4 deletions src/Controller/Dashboard/DashboardResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace CodedMonkey\Conductor\Controller\Dashboard;

use CodedMonkey\Conductor\Doctrine\Entity\User;
use CodedMonkey\Conductor\Form\ResetPasswordRequestType;
use CodedMonkey\Conductor\Form\ResetPasswordType;
use CodedMonkey\Conductor\Form\ResetPasswordFormType;
use CodedMonkey\Conductor\Form\ResetPasswordRequestFormType;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
Expand Down Expand Up @@ -34,7 +34,7 @@ public function __construct(
#[Route('/reset-password', name: 'dashboard_reset_password_request')]
public function request(Request $request): Response
{
$form = $this->createForm(ResetPasswordRequestType::class);
$form = $this->createForm(ResetPasswordRequestFormType::class);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
Expand Down Expand Up @@ -85,7 +85,7 @@ public function passwordReset(Request $request, string $token = null): Response
return $this->redirect($this->adminUrlGenerator->setRoute('dashboard_reset_password_request')->generateUrl());
}

$form = $this->createForm(ResetPasswordType::class);
$form = $this->createForm(ResetPasswordFormType::class);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
Expand Down
1 change: 1 addition & 0 deletions src/Controller/Dashboard/DashboardSecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function login(AuthenticationUtils $authenticationUtils, AdminUrlGenerato
'last_username' => $authenticationUtils->getLastUsername(),
'forgot_password_enabled' => true,
'forgot_password_path' => $adminUrlGenerator->setRoute('dashboard_reset_password_request')->generateUrl(),
'remember_me_enabled' => true,
]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Controller/Dashboard/DashboardUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CodedMonkey\Conductor\Controller\Dashboard;

use CodedMonkey\Conductor\Doctrine\Entity\User;
use CodedMonkey\Conductor\Form\NewPasswordType;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
Expand Down Expand Up @@ -49,6 +50,7 @@ public function configureFields(string $pageName): iterable
yield TextField::new('plainPassword')
->setLabel('Password')
->setFormType(PasswordType::class)
->setFormTypeOption('constraints', NewPasswordType::constraints())
->onlyOnForms();
yield ChoiceField::new('roles')
->setChoices([
Expand Down
2 changes: 1 addition & 1 deletion src/Form/AccountType.php → src/Form/AccountFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AccountType extends AbstractType
class AccountFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
Expand Down
21 changes: 21 additions & 0 deletions src/Form/ChangePasswordFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace CodedMonkey\Conductor\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;

class ChangePasswordFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('currentPassword', PasswordType::class, [
'required' => true,
])
->add('newPassword', NewPasswordType::class, [
'new_password' => true,
]);
}
}
29 changes: 0 additions & 29 deletions src/Form/ChangePasswordType.php

This file was deleted.

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

namespace CodedMonkey\Conductor\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotCompromisedPassword;
use Symfony\Component\Validator\Constraints\PasswordStrength;

class NewPasswordType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'new_password' => false,
'type' => PasswordType::class,
'options' => [
'attr' => ['autocomplete' => 'new-password'],
],
'first_options' => function (Options $options): array {
$label = $options['new_password'] ? 'New password' : 'Password';

return [
'constraints' => self::constraints(false),
'label' => $label,
];
},
'second_options' => function (Options $options): array {
$label = $options['new_password'] ? 'Repeat new password' : 'Repeat password';

return [
'label' => $label,
];
},
'invalid_message' => 'The password fields must match',
]);
}

public function getParent(): string
{
return RepeatedType::class;
}

public static function constraints(bool $nullable = true): array
{
$constraints = [
new Length([
'min' => 8,
'minMessage' => 'Your password should be at least {{ limit }} characters',
'max' => 4096, // max length allowed by Symfony for security reasons
]),
new PasswordStrength(minScore: PasswordStrength::STRENGTH_WEAK),
new NotCompromisedPassword(),
];

if (!$nullable) {
$constraints[] = new NotBlank([
'message' => 'Please enter a password',
]);
}

return $constraints;
}
}
7 changes: 1 addition & 6 deletions src/Form/RegistrationFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use CodedMonkey\Conductor\Doctrine\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -18,14 +17,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('username', TextType::class, [
'required' => true,
])
->add('name', TextType::class, [
'required' => false,
])
->add('email', EmailType::class, [
'required' => false,
])
->add('plainPassword', PasswordType::class, [
'label' => 'Password',
->add('plainPassword', NewPasswordType::class, [
'required' => true,
]);
}
Expand Down
23 changes: 23 additions & 0 deletions src/Form/ResetPasswordFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace CodedMonkey\Conductor\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ResetPasswordFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('plainPassword', NewPasswordType::class, [
'new_password' => true,
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;

class ResetPasswordRequestType extends AbstractType
class ResetPasswordRequestFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('email', TextType::class, [
'data' => $options['last_username'],
'attr' => ['autocomplete' => 'email'],
'constraints' => [
new NotBlank([
Expand All @@ -26,8 +25,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'last_username' => null,
]);
$resolver->setDefaults([]);
}
}
58 changes: 0 additions & 58 deletions src/Form/ResetPasswordType.php

This file was deleted.

1 change: 0 additions & 1 deletion templates/dashboard/security/register.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

{{ form_start(form) }}
{{ form_row(form.username) }}
{{ form_row(form.name) }}
{{ form_row(form.email) }}
{{ form_row(form.plainPassword) }}

Expand Down
5 changes: 5 additions & 0 deletions translations/validators.en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enter an email address: Enter an email address

Please enter a password: Please enter a password
The password fields must match: The password fields must match
Your password should be at least {{ limit }} characters: Your password should be at least {{ limit }} characters

0 comments on commit cdfb384

Please sign in to comment.