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 Jan 4, 2025
1 parent 0624f13 commit f9ba24e
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 38 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
8 changes: 1 addition & 7 deletions templates/registration/RegistrationController.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +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()) {
/** @var string $plainPassword */
$plainPassword = $form->get('plainPassword')->getData();

// encode the plain password
$user->set<?= ucfirst($password_field) ?>($userPasswordHasher->hashPassword($user, $plainPassword));

$entityManager->persist($user);
$entityManager->flush();
<?php if ($will_verify_email): ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,18 @@
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;

class RegistrationController extends AbstractController
{
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, Security $security, EntityManagerInterface $entityManager): Response
public function register(Request $request, Security $security, EntityManagerInterface $entityManager): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
/** @var string $plainPassword */
$plainPassword = $form->get('plainPassword')->getData();

// encode the plain password
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));

$entityManager->persist($user);
$entityManager->flush();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,18 @@
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;

class RegistrationController extends AbstractController
{
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, Security $security, EntityManagerInterface $entityManager): Response
public function register(Request $request, Security $security, EntityManagerInterface $entityManager): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
/** @var string $plainPassword */
$plainPassword = $form->get('plainPassword')->getData();

// encode the plain password
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));

$entityManager->persist($user);
$entityManager->flush();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,18 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;

class RegistrationController extends AbstractController
{
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
public function register(Request $request, EntityManagerInterface $entityManager): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
/** @var string $plainPassword */
$plainPassword = $form->get('plainPassword')->getData();

// encode the plain password
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));

$entityManager->persist($user);
$entityManager->flush();

Expand Down

0 comments on commit f9ba24e

Please sign in to comment.