Skip to content

Commit

Permalink
[make:registration-form] Add hash_property_path option to `Password…
Browse files Browse the repository at this point in the history
…Type`
  • Loading branch information
Seb33300 committed May 7, 2024
1 parent b87b1b2 commit 3977f91
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
11 changes: 4 additions & 7 deletions src/Maker/MakeRegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\User\UserInterface;
Expand Down Expand Up @@ -291,7 +290,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$formClassDetails = $this->generateFormClass(
$userClassNameDetails,
$generator,
$usernameField
$usernameField,
$this->passwordField
);

// 2) Generate the controller
Expand All @@ -307,7 +307,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
Request::class,
Response::class,
Route::class,
UserPasswordHasherInterface::class,
EntityManagerInterface::class,
]);

Expand Down Expand Up @@ -355,7 +354,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'route_name' => 'app_register',
'form_class_name' => $formClassDetails->getShortName(),
'user_class_name' => $userClassNameDetails->getShortName(),
'password_field' => $this->passwordField,
'redirect_route_name' => $this->redirectRouteName ?? null,
'translator_available' => $isTranslatorAvailable,
],
Expand Down Expand Up @@ -545,7 +543,7 @@ public function configureDependencies(DependencyBuilder $dependencies): void
);
}

private function generateFormClass(ClassNameDetails $userClassDetails, Generator $generator, string $usernameField): ClassNameDetails
private function generateFormClass(ClassNameDetails $userClassDetails, Generator $generator, string $usernameField, string $passwordField): ClassNameDetails
{
$formClassDetails = $generator->createClassNameDetails(
'RegistrationFormType',
Expand All @@ -568,9 +566,8 @@ private function generateFormClass(ClassNameDetails $userClassDetails, Generator
'plainPassword' => [
'type' => PasswordType::class,
'options_code' => <<<EOF
// instead of being set onto the object directly,
// this is read and encoded in the controller
'mapped' => false,
'hash_property_path' => '$passwordField',
'attr' => ['autocomplete' => 'new-password'],
'constraints' => [
new NotBlank([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@ public function __construct(private <?= $generator->getPropertyType($email_verif

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

if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->set<?= ucfirst($password_field) ?>(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);

$entityManager->persist($user);
$entityManager->flush();
<?php if ($will_verify_email): ?>
Expand Down

0 comments on commit 3977f91

Please sign in to comment.