27
27
*/
28
28
class EntityUserProvider implements UserProviderInterface
29
29
{
30
+ private $ registry ;
31
+ private $ managerName ;
32
+ private $ classOrAlias ;
30
33
private $ class ;
31
- private $ repository ;
32
34
private $ property ;
33
- private $ metadata ;
34
35
35
- public function __construct (ManagerRegistry $ registry , $ class , $ property = null , $ managerName = null )
36
+ public function __construct (ManagerRegistry $ registry , $ classOrAlias , $ property = null , $ managerName = null )
36
37
{
37
- $ em = $ registry ->getManager ($ managerName );
38
- $ this ->class = $ class ;
39
- $ this ->metadata = $ em ->getClassMetadata ($ class );
40
-
41
- if (false !== strpos ($ this ->class , ': ' )) {
42
- $ this ->class = $ this ->metadata ->getName ();
43
- }
44
-
45
- $ this ->repository = $ em ->getRepository ($ class );
38
+ $ this ->registry = $ registry ;
39
+ $ this ->managerName = $ managerName ;
40
+ $ this ->classOrAlias = $ classOrAlias ;
46
41
$ this ->property = $ property ;
47
42
}
48
43
@@ -51,18 +46,19 @@ public function __construct(ManagerRegistry $registry, $class, $property = null,
51
46
*/
52
47
public function loadUserByUsername ($ username )
53
48
{
49
+ $ repository = $ this ->getRepository ();
54
50
if (null !== $ this ->property ) {
55
- $ user = $ this -> repository ->findOneBy (array ($ this ->property => $ username ));
51
+ $ user = $ repository ->findOneBy (array ($ this ->property => $ username ));
56
52
} else {
57
- if (!$ this -> repository instanceof UserLoaderInterface) {
58
- if (!$ this -> repository instanceof UserProviderInterface) {
59
- throw new \InvalidArgumentException (sprintf ('The Doctrine repository "%s" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface. ' , get_class ($ this -> repository )));
53
+ if (!$ repository instanceof UserLoaderInterface) {
54
+ if (!$ repository instanceof UserProviderInterface) {
55
+ throw new \InvalidArgumentException (sprintf ('The Doctrine repository "%s" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface. ' , get_class ($ repository )));
60
56
}
61
57
62
58
@trigger_error ('Implementing loadUserByUsername from Symfony\Component\Security\Core\User\UserProviderInterface is deprecated since version 2.8 and will be removed in 3.0. Implement the Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface instead. ' , E_USER_DEPRECATED );
63
59
}
64
60
65
- $ user = $ this -> repository ->loadUserByUsername ($ username );
61
+ $ user = $ repository ->loadUserByUsername ($ username );
66
62
}
67
63
68
64
if (null === $ user ) {
@@ -77,26 +73,28 @@ public function loadUserByUsername($username)
77
73
*/
78
74
public function refreshUser (UserInterface $ user )
79
75
{
80
- if (!$ user instanceof $ this ->class ) {
76
+ $ class = $ this ->getClass ();
77
+ if (!$ user instanceof $ class ) {
81
78
throw new UnsupportedUserException (sprintf ('Instances of "%s" are not supported. ' , get_class ($ user )));
82
79
}
83
80
84
- if ($ this ->repository instanceof UserProviderInterface) {
85
- $ refreshedUser = $ this ->repository ->refreshUser ($ user );
81
+ $ repository = $ this ->getRepository ();
82
+ if ($ repository instanceof UserProviderInterface) {
83
+ $ refreshedUser = $ repository ->refreshUser ($ user );
86
84
} else {
87
85
// The user must be reloaded via the primary key as all other data
88
86
// might have changed without proper persistence in the database.
89
87
// That's the case when the user has been changed by a form with
90
88
// validation errors.
91
- if (!$ id = $ this ->metadata ->getIdentifierValues ($ user )) {
89
+ if (!$ id = $ this ->getClassMetadata () ->getIdentifierValues ($ user )) {
92
90
throw new \InvalidArgumentException ('You cannot refresh a user ' .
93
91
'from the EntityUserProvider that does not contain an identifier. ' .
94
92
'The user object has to be serialized with its own identifier ' .
95
93
'mapped by Doctrine. '
96
94
);
97
95
}
98
96
99
- $ refreshedUser = $ this -> repository ->find ($ id );
97
+ $ refreshedUser = $ repository ->find ($ id );
100
98
if (null === $ refreshedUser ) {
101
99
throw new UsernameNotFoundException (sprintf ('User with id %s not found ' , json_encode ($ id )));
102
100
}
@@ -110,6 +108,36 @@ public function refreshUser(UserInterface $user)
110
108
*/
111
109
public function supportsClass ($ class )
112
110
{
113
- return $ class === $ this ->class || is_subclass_of ($ class , $ this ->class );
111
+ return $ class === $ this ->getClass () || is_subclass_of ($ class , $ this ->getClass ());
112
+ }
113
+
114
+ private function getObjectManager ()
115
+ {
116
+ return $ this ->registry ->getManager ($ this ->managerName );
117
+ }
118
+
119
+ private function getRepository ()
120
+ {
121
+ return $ this ->getObjectManager ()->getRepository ($ this ->classOrAlias );
122
+ }
123
+
124
+ private function getClass ()
125
+ {
126
+ if (null === $ this ->class ) {
127
+ $ class = $ this ->classOrAlias ;
128
+
129
+ if (false !== strpos ($ class , ': ' )) {
130
+ $ class = $ this ->getClassMetadata ()->getName ();
131
+ }
132
+
133
+ $ this ->class = $ class ;
134
+ }
135
+
136
+ return $ this ->class ;
137
+ }
138
+
139
+ private function getClassMetadata ()
140
+ {
141
+ return $ this ->getObjectManager ()->getClassMetadata ($ this ->classOrAlias );
114
142
}
115
143
}
0 commit comments