Skip to content

Commit 7349f26

Browse files
committed
feature #1380 [Sf62] Use the new 'hash_property_path' option of the PasswordType field (rosier)
This PR was merged into the main branch. Discussion ---------- [Sf62] Use the new 'hash_property_path' option of the PasswordType field https://symfony.com/blog/new-in-symfony-6-2-security-improvements-part-2#improved-password-form-field Fixes #1378 Commits ------- dbace66 Use the new 'hash_property_path' option of the PasswordType field
2 parents 8b036a5 + dbace66 commit 7349f26

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/Controller/UserController.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
namespace App\Controller;
1313

1414
use App\Entity\User;
15-
use App\Form\Type\ChangePasswordType;
15+
use App\Form\ChangePasswordType;
1616
use App\Form\UserType;
1717
use Doctrine\ORM\EntityManagerInterface;
1818
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1919
use Symfony\Component\HttpFoundation\Request;
2020
use Symfony\Component\HttpFoundation\Response;
21-
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
2221
use Symfony\Component\Routing\Annotation\Route;
2322
use Symfony\Component\Security\Http\Attribute\CurrentUser;
2423
use Symfony\Component\Security\Http\Attribute\IsGranted;
@@ -61,17 +60,12 @@ public function edit(
6160
public function changePassword(
6261
#[CurrentUser] User $user,
6362
Request $request,
64-
UserPasswordHasherInterface $passwordHasher,
6563
EntityManagerInterface $entityManager,
6664
): Response {
67-
$form = $this->createForm(ChangePasswordType::class);
65+
$form = $this->createForm(ChangePasswordType::class, $user);
6866
$form->handleRequest($request);
6967

7068
if ($form->isSubmitted() && $form->isValid()) {
71-
/** @var string $plainPassword */
72-
$plainPassword = $form->get('newPassword')->getData();
73-
74-
$user->setPassword($passwordHasher->hashPassword($user, $plainPassword));
7569
$entityManager->flush();
7670

7771
return $this->redirectToRoute('security_logout');

src/Form/Type/ChangePasswordType.php renamed to src/Form/ChangePasswordType.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace App\Form\Type;
12+
namespace App\Form;
1313

14+
use App\Entity\User;
1415
use Symfony\Component\Form\AbstractType;
1516
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
1617
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
1718
use Symfony\Component\Form\FormBuilderInterface;
19+
use Symfony\Component\OptionsResolver\OptionsResolver;
1820
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
1921
use Symfony\Component\Validator\Constraints\Length;
2022
use Symfony\Component\Validator\Constraints\NotBlank;
2123

2224
/**
23-
* Defines the custom form field type used to change user's password.
25+
* Defines the form used to change user's password.
2426
*
2527
* @author Romain Monteil <[email protected]>
2628
*/
@@ -37,6 +39,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
3739
new UserPassword(),
3840
],
3941
'label' => 'label.current_password',
42+
'mapped' => false,
4043
'attr' => [
4144
'autocomplete' => 'off',
4245
],
@@ -51,12 +54,24 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
5154
),
5255
],
5356
'first_options' => [
57+
'hash_property_path' => 'password',
5458
'label' => 'label.new_password',
5559
],
60+
'mapped' => false,
5661
'second_options' => [
5762
'label' => 'label.new_password_confirm',
5863
],
5964
])
6065
;
6166
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function configureOptions(OptionsResolver $resolver): void
72+
{
73+
$resolver->setDefaults([
74+
'data_class' => User::class,
75+
]);
76+
}
6277
}

0 commit comments

Comments
 (0)