Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit56c1282

Browse files
committed
[Form] Add hash_mapping option to PasswordType
1 parent823f332 commit56c1282

File tree

10 files changed

+420
-0
lines changed

10 files changed

+420
-0
lines changed

‎src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
useSymfony\Component\EventDispatcher\EventDispatcher;
3434
useSymfony\Component\ExpressionLanguage\Expression;
3535
useSymfony\Component\ExpressionLanguage\ExpressionLanguage;
36+
useSymfony\Component\Form\Extension\PasswordHasher\PasswordHasherExtension;
3637
useSymfony\Component\HttpFoundation\ChainRequestMatcher;
3738
useSymfony\Component\HttpFoundation\RequestMatcher\AttributesRequestMatcher;
3839
useSymfony\Component\HttpFoundation\RequestMatcher\HostRequestMatcher;
@@ -124,6 +125,12 @@ public function load(array $configs, ContainerBuilder $container)
124125
$container->removeDefinition('security.is_granted_attribute_expression_language');
125126
}
126127

128+
if (!class_exists(PasswordHasherExtension::class)) {
129+
$container->removeDefinition('form.listener.password_hasher');
130+
$container->removeDefinition('form.type_extension.form.password_hasher');
131+
$container->removeDefinition('form.type_extension.password.password_hasher');
132+
}
133+
127134
// set some global scalars
128135
$container->setParameter('security.access.denied_url',$config['access_denied_url']);
129136
$container->setParameter('security.authentication.manager.erase_credentials',$config['erase_credentials']);

‎src/Symfony/Bundle/SecurityBundle/Resources/config/password_hasher.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
namespaceSymfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
useSymfony\Component\Form\Extension\Core\Type\FormType;
15+
useSymfony\Component\Form\Extension\Core\Type\PasswordType;
16+
useSymfony\Component\Form\Extension\PasswordHasher\EventListener\PasswordHasherListener;
17+
useSymfony\Component\Form\Extension\PasswordHasher\Type\FormTypePasswordHasherExtension;
18+
useSymfony\Component\Form\Extension\PasswordHasher\Type\PasswordTypePasswordHasherExtension;
1419
useSymfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;
1520
useSymfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
1621
useSymfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
@@ -26,5 +31,23 @@
2631
->args([service('security.password_hasher_factory')])
2732
->alias('security.password_hasher','security.user_password_hasher')
2833
->alias(UserPasswordHasherInterface::class,'security.password_hasher')
34+
35+
->set('form.listener.password_hasher', PasswordHasherListener::class)
36+
->args([
37+
service('security.password_hasher'),
38+
service('property_accessor')->nullOnInvalid(),
39+
])
40+
41+
->set('form.type_extension.form.password_hasher', FormTypePasswordHasherExtension::class)
42+
->args([
43+
service('form.listener.password_hasher'),
44+
])
45+
->tag('form.type_extension', ['extended-type' => FormType::class])
46+
47+
->set('form.type_extension.password.password_hasher', PasswordTypePasswordHasherExtension::class)
48+
->args([
49+
service('form.listener.password_hasher'),
50+
])
51+
->tag('form.type_extension', ['extended-type' => PasswordType::class])
2952
;
3053
};

‎src/Symfony/Component/Form/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* Deprecate calling`Button/Form::setParent()`,`ButtonBuilder/FormConfigBuilder::setDataMapper()`,`TransformationFailedException::setInvalidMessage()` without arguments
1010
* Change the signature of`FormConfigBuilderInterface::setDataMapper()` to`setDataMapper(?DataMapperInterface)`
1111
* Change the signature of`FormInterface::setParent()` to`setParent(?self)`
12+
* Add`PasswordHasherExtension` with support for`hash_property_path` option in`PasswordType`
1213

1314
6.1
1415
---
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespaceSymfony\Component\Form\Extension\PasswordHasher\EventListener;
13+
14+
useSymfony\Component\Form\Exception\InvalidConfigurationException;
15+
useSymfony\Component\Form\Extension\Core\Type\RepeatedType;
16+
useSymfony\Component\Form\FormEvent;
17+
useSymfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
18+
useSymfony\Component\PropertyAccess\PropertyAccess;
19+
useSymfony\Component\PropertyAccess\PropertyAccessorInterface;
20+
useSymfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
21+
22+
/**
23+
* @author Sébastien Alfaiate <s.alfaiate@webarea.fr>
24+
*/
25+
class PasswordHasherListener
26+
{
27+
privatearray$passwords = [];
28+
29+
publicfunction__construct(
30+
privateUserPasswordHasherInterface$passwordHasher,
31+
private ?PropertyAccessorInterface$propertyAccessor =null,
32+
) {
33+
$this->propertyAccessor ??= PropertyAccess::createPropertyAccessor();
34+
}
35+
36+
publicfunctionregisterPassword(FormEvent$event)
37+
{
38+
$form =$event->getForm();
39+
$parentForm =$form->getParent();
40+
$mapped =$form->getConfig()->getMapped();
41+
42+
if ($parentForm &&$parentForm->getConfig()->getType()->getInnerType()instanceof RepeatedType) {
43+
$mapped =$parentForm->getConfig()->getMapped();
44+
$parentForm =$parentForm->getParent();
45+
}
46+
47+
if ($mapped) {
48+
thrownewInvalidConfigurationException('The "hash_property_path" option cannot be used on mapped field.');
49+
}
50+
51+
if (!$parentForm || !($user =$parentForm->getData()) || !$userinstanceof PasswordAuthenticatedUserInterface) {
52+
thrownewInvalidConfigurationException(sprintf('The "hash_property_path" option only supports "%s" objects, "%s" given.', PasswordAuthenticatedUserInterface::class,get_debug_type($user ??null)));
53+
}
54+
55+
$this->passwords[] = [
56+
'user' =>$user,
57+
'property_path' =>$form->getConfig()->getOption('hash_property_path'),
58+
'password' =>$event->getData(),
59+
];
60+
}
61+
62+
publicfunctionhashPasswords(FormEvent$event)
63+
{
64+
$form =$event->getForm();
65+
66+
if (!$form->isRoot()) {
67+
return;
68+
}
69+
70+
if ($form->isValid()) {
71+
foreach ($this->passwordsas$password) {
72+
$this->propertyAccessor->setValue(
73+
$password['user'],
74+
$password['property_path'],
75+
$this->passwordHasher->hashPassword($password['user'],$password['password'])
76+
);
77+
}
78+
}
79+
80+
$this->passwords = [];
81+
}
82+
}
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 <fabien@symfony.com>
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+
namespaceSymfony\Component\Form\Extension\PasswordHasher;
13+
14+
useSymfony\Component\Form\AbstractExtension;
15+
useSymfony\Component\Form\Extension\PasswordHasher\EventListener\PasswordHasherListener;
16+
17+
/**
18+
* Integrates the PasswordHasher component with the Form library.
19+
*
20+
* @author Sébastien Alfaiate <s.alfaiate@webarea.fr>
21+
*/
22+
class PasswordHasherExtensionextends AbstractExtension
23+
{
24+
publicfunction__construct(
25+
privatePasswordHasherListener$passwordHasherListener,
26+
) {
27+
}
28+
29+
protectedfunctionloadTypeExtensions():array
30+
{
31+
return [
32+
newType\FormTypePasswordHasherExtension($this->passwordHasherListener),
33+
newType\PasswordTypePasswordHasherExtension($this->passwordHasherListener),
34+
];
35+
}
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespaceSymfony\Component\Form\Extension\PasswordHasher\Type;
13+
14+
useSymfony\Component\Form\AbstractTypeExtension;
15+
useSymfony\Component\Form\Extension\Core\Type\FormType;
16+
useSymfony\Component\Form\Extension\PasswordHasher\EventListener\PasswordHasherListener;
17+
useSymfony\Component\Form\FormBuilderInterface;
18+
useSymfony\Component\Form\FormEvents;
19+
20+
/**
21+
* @author Sébastien Alfaiate <s.alfaiate@webarea.fr>
22+
*/
23+
class FormTypePasswordHasherExtensionextends AbstractTypeExtension
24+
{
25+
publicfunction__construct(
26+
privatePasswordHasherListener$passwordHasherListener,
27+
) {
28+
}
29+
30+
publicfunctionbuildForm(FormBuilderInterface$builder,array$options)
31+
{
32+
$builder->addEventListener(FormEvents::POST_SUBMIT, [$this->passwordHasherListener,'hashPasswords']);
33+
}
34+
35+
publicstaticfunctiongetExtendedTypes():iterable
36+
{
37+
return [FormType::class];
38+
}
39+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespaceSymfony\Component\Form\Extension\PasswordHasher\Type;
13+
14+
useSymfony\Component\Form\AbstractTypeExtension;
15+
useSymfony\Component\Form\Extension\Core\Type\PasswordType;
16+
useSymfony\Component\Form\Extension\PasswordHasher\EventListener\PasswordHasherListener;
17+
useSymfony\Component\Form\FormBuilderInterface;
18+
useSymfony\Component\Form\FormEvents;
19+
useSymfony\Component\OptionsResolver\OptionsResolver;
20+
useSymfony\Component\PropertyAccess\PropertyPath;
21+
22+
/**
23+
* @author Sébastien Alfaiate <s.alfaiate@webarea.fr>
24+
*/
25+
class PasswordTypePasswordHasherExtensionextends AbstractTypeExtension
26+
{
27+
publicfunction__construct(
28+
privatePasswordHasherListener$passwordHasherListener,
29+
) {
30+
}
31+
32+
publicfunctionbuildForm(FormBuilderInterface$builder,array$options)
33+
{
34+
if ($options['hash_property_path']) {
35+
$builder->addEventListener(FormEvents::POST_SUBMIT, [$this->passwordHasherListener,'registerPassword']);
36+
}
37+
}
38+
39+
publicfunctionconfigureOptions(OptionsResolver$resolver)
40+
{
41+
$resolver->setDefaults([
42+
'hash_property_path' =>null,
43+
]);
44+
45+
$resolver->setAllowedTypes('hash_property_path', ['null','string', PropertyPath::class]);
46+
47+
$resolver->setInfo('hash_property_path','A valid PropertyAccess syntax where the hashed password will be set.');
48+
}
49+
50+
publicstaticfunctiongetExtendedTypes():iterable
51+
{
52+
return [PasswordType::class];
53+
}
54+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp