diff --git a/src/Maker/MakeRegistrationForm.php b/src/Maker/MakeRegistrationForm.php index 48a8ae34a..e2cb01daf 100644 --- a/src/Maker/MakeRegistrationForm.php +++ b/src/Maker/MakeRegistrationForm.php @@ -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; @@ -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 @@ -307,7 +307,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen Request::class, Response::class, Route::class, - UserPasswordHasherInterface::class, EntityManagerInterface::class, ]); @@ -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, ], @@ -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', @@ -568,9 +566,8 @@ private function generateFormClass(ClassNameDetails $userClassDetails, Generator 'plainPassword' => [ 'type' => PasswordType::class, 'options_code' => << false, + 'hash_property_path' => '$passwordField', 'attr' => ['autocomplete' => 'new-password'], 'constraints' => [ new NotBlank([ diff --git a/templates/registration/RegistrationController.tpl.php b/templates/registration/RegistrationController.tpl.php index ad3707910..647689acf 100644 --- a/templates/registration/RegistrationController.tpl.php +++ b/templates/registration/RegistrationController.tpl.php @@ -13,19 +13,13 @@ public function __construct(private getPropertyType($email_verif generateRouteForControllerMethod($route_path, $route_name) ?> - public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response + public function register(Request $request, EntityManagerInterface $entityManager): Response { $user = new (); $form = $this->createForm(::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - /** @var string $plainPassword */ - $plainPassword = $form->get('plainPassword')->getData(); - - // encode the plain password - $user->set($userPasswordHasher->hashPassword($user, $plainPassword)); - $entityManager->persist($user); $entityManager->flush(); diff --git a/tests/fixtures/make-registration-form/expected/RegistrationControllerCustomAuthenticator.php b/tests/fixtures/make-registration-form/expected/RegistrationControllerCustomAuthenticator.php index 97e5428e7..05eb960a7 100644 --- a/tests/fixtures/make-registration-form/expected/RegistrationControllerCustomAuthenticator.php +++ b/tests/fixtures/make-registration-form/expected/RegistrationControllerCustomAuthenticator.php @@ -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(); diff --git a/tests/fixtures/make-registration-form/expected/RegistrationControllerFormLogin.php b/tests/fixtures/make-registration-form/expected/RegistrationControllerFormLogin.php index 1fd6787d8..20c4656d0 100644 --- a/tests/fixtures/make-registration-form/expected/RegistrationControllerFormLogin.php +++ b/tests/fixtures/make-registration-form/expected/RegistrationControllerFormLogin.php @@ -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(); diff --git a/tests/fixtures/make-registration-form/expected/RegistrationControllerNoLogin.php b/tests/fixtures/make-registration-form/expected/RegistrationControllerNoLogin.php index 8863f9342..ec77bd79e 100644 --- a/tests/fixtures/make-registration-form/expected/RegistrationControllerNoLogin.php +++ b/tests/fixtures/make-registration-form/expected/RegistrationControllerNoLogin.php @@ -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();