From 855bef00a013eb9cb320539f890e8cdb86e5d92b Mon Sep 17 00:00:00 2001 From: David Beniamine <david.beniamine@tetras-libre.fr> Date: Tue, 8 Feb 2022 11:54:10 +0100 Subject: [PATCH] Mapp plainPassword and acceptGeneralConditions All RegistrationForm fields are mapped to the userEntity --- src/Builder/UserBuilder.php | 9 +++++---- src/Controller/RegistrationController.php | 9 --------- src/Entity/User.php | 7 ++++++- src/Form/RegistrationFormType.php | 2 -- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Builder/UserBuilder.php b/src/Builder/UserBuilder.php index e457f77..1b3cea4 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 2c3fcd2..13d97d2 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 1157a35..fcde46a 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 403bfa8..85e7f5d 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']), ], -- GitLab