14
14
use Symfony \Component \Form \Exception \InvalidConfigurationException ;
15
15
use Symfony \Component \Form \Extension \Core \Type \RepeatedType ;
16
16
use Symfony \Component \Form \FormEvent ;
17
+ use Symfony \Component \Form \FormInterface ;
17
18
use Symfony \Component \PasswordHasher \Hasher \UserPasswordHasherInterface ;
18
19
use Symfony \Component \PropertyAccess \PropertyAccess ;
19
20
use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
20
21
use Symfony \Component \Security \Core \User \PasswordAuthenticatedUserInterface ;
21
22
22
23
/**
23
24
* @author Sébastien Alfaiate <[email protected] >
25
+ * @author Gábor Egyed <[email protected] >
24
26
*/
25
27
class PasswordHasherListener
26
28
{
@@ -35,26 +37,11 @@ public function __construct(
35
37
36
38
public function registerPassword (FormEvent $ event )
37
39
{
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
- throw new InvalidConfigurationException ('The "hash_property_path" option cannot be used on mapped field. ' );
49
- }
50
-
51
- if (!($ user = $ parentForm ?->getData()) || !$ user instanceof PasswordAuthenticatedUserInterface) {
52
- throw new InvalidConfigurationException (sprintf ('The "hash_property_path" option only supports "%s" objects, "%s" given. ' , PasswordAuthenticatedUserInterface::class, get_debug_type ($ user )));
53
- }
40
+ $ this ->assertNotMapped ($ event ->getForm ());
54
41
55
42
$ this ->passwords [] = [
56
- 'user ' => $ user ,
57
- 'property_path ' => $ form ->getConfig ()->getOption ('hash_property_path ' ),
43
+ 'form ' => $ event -> getForm () ,
44
+ 'property_path ' => $ event -> getForm () ->getConfig ()->getOption ('hash_property_path ' ),
58
45
'password ' => $ event ->getData (),
59
46
];
60
47
}
@@ -69,14 +56,45 @@ public function hashPasswords(FormEvent $event)
69
56
70
57
if ($ form ->isValid ()) {
71
58
foreach ($ this ->passwords as $ password ) {
59
+ $ user = $ this ->getUser ($ password ['form ' ]);
60
+
72
61
$ this ->propertyAccessor ->setValue (
73
- $ password [ ' user ' ] ,
62
+ $ user ,
74
63
$ password ['property_path ' ],
75
- $ this ->passwordHasher ->hashPassword ($ password [ ' user ' ] , $ password ['password ' ])
64
+ $ this ->passwordHasher ->hashPassword ($ user , $ password ['password ' ])
76
65
);
77
66
}
78
67
}
79
68
80
69
$ this ->passwords = [];
81
70
}
71
+
72
+ private function getTargetForm (FormInterface $ form ): FormInterface
73
+ {
74
+ $ parent = $ form ->getParent ();
75
+
76
+ if ($ parent && $ parent ->getConfig ()->getType ()->getInnerType () instanceof RepeatedType) {
77
+ return $ parent ;
78
+ }
79
+
80
+ return $ form ;
81
+ }
82
+
83
+ private function getUser (FormInterface $ form ): PasswordAuthenticatedUserInterface
84
+ {
85
+ $ parent = $ this ->getTargetForm ($ form )->getParent ();
86
+
87
+ if (!($ user = $ parent ?->getData()) || !$ user instanceof PasswordAuthenticatedUserInterface) {
88
+ throw new InvalidConfigurationException (sprintf ('The "hash_property_path" option only supports "%s" objects, "%s" given. ' , PasswordAuthenticatedUserInterface::class, get_debug_type ($ user )));
89
+ }
90
+
91
+ return $ user ;
92
+ }
93
+
94
+ private function assertNotMapped (FormInterface $ form ): void
95
+ {
96
+ if ($ this ->getTargetForm ($ form )->getConfig ()->getMapped ()) {
97
+ throw new InvalidConfigurationException ('The "hash_property_path" option cannot be used on mapped field. ' );
98
+ }
99
+ }
82
100
}
0 commit comments