32
32
*/
33
33
class AuthLdap extends Auth implements AuthDriverInterface
34
34
{
35
- /** @var LdapCore */
36
- private $ ldap = null ;
35
+ /** @var LdapCore|null */
36
+ private ? LdapCore $ ldap = null ;
37
37
38
38
/** @var string[] Array of LDAP servers */
39
- private $ ldapServer ;
39
+ private array $ ldapServer ;
40
40
41
41
/** @var int Active LDAP server */
42
- private $ activeServer = 0 ;
42
+ private int $ activeServer = 0 ;
43
43
44
44
/** @var bool */
45
45
private $ multipleServers ;
@@ -61,17 +61,7 @@ public function __construct(Configuration $config)
61
61
}
62
62
63
63
$ this ->ldap = new LdapCore ($ this ->config );
64
- $ this ->ldap ->connect (
65
- $ this ->ldapServer [$ this ->activeServer ]['ldap_server ' ],
66
- $ this ->ldapServer [$ this ->activeServer ]['ldap_port ' ],
67
- $ this ->ldapServer [$ this ->activeServer ]['ldap_base ' ],
68
- $ this ->ldapServer [$ this ->activeServer ]['ldap_user ' ],
69
- $ this ->ldapServer [$ this ->activeServer ]['ldap_password ' ]
70
- );
71
-
72
- if ($ this ->ldap ->error ) {
73
- $ this ->errors [] = $ this ->ldap ->error ;
74
- }
64
+ $ this ->connect ($ this ->activeServer );
75
65
}
76
66
77
67
/**
@@ -83,17 +73,7 @@ public function create($login, $password, $domain = ''): bool
83
73
$ user = new User ($ this ->config );
84
74
$ result = $ user ->createUser ($ login , '' , $ domain );
85
75
86
- $ this ->ldap ->connect (
87
- $ this ->ldapServer [$ this ->activeServer ]['ldap_server ' ],
88
- $ this ->ldapServer [$ this ->activeServer ]['ldap_port ' ],
89
- $ this ->ldapServer [$ this ->activeServer ]['ldap_base ' ],
90
- $ this ->ldapServer [$ this ->activeServer ]['ldap_user ' ],
91
- $ this ->ldapServer [$ this ->activeServer ]['ldap_password ' ]
92
- );
93
-
94
- if ($ this ->ldap ->error ) {
95
- $ this ->errors [] = $ this ->ldap ->error ;
96
- }
76
+ $ this ->connect ($ this ->activeServer );
97
77
98
78
$ user ->setStatus ('active ' );
99
79
@@ -126,29 +106,20 @@ public function delete($login): bool
126
106
127
107
/**
128
108
* @inheritDoc
109
+ * @throws Exception
129
110
*/
130
111
public function checkCredentials ($ login , $ password , array $ optionalData = null ): bool
131
112
{
132
113
if ('' === trim ($ password )) {
133
114
$ this ->errors [] = User::ERROR_USER_INCORRECT_PASSWORD ;
134
-
135
115
return false ;
136
116
}
137
117
138
118
// Get active LDAP server for current user
139
119
if ($ this ->multipleServers ) {
140
120
// Try all LDAP servers
141
121
foreach ($ this ->ldapServer as $ key => $ value ) {
142
- $ this ->ldap ->connect (
143
- $ this ->ldapServer [$ key ]['ldap_server ' ],
144
- $ this ->ldapServer [$ key ]['ldap_port ' ],
145
- $ this ->ldapServer [$ key ]['ldap_base ' ],
146
- $ this ->ldapServer [$ key ]['ldap_user ' ],
147
- $ this ->ldapServer [$ key ]['ldap_password ' ]
148
- );
149
- if ($ this ->ldap ->error ) {
150
- $ this ->errors [] = $ this ->ldap ->error ;
151
- }
122
+ $ this ->connect ($ key );
152
123
153
124
if (false !== $ this ->ldap ->getDn ($ login )) {
154
125
$ this ->activeServer = (int )$ key ;
@@ -163,16 +134,7 @@ public function checkCredentials($login, $password, array $optionalData = null):
163
134
$ bindLogin = $ optionalData ['domain ' ] . '\\' . $ login ;
164
135
}
165
136
} else {
166
- $ this ->ldap ->connect (
167
- $ this ->ldapServer [$ this ->activeServer ]['ldap_server ' ],
168
- $ this ->ldapServer [$ this ->activeServer ]['ldap_port ' ],
169
- $ this ->ldapServer [$ this ->activeServer ]['ldap_base ' ],
170
- $ this ->ldapServer [$ this ->activeServer ]['ldap_user ' ],
171
- $ this ->ldapServer [$ this ->activeServer ]['ldap_password ' ]
172
- );
173
- if ($ this ->ldap ->error ) {
174
- $ this ->errors [] = $ this ->ldap ->error ;
175
- }
137
+ $ this ->connect ($ this ->activeServer );
176
138
177
139
$ bindLogin = $ this ->ldap ->getDn ($ login );
178
140
}
@@ -205,16 +167,7 @@ public function isValidLogin($login, array $optionalData = null): int
205
167
if ($ this ->multipleServers ) {
206
168
// Try all LDAP servers
207
169
foreach ($ this ->ldapServer as $ key => $ value ) {
208
- $ this ->ldap ->connect (
209
- $ this ->ldapServer [$ key ]['ldap_server ' ],
210
- $ this ->ldapServer [$ key ]['ldap_port ' ],
211
- $ this ->ldapServer [$ key ]['ldap_base ' ],
212
- $ this ->ldapServer [$ key ]['ldap_user ' ],
213
- $ this ->ldapServer [$ key ]['ldap_password ' ]
214
- );
215
- if ($ this ->ldap ->error ) {
216
- $ this ->errors [] = $ this ->ldap ->error ;
217
- }
170
+ $ this ->connect ($ key );
218
171
219
172
if (false !== $ this ->ldap ->getDn ($ login )) {
220
173
$ this ->activeServer = (int )$ key ;
@@ -223,14 +176,26 @@ public function isValidLogin($login, array $optionalData = null): int
223
176
}
224
177
}
225
178
179
+ $ this ->connect ($ this ->activeServer );
180
+
181
+ return strlen ($ this ->ldap ->getCompleteName ($ login ));
182
+ }
183
+
184
+ /**
185
+ * @param int $activeServer
186
+ */
187
+ private function connect (int $ activeServer = 0 ): void
188
+ {
226
189
$ this ->ldap ->connect (
227
- $ this ->ldapServer [$ this -> activeServer ]['ldap_server ' ],
228
- $ this ->ldapServer [$ this -> activeServer ]['ldap_port ' ],
229
- $ this ->ldapServer [$ this -> activeServer ]['ldap_base ' ],
230
- $ this ->ldapServer [$ this -> activeServer ]['ldap_user ' ],
231
- $ this ->ldapServer [$ this -> activeServer ]['ldap_password ' ]
190
+ $ this ->ldapServer [$ activeServer ]['ldap_server ' ],
191
+ $ this ->ldapServer [$ activeServer ]['ldap_port ' ],
192
+ $ this ->ldapServer [$ activeServer ]['ldap_base ' ],
193
+ $ this ->ldapServer [$ activeServer ]['ldap_user ' ],
194
+ $ this ->ldapServer [$ activeServer ]['ldap_password ' ]
232
195
);
233
196
234
- return strlen ($ this ->ldap ->getCompleteName ($ login ));
197
+ if ($ this ->ldap ->error ) {
198
+ $ this ->errors [] = $ this ->ldap ->error ;
199
+ }
235
200
}
236
201
}
0 commit comments