Skip to content

Commit f9ba24e

Browse files
committed
[make:registration-form] Add hash_property_path option to PasswordType
1 parent 0624f13 commit f9ba24e

File tree

5 files changed

+8
-38
lines changed

5 files changed

+8
-38
lines changed

src/Maker/MakeRegistrationForm.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
use Symfony\Component\HttpFoundation\Response;
5353
use Symfony\Component\Mailer\MailerInterface;
5454
use Symfony\Component\Mime\Address;
55-
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
5655
use Symfony\Component\Routing\Attribute\Route;
5756
use Symfony\Component\Routing\RouterInterface;
5857
use Symfony\Component\Security\Core\User\UserInterface;
@@ -291,7 +290,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
291290
$formClassDetails = $this->generateFormClass(
292291
$userClassNameDetails,
293292
$generator,
294-
$usernameField
293+
$usernameField,
294+
$this->passwordField
295295
);
296296

297297
// 2) Generate the controller
@@ -307,7 +307,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
307307
Request::class,
308308
Response::class,
309309
Route::class,
310-
UserPasswordHasherInterface::class,
311310
EntityManagerInterface::class,
312311
]);
313312

@@ -355,7 +354,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
355354
'route_name' => 'app_register',
356355
'form_class_name' => $formClassDetails->getShortName(),
357356
'user_class_name' => $userClassNameDetails->getShortName(),
358-
'password_field' => $this->passwordField,
359357
'redirect_route_name' => $this->redirectRouteName ?? null,
360358
'translator_available' => $isTranslatorAvailable,
361359
],
@@ -545,7 +543,7 @@ public function configureDependencies(DependencyBuilder $dependencies): void
545543
);
546544
}
547545

548-
private function generateFormClass(ClassNameDetails $userClassDetails, Generator $generator, string $usernameField): ClassNameDetails
546+
private function generateFormClass(ClassNameDetails $userClassDetails, Generator $generator, string $usernameField, string $passwordField): ClassNameDetails
549547
{
550548
$formClassDetails = $generator->createClassNameDetails(
551549
'RegistrationFormType',
@@ -568,9 +566,8 @@ private function generateFormClass(ClassNameDetails $userClassDetails, Generator
568566
'plainPassword' => [
569567
'type' => PasswordType::class,
570568
'options_code' => <<<EOF
571-
// instead of being set onto the object directly,
572-
// this is read and encoded in the controller
573569
'mapped' => false,
570+
'hash_property_path' => '$passwordField',
574571
'attr' => ['autocomplete' => 'new-password'],
575572
'constraints' => [
576573
new NotBlank([

templates/registration/RegistrationController.tpl.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,13 @@ public function __construct(private <?= $generator->getPropertyType($email_verif
1313

1414
<?php endif; ?>
1515
<?= $generator->generateRouteForControllerMethod($route_path, $route_name) ?>
16-
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher<?= $login_after_registration ? ', Security $security': '' ?>, EntityManagerInterface $entityManager): Response
16+
public function register(Request $request<?= $login_after_registration ? ', Security $security': '' ?>, EntityManagerInterface $entityManager): Response
1717
{
1818
$user = new <?= $user_class_name ?>();
1919
$form = $this->createForm(<?= $form_class_name ?>::class, $user);
2020
$form->handleRequest($request);
2121

2222
if ($form->isSubmitted() && $form->isValid()) {
23-
/** @var string $plainPassword */
24-
$plainPassword = $form->get('plainPassword')->getData();
25-
26-
// encode the plain password
27-
$user->set<?= ucfirst($password_field) ?>($userPasswordHasher->hashPassword($user, $plainPassword));
28-
2923
$entityManager->persist($user);
3024
$entityManager->flush();
3125
<?php if ($will_verify_email): ?>

tests/fixtures/make-registration-form/expected/RegistrationControllerCustomAuthenticator.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,18 @@
1010
use Symfony\Bundle\SecurityBundle\Security;
1111
use Symfony\Component\HttpFoundation\Request;
1212
use Symfony\Component\HttpFoundation\Response;
13-
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
1413
use Symfony\Component\Routing\Attribute\Route;
1514

1615
class RegistrationController extends AbstractController
1716
{
1817
#[Route('/register', name: 'app_register')]
19-
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, Security $security, EntityManagerInterface $entityManager): Response
18+
public function register(Request $request, Security $security, EntityManagerInterface $entityManager): Response
2019
{
2120
$user = new User();
2221
$form = $this->createForm(RegistrationFormType::class, $user);
2322
$form->handleRequest($request);
2423

2524
if ($form->isSubmitted() && $form->isValid()) {
26-
/** @var string $plainPassword */
27-
$plainPassword = $form->get('plainPassword')->getData();
28-
29-
// encode the plain password
30-
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));
31-
3225
$entityManager->persist($user);
3326
$entityManager->flush();
3427

tests/fixtures/make-registration-form/expected/RegistrationControllerFormLogin.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,18 @@
99
use Symfony\Bundle\SecurityBundle\Security;
1010
use Symfony\Component\HttpFoundation\Request;
1111
use Symfony\Component\HttpFoundation\Response;
12-
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
1312
use Symfony\Component\Routing\Attribute\Route;
1413

1514
class RegistrationController extends AbstractController
1615
{
1716
#[Route('/register', name: 'app_register')]
18-
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, Security $security, EntityManagerInterface $entityManager): Response
17+
public function register(Request $request, Security $security, EntityManagerInterface $entityManager): Response
1918
{
2019
$user = new User();
2120
$form = $this->createForm(RegistrationFormType::class, $user);
2221
$form->handleRequest($request);
2322

2423
if ($form->isSubmitted() && $form->isValid()) {
25-
/** @var string $plainPassword */
26-
$plainPassword = $form->get('plainPassword')->getData();
27-
28-
// encode the plain password
29-
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));
30-
3124
$entityManager->persist($user);
3225
$entityManager->flush();
3326

tests/fixtures/make-registration-form/expected/RegistrationControllerNoLogin.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,18 @@
88
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
99
use Symfony\Component\HttpFoundation\Request;
1010
use Symfony\Component\HttpFoundation\Response;
11-
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
1211
use Symfony\Component\Routing\Attribute\Route;
1312

1413
class RegistrationController extends AbstractController
1514
{
1615
#[Route('/register', name: 'app_register')]
17-
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
16+
public function register(Request $request, EntityManagerInterface $entityManager): Response
1817
{
1918
$user = new User();
2019
$form = $this->createForm(RegistrationFormType::class, $user);
2120
$form->handleRequest($request);
2221

2322
if ($form->isSubmitted() && $form->isValid()) {
24-
/** @var string $plainPassword */
25-
$plainPassword = $form->get('plainPassword')->getData();
26-
27-
// encode the plain password
28-
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));
29-
3023
$entityManager->persist($user);
3124
$entityManager->flush();
3225

0 commit comments

Comments
 (0)