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

Skip to content

[Form] Check for RepeatedType child in PasswordHasherListener #49267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,18 @@ public function hashPasswords(FormEvent $event)

private function getTargetForm(FormInterface $form): FormInterface
{
$parent = $form->getParent();

if ($parent && $parent->getConfig()->getType()->getInnerType() instanceof RepeatedType) {
return $parent;
if (!$parentForm = $form->getParent()) {
return $form;
}

$parentType = $parentForm->getConfig()->getType();

do {
if ($parentType->getInnerType() instanceof RepeatedType) {
return $parentForm;
}
} while ($parentType = $parentType->getParent());

return $form;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Form\Exception\InvalidConfigurationException;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\PasswordHasher\EventListener\PasswordHasherListener;
use Symfony\Component\Form\Extension\PasswordHasher\PasswordHasherExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Form\Tests\Fixtures\RepeatedPasswordField;
use Symfony\Component\Form\Tests\Fixtures\User;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
Expand Down Expand Up @@ -113,6 +116,53 @@ public function testPasswordHashSuccessWithEmptyData()
$this->assertSame($user->getPassword(), $hashedPassword);
}

/**
* @dataProvider provideRepeatedPasswordField
*/
public function testRepeatedPasswordField(string $type, array $options = [])
{
$user = new User();

$plainPassword = 'PlainPassword';
$hashedPassword = 'HashedPassword';

$this->passwordHasher
->expects($this->once())
->method('hashPassword')
->with($user, $plainPassword)
->willReturn($hashedPassword)
;

$this->assertNull($user->getPassword());

$form = $this->factory
->createBuilder(data: $user)
->add('plainPassword', $type, $options)
->getForm()
;

$form->submit(['plainPassword' => ['first' => $plainPassword, 'second' => $plainPassword]]);

$this->assertTrue($form->isValid());
$this->assertSame($user->getPassword(), $hashedPassword);
}

public static function provideRepeatedPasswordField(): iterable
{
yield 'RepeatedType' => [
RepeatedType::class,
[
'type' => PasswordType::class,
'first_options' => [
'hash_property_path' => 'password',
],
'mapped' => false,
],
];

yield 'RepeatedType child' => [RepeatedPasswordField::class];
}

public function testPasswordHashOnInvalidForm()
{
$user = new User();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RepeatedPasswordField extends AbstractType
{
public function getParent(): ?string
{
return RepeatedType::class;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'mapped' => false,
'type' => PasswordType::class,
'first_options' => [
'hash_property_path' => 'password',
],
]);
}
}