-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Add hash_property_path
option to PasswordType
#46224
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/Symfony/Component/Form/Extension/PasswordHasher/EventListener/PasswordHasherListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\PasswordHasher\EventListener; | ||
|
||
use Symfony\Component\Form\Exception\InvalidConfigurationException; | ||
use Symfony\Component\Form\Extension\Core\Type\RepeatedType; | ||
use Symfony\Component\Form\FormEvent; | ||
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> | ||
*/ | ||
class PasswordHasherListener | ||
{ | ||
private array $passwords = []; | ||
|
||
public function __construct( | ||
private UserPasswordHasherInterface $passwordHasher, | ||
private ?PropertyAccessorInterface $propertyAccessor = null, | ||
) { | ||
$this->propertyAccessor ??= PropertyAccess::createPropertyAccessor(); | ||
} | ||
|
||
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->passwords[] = [ | ||
'user' => $user, | ||
'property_path' => $form->getConfig()->getOption('hash_property_path'), | ||
'password' => $event->getData(), | ||
]; | ||
} | ||
|
||
public function hashPasswords(FormEvent $event) | ||
{ | ||
$form = $event->getForm(); | ||
|
||
if (!$form->isRoot()) { | ||
return; | ||
} | ||
|
||
if ($form->isValid()) { | ||
foreach ($this->passwords as $password) { | ||
$this->propertyAccessor->setValue( | ||
$password['user'], | ||
$password['property_path'], | ||
$this->passwordHasher->hashPassword($password['user'], $password['password']) | ||
); | ||
} | ||
} | ||
|
||
$this->passwords = []; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Symfony/Component/Form/Extension/PasswordHasher/PasswordHasherExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\PasswordHasher; | ||
|
||
use Symfony\Component\Form\AbstractExtension; | ||
use Symfony\Component\Form\Extension\PasswordHasher\EventListener\PasswordHasherListener; | ||
|
||
/** | ||
* Integrates the PasswordHasher component with the Form library. | ||
* | ||
* @author Sébastien Alfaiate <s.alfaiate@webarea.fr> | ||
*/ | ||
class PasswordHasherExtension extends AbstractExtension | ||
{ | ||
public function __construct( | ||
private PasswordHasherListener $passwordHasherListener, | ||
) { | ||
} | ||
|
||
protected function loadTypeExtensions(): array | ||
{ | ||
return [ | ||
new Type\FormTypePasswordHasherExtension($this->passwordHasherListener), | ||
new Type\PasswordTypePasswordHasherExtension($this->passwordHasherListener), | ||
]; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/Form/Extension/PasswordHasher/Type/FormTypePasswordHasherExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\PasswordHasher\Type; | ||
|
||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\Extension\Core\Type\FormType; | ||
use Symfony\Component\Form\Extension\PasswordHasher\EventListener\PasswordHasherListener; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormEvents; | ||
|
||
/** | ||
* @author Sébastien Alfaiate <s.alfaiate@webarea.fr> | ||
*/ | ||
class FormTypePasswordHasherExtension extends AbstractTypeExtension | ||
{ | ||
public function __construct( | ||
private PasswordHasherListener $passwordHasherListener, | ||
) { | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder->addEventListener(FormEvents::POST_SUBMIT, [$this->passwordHasherListener, 'hashPasswords']); | ||
} | ||
|
||
public static function getExtendedTypes(): iterable | ||
{ | ||
return [FormType::class]; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...fony/Component/Form/Extension/PasswordHasher/Type/PasswordTypePasswordHasherExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\PasswordHasher\Type; | ||
|
||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\Extension\Core\Type\PasswordType; | ||
use Symfony\Component\Form\Extension\PasswordHasher\EventListener\PasswordHasherListener; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\PropertyAccess\PropertyPath; | ||
|
||
/** | ||
* @author Sébastien Alfaiate <s.alfaiate@webarea.fr> | ||
*/ | ||
class PasswordTypePasswordHasherExtension extends AbstractTypeExtension | ||
{ | ||
public function __construct( | ||
private PasswordHasherListener $passwordHasherListener, | ||
) { | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
if ($options['hash_property_path']) { | ||
$builder->addEventListener(FormEvents::POST_SUBMIT, [$this->passwordHasherListener, 'registerPassword']); | ||
} | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults([ | ||
'hash_property_path' => null, | ||
yceruto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]); | ||
|
||
$resolver->setAllowedTypes('hash_property_path', ['null', 'string', PropertyPath::class]); | ||
|
||
$resolver->setInfo('hash_property_path', 'A valid PropertyAccess syntax where the hashed password will be set.'); | ||
} | ||
|
||
public static function getExtendedTypes(): iterable | ||
{ | ||
return [PasswordType::class]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.