diff --git a/src/Builder/UserBuilder.php b/src/Builder/UserBuilder.php index e457f777d8ee4d6ad98d50b84564f60eb56c7aae..1b3cea4c6b3f8a2f513112f06eb762acf4f0e750 100644 --- a/src/Builder/UserBuilder.php +++ b/src/Builder/UserBuilder.php @@ -48,8 +48,7 @@ class UserBuilder !StringHelper::isNullOrWhitespace($plainPassword), 'A user should have none empty password' ); - $this->user->setSalt($salt); - $this->user->setPassword($this->password_hasher->hashPassword($this->user, $plainPassword)); + $this->user->plainPassword = $plainPassword; return $this; } @@ -87,14 +86,16 @@ class UserBuilder "A user must have a first name (current:'" . $this->user->getFirstName() . "')" ); ContractHelper::requires( - !StringHelper::isNullOrWhitespace($this->user->getPassword()), + !StringHelper::isNullOrWhitespace($this->user->plainPassword), "A user must have a have a none empty or whitespace password" ); ContractHelper::requires( !empty($this->user->getRoles()), "A user must have a have roles" ); - + $this->user->setSalt(random_bytes(100)); + $this->user->setPassword($this->password_hasher->hashPassword($this->user, $this->user->plainPassword)); + $this->user->eraseCredentials(); return $this->user; } diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index 2c3fcd2ed1075ad970c0e28ed11bb04e70f06e28..13d97d2a0e9985ede679190c39559c2538fadda9 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -56,15 +56,6 @@ class RegistrationController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $userBuilder = new UserBuilder($userPasswordHasher, $user); - // Ugly fix because I don't understand why those values aren't set correctly - $userBuilder->withAcceptGeneralConditions($form->get('acceptGeneralConditions')->getData()); - - $userBuilder - ->withPassword( - random_bytes(100), - $form->get('plainPassword')->getData() - ); - $this->entity_manager->persist($userBuilder->createUser()); $this->entity_manager->flush(); diff --git a/src/Entity/User.php b/src/Entity/User.php index 1157a35cc77f036cea638825a64a330d620907b9..fcde46a8ed06432d816aa83069a1036af58d30b0 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -99,6 +99,11 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface */ private Collection $capsules; + /** + * @var string $plainPassword plain password to store before hashing it + */ + public string $plainPassword; + public function __construct() { $this->capsules = new ArrayCollection(); @@ -199,7 +204,7 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface public function eraseCredentials(): void { // If you store any temporary, sensitive data on the user, clear it here - // $this->plainPassword = null; + $this->plainPassword = ""; } public function isVerified(): bool diff --git a/src/Form/RegistrationFormType.php b/src/Form/RegistrationFormType.php index 403bfa89de8e5cffb84b3bde9fc7836c8cf00cf2..85e7f5d9a12c132770ed5fd23acafd812d9dcd91 100644 --- a/src/Form/RegistrationFormType.php +++ b/src/Form/RegistrationFormType.php @@ -51,7 +51,6 @@ class RegistrationFormType extends AbstractType ) ->add('plainPassword', RepeatedType::class, [ 'type' => PasswordType::class, - 'mapped' => false, 'required' => true, 'constraints' => [ new NotBlank(['message' => 'password.not_blank']), @@ -80,7 +79,6 @@ class RegistrationFormType extends AbstractType 'acceptGeneralConditions', CheckboxType::class, [ - 'mapped' => false, 'constraints' => [ new IsTrue(['message' => 'agreeTerms']), ],