11
11
12
12
namespace Symfony \Component \Security \Core \User ;
13
13
14
+ @trigger_error (sprintf ('The "%s" class is deprecated since Symfony 4.4, use "%s" instead. ' , LdapUserProvider::class, BaseLdapUserProvider::class), E_USER_DEPRECATED );
15
+
14
16
use Symfony \Component \Ldap \Entry ;
15
- use Symfony \Component \Ldap \Exception \ConnectionException ;
16
- use Symfony \Component \Ldap \LdapInterface ;
17
- use Symfony \Component \Security \Core \Exception \InvalidArgumentException ;
17
+ use Symfony \Component \Ldap \Security \LdapUserProvider as BaseLdapUserProvider ;
18
18
use Symfony \Component \Security \Core \Exception \UnsupportedUserException ;
19
- use Symfony \Component \Security \Core \Exception \UsernameNotFoundException ;
20
19
21
20
/**
22
21
* LdapUserProvider is a simple user provider on top of ldap.
23
22
*
24
23
* @author Grégoire Pineau <[email protected] >
25
24
* @author Charles Sarrazin <[email protected] >
25
+ *
26
+ * @deprecated since Symfony 4.4, use "Symfony\Component\Ldap\Security\LdapUserProvider" instead
26
27
*/
27
- class LdapUserProvider implements UserProviderInterface
28
+ class LdapUserProvider extends BaseLdapUserProvider
28
29
{
29
- private $ ldap ;
30
- private $ baseDn ;
31
- private $ searchDn ;
32
- private $ searchPassword ;
33
- private $ defaultRoles ;
34
- private $ uidKey ;
35
- private $ defaultSearch ;
36
- private $ passwordAttribute ;
37
- private $ extraFields ;
38
-
39
- public function __construct (LdapInterface $ ldap , string $ baseDn , string $ searchDn = null , string $ searchPassword = null , array $ defaultRoles = [], string $ uidKey = null , string $ filter = null , string $ passwordAttribute = null , array $ extraFields = [])
40
- {
41
- if (null === $ uidKey ) {
42
- $ uidKey = 'sAMAccountName ' ;
43
- }
44
-
45
- if (null === $ filter ) {
46
- $ filter = '({uid_key}={username}) ' ;
47
- }
48
-
49
- $ this ->ldap = $ ldap ;
50
- $ this ->baseDn = $ baseDn ;
51
- $ this ->searchDn = $ searchDn ;
52
- $ this ->searchPassword = $ searchPassword ;
53
- $ this ->defaultRoles = $ defaultRoles ;
54
- $ this ->uidKey = $ uidKey ;
55
- $ this ->defaultSearch = str_replace ('{uid_key} ' , $ uidKey , $ filter );
56
- $ this ->passwordAttribute = $ passwordAttribute ;
57
- $ this ->extraFields = $ extraFields ;
58
- }
59
-
60
- /**
61
- * {@inheritdoc}
62
- */
63
- public function loadUserByUsername ($ username )
64
- {
65
- try {
66
- $ this ->ldap ->bind ($ this ->searchDn , $ this ->searchPassword );
67
- $ username = $ this ->ldap ->escape ($ username , '' , LdapInterface::ESCAPE_FILTER );
68
- $ query = str_replace ('{username} ' , $ username , $ this ->defaultSearch );
69
- $ search = $ this ->ldap ->query ($ this ->baseDn , $ query );
70
- } catch (ConnectionException $ e ) {
71
- throw new UsernameNotFoundException (sprintf ('User "%s" not found. ' , $ username ), 0 , $ e );
72
- }
73
-
74
- $ entries = $ search ->execute ();
75
- $ count = \count ($ entries );
76
-
77
- if (!$ count ) {
78
- throw new UsernameNotFoundException (sprintf ('User "%s" not found. ' , $ username ));
79
- }
80
-
81
- if ($ count > 1 ) {
82
- throw new UsernameNotFoundException ('More than one user found ' );
83
- }
84
-
85
- $ entry = $ entries [0 ];
86
-
87
- try {
88
- if (null !== $ this ->uidKey ) {
89
- $ username = $ this ->getAttributeValue ($ entry , $ this ->uidKey );
90
- }
91
- } catch (InvalidArgumentException $ e ) {
92
- }
93
-
94
- return $ this ->loadUser ($ username , $ entry );
95
- }
96
-
97
30
/**
98
31
* {@inheritdoc}
99
32
*/
@@ -117,42 +50,12 @@ public function supportsClass($class)
117
50
/**
118
51
* Loads a user from an LDAP entry.
119
52
*
120
- * @param string $username
121
- * @param Entry $entry
122
- *
123
53
* @return User
124
54
*/
125
55
protected function loadUser ($ username , Entry $ entry )
126
56
{
127
- $ password = null ;
128
- $ extraFields = [];
129
-
130
- if (null !== $ this ->passwordAttribute ) {
131
- $ password = $ this ->getAttributeValue ($ entry , $ this ->passwordAttribute );
132
- }
133
-
134
- foreach ($ this ->extraFields as $ field ) {
135
- $ extraFields [$ field ] = $ this ->getAttributeValue ($ entry , $ field );
136
- }
137
-
138
- return new User ($ username , $ password , $ this ->defaultRoles , true , true , true , true , $ extraFields );
139
- }
140
-
141
- /**
142
- * Fetches a required unique attribute value from an LDAP entry.
143
- */
144
- private function getAttributeValue (Entry $ entry , string $ attribute )
145
- {
146
- if (!$ entry ->hasAttribute ($ attribute )) {
147
- throw new InvalidArgumentException (sprintf ('Missing attribute "%s" for user "%s". ' , $ attribute , $ entry ->getDn ()));
148
- }
149
-
150
- $ values = $ entry ->getAttribute ($ attribute );
151
-
152
- if (1 !== \count ($ values )) {
153
- throw new InvalidArgumentException (sprintf ('Attribute "%s" has multiple values. ' , $ attribute ));
154
- }
57
+ $ ldapUser = parent ::loadUser ($ username , $ entry );
155
58
156
- return $ values [ 0 ] ;
59
+ return new User ( $ ldapUser -> getUsername (), $ ldapUser -> getPassword (), $ ldapUser -> getRoles (), true , true , true , true , $ ldapUser -> getExtraFields ()) ;
157
60
}
158
61
}
0 commit comments