Skip to content

Commit 3977f91

Browse files
committed
[make:registration-form] Add hash_property_path option to PasswordType
1 parent b87b1b2 commit 3977f91

File tree

2 files changed

+5
-16
lines changed

2 files changed

+5
-16
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([

src/Resources/skeleton/registration/RegistrationController.tpl.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +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-
// encode the plain password
24-
$user->set<?= ucfirst($password_field) ?>(
25-
$userPasswordHasher->hashPassword(
26-
$user,
27-
$form->get('plainPassword')->getData()
28-
)
29-
);
30-
3123
$entityManager->persist($user);
3224
$entityManager->flush();
3325
<?php if ($will_verify_email): ?>

0 commit comments

Comments
 (0)