Skip to content

[Form] Fix PasswordHasherListener to work with empty data #49208

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 4, 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 @@ -14,13 +14,15 @@
use Symfony\Component\Form\Exception\InvalidConfigurationException;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;

/**
* @author Sébastien Alfaiate <s.alfaiate@webarea.fr>
* @author Gábor Egyed <gabor.egyed@gmail.com>
*/
class PasswordHasherListener
{
Expand All @@ -35,26 +37,11 @@ public function __construct(

public function registerPassword(FormEvent $event)
{
$form = $event->getForm();
$parentForm = $form->getParent();
$mapped = $form->getConfig()->getMapped();

if ($parentForm && $parentForm->getConfig()->getType()->getInnerType() instanceof RepeatedType) {
$mapped = $parentForm->getConfig()->getMapped();
$parentForm = $parentForm->getParent();
}

if ($mapped) {
throw new InvalidConfigurationException('The "hash_property_path" option cannot be used on mapped field.');
}

if (!($user = $parentForm?->getData()) || !$user instanceof PasswordAuthenticatedUserInterface) {
throw new InvalidConfigurationException(sprintf('The "hash_property_path" option only supports "%s" objects, "%s" given.', PasswordAuthenticatedUserInterface::class, get_debug_type($user)));
}
$this->assertNotMapped($event->getForm());

$this->passwords[] = [
'user' => $user,
'property_path' => $form->getConfig()->getOption('hash_property_path'),
'form' => $event->getForm(),
'property_path' => $event->getForm()->getConfig()->getOption('hash_property_path'),
'password' => $event->getData(),
];
}
Expand All @@ -69,14 +56,45 @@ public function hashPasswords(FormEvent $event)

if ($form->isValid()) {
foreach ($this->passwords as $password) {
$user = $this->getUser($password['form']);

$this->propertyAccessor->setValue(
$password['user'],
$user,
$password['property_path'],
$this->passwordHasher->hashPassword($password['user'], $password['password'])
$this->passwordHasher->hashPassword($user, $password['password'])
);
}
}

$this->passwords = [];
}

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

if ($parent && $parent->getConfig()->getType()->getInnerType() instanceof RepeatedType) {
return $parent;
}

return $form;
}

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

if (!($user = $parent?->getData()) || !$user instanceof PasswordAuthenticatedUserInterface) {
throw new InvalidConfigurationException(sprintf('The "hash_property_path" option only supports "%s" objects, "%s" given.', PasswordAuthenticatedUserInterface::class, get_debug_type($user)));
}

return $user;
}

private function assertNotMapped(FormInterface $form): void
{
if ($this->getTargetForm($form)->getConfig()->getMapped()) {
throw new InvalidConfigurationException('The "hash_property_path" option cannot be used on mapped field.');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,42 @@ public function testPasswordHashSuccess()
$this->assertSame($user->getPassword(), $hashedPassword);
}

public function testPasswordHashSuccessWitnEmptyData()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testPasswordHashSuccessWitnEmptyData

should be:

testPasswordHashSuccessWithEmptyData

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, but it's already fixed in the 6.2 branch :) (see d88d5b3)

{
$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('Symfony\Component\Form\Extension\Core\Type\FormType', null, [
'data_class' => User::class,
'empty_data' => function () use ($user) {
return $user;
},
])
->add('plainPassword', 'Symfony\Component\Form\Extension\Core\Type\PasswordType', [
'hash_property_path' => 'password',
'mapped' => false,
])
->getForm()
;

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

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

public function testPasswordHashOnInvalidForm()
{
$user = new User();
Expand Down