Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50e2de9

Browse files
bug #49267 [Form] Check for RepeatedType child in PasswordHasherListener (MatTheCat)
This PR was merged into the 6.2 branch. Discussion ---------- [Form] Check for `RepeatedType` child in `PasswordHasherListener` | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #49260 | License | MIT | Doc PR | N/A Commits ------- fb28c5b [Form] Check for `RepeatedType` child in `PasswordHasherListener`
2 parents aab5c1c + fb28c5b commit 50e2de9

File tree

3 files changed

+96
-4
lines changed

3 files changed

+96
-4
lines changed

src/Symfony/Component/Form/Extension/PasswordHasher/EventListener/PasswordHasherListener.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ public function hashPasswords(FormEvent $event)
7171

7272
private function getTargetForm(FormInterface $form): FormInterface
7373
{
74-
$parent = $form->getParent();
75-
76-
if ($parent && $parent->getConfig()->getType()->getInnerType() instanceof RepeatedType) {
77-
return $parent;
74+
if (!$parentForm = $form->getParent()) {
75+
return $form;
7876
}
7977

78+
$parentType = $parentForm->getConfig()->getType();
79+
80+
do {
81+
if ($parentType->getInnerType() instanceof RepeatedType) {
82+
return $parentForm;
83+
}
84+
} while ($parentType = $parentType->getParent());
85+
8086
return $form;
8187
}
8288

src/Symfony/Component/Form/Tests/Extension/PasswordHasher/Type/PasswordTypePasswordHasherExtensionTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use Symfony\Component\Form\Exception\InvalidConfigurationException;
16+
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
17+
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
1618
use Symfony\Component\Form\Extension\PasswordHasher\EventListener\PasswordHasherListener;
1719
use Symfony\Component\Form\Extension\PasswordHasher\PasswordHasherExtension;
1820
use Symfony\Component\Form\Test\TypeTestCase;
21+
use Symfony\Component\Form\Tests\Fixtures\RepeatedPasswordField;
1922
use Symfony\Component\Form\Tests\Fixtures\User;
2023
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
2124
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
@@ -113,6 +116,53 @@ public function testPasswordHashSuccessWithEmptyData()
113116
$this->assertSame($user->getPassword(), $hashedPassword);
114117
}
115118

119+
/**
120+
* @dataProvider provideRepeatedPasswordField
121+
*/
122+
public function testRepeatedPasswordField(string $type, array $options = [])
123+
{
124+
$user = new User();
125+
126+
$plainPassword = 'PlainPassword';
127+
$hashedPassword = 'HashedPassword';
128+
129+
$this->passwordHasher
130+
->expects($this->once())
131+
->method('hashPassword')
132+
->with($user, $plainPassword)
133+
->willReturn($hashedPassword)
134+
;
135+
136+
$this->assertNull($user->getPassword());
137+
138+
$form = $this->factory
139+
->createBuilder(data: $user)
140+
->add('plainPassword', $type, $options)
141+
->getForm()
142+
;
143+
144+
$form->submit(['plainPassword' => ['first' => $plainPassword, 'second' => $plainPassword]]);
145+
146+
$this->assertTrue($form->isValid());
147+
$this->assertSame($user->getPassword(), $hashedPassword);
148+
}
149+
150+
public static function provideRepeatedPasswordField(): iterable
151+
{
152+
yield 'RepeatedType' => [
153+
RepeatedType::class,
154+
[
155+
'type' => PasswordType::class,
156+
'first_options' => [
157+
'hash_property_path' => 'password',
158+
],
159+
'mapped' => false,
160+
],
161+
];
162+
163+
yield 'RepeatedType child' => [RepeatedPasswordField::class];
164+
}
165+
116166
public function testPasswordHashOnInvalidForm()
117167
{
118168
$user = new User();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Fixtures;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
16+
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
18+
19+
class RepeatedPasswordField extends AbstractType
20+
{
21+
public function getParent(): ?string
22+
{
23+
return RepeatedType::class;
24+
}
25+
26+
public function configureOptions(OptionsResolver $resolver): void
27+
{
28+
$resolver->setDefaults([
29+
'mapped' => false,
30+
'type' => PasswordType::class,
31+
'first_options' => [
32+
'hash_property_path' => 'password',
33+
],
34+
]);
35+
}
36+
}

0 commit comments

Comments
 (0)