Révision 32700c57
Ajouté par Assos Assos il y a environ 5 ans
drupal7/sites/all/modules/ldap/ldap_test/LdapServerTest.class.php | ||
---|---|---|
2 | 2 |
|
3 | 3 |
/** |
4 | 4 |
* @file |
5 |
* Simpletest ldapServer class for testing without an actual ldap server |
|
6 |
* |
|
5 |
* Simpletest ldapServer class for testing without an actual ldap server. |
|
7 | 6 |
*/ |
8 | 7 |
|
9 | 8 |
/** |
10 |
* LDAP Server Class |
|
9 |
* LDAP Server Class.
|
|
11 | 10 |
* |
12 | 11 |
* This class is used to create, work with, and eventually destroy ldap_server |
13 | 12 |
* objects. |
... | ... | |
16 | 15 |
*/ |
17 | 16 |
|
18 | 17 |
ldap_servers_module_load_include('php', 'ldap_servers', 'LdapServer.class'); |
19 |
|
|
18 |
/** |
|
19 |
* |
|
20 |
*/ |
|
20 | 21 |
class LdapServerTest extends LdapServer { |
21 | 22 |
|
22 | 23 |
public $entries; |
23 | 24 |
public $methodResponses; |
24 | 25 |
public $searchResults; |
25 |
public $binddn = FALSE; // Default to an anonymous bind. |
|
26 |
public $bindpw = FALSE; // Default to an anonymous bind. |
|
26 |
/** |
|
27 |
* Default to an anonymous bind. |
|
28 |
*/ |
|
29 |
public $binddn = FALSE; |
|
30 |
/** |
|
31 |
* Default to an anonymous bind. |
|
32 |
*/ |
|
33 |
public $bindpw = FALSE; |
|
27 | 34 |
|
28 | 35 |
/** |
29 |
* Constructor Method |
|
36 |
* Constructor Method.
|
|
30 | 37 |
* |
31 |
* can take array of form property_name => property_value
|
|
38 |
* Can take array of form property_name => property_value
|
|
32 | 39 |
* or $sid, where sid is used to derive the include file. |
33 | 40 |
*/ |
34 |
function __construct($sid) { |
|
41 |
public function __construct($sid) {
|
|
35 | 42 |
if (!is_scalar($sid)) { |
36 | 43 |
$test_data = $sid; |
37 | 44 |
$sid = $test_data['sid']; |
38 | 45 |
} |
39 | 46 |
else { |
40 |
$test_data = variable_get('ldap_test_server__' . $sid, array());
|
|
47 |
$test_data = variable_get('ldap_test_server__' . $sid, []);
|
|
41 | 48 |
} |
42 | 49 |
|
43 | 50 |
$bindpw = (isset($test_data['bindpw'])) ? $test_data['bindpw'] : 'goodpwd'; |
... | ... | |
46 | 53 |
$this->initDerivedProperties($bindpw); |
47 | 54 |
} |
48 | 55 |
|
56 |
/** |
|
57 |
* |
|
58 |
*/ |
|
49 | 59 |
public function refreshFakeData() { |
50 |
$test_data = variable_get('ldap_test_server__' . $this->sid, array());
|
|
51 |
$this->methodResponses = (is_array($test_data) && isset($test_data['methodResponses'])) ? $test_data['methodResponses'] : array();
|
|
52 |
$this->entries = (is_array($test_data) && isset($test_data['ldap'])) ? $test_data['ldap'] : array();
|
|
53 |
$this->searchResults = (isset($test_data['search_results'])) ? $test_data['search_results'] : array();
|
|
60 |
$test_data = variable_get('ldap_test_server__' . $this->sid, []);
|
|
61 |
$this->methodResponses = (is_array($test_data) && isset($test_data['methodResponses'])) ? $test_data['methodResponses'] : [];
|
|
62 |
$this->entries = (is_array($test_data) && isset($test_data['ldap'])) ? $test_data['ldap'] : [];
|
|
63 |
$this->searchResults = (isset($test_data['search_results'])) ? $test_data['search_results'] : [];
|
|
54 | 64 |
$this->detailedWatchdogLog = variable_get('ldap_help_watchdog_detail', 0); |
55 |
foreach ($test_data['properties'] as $property_name => $property_value ) {
|
|
65 |
foreach ($test_data['properties'] as $property_name => $property_value) { |
|
56 | 66 |
$this->{$property_name} = $property_value; |
57 | 67 |
} |
58 |
// $this->basedn = unserialize($this->basedn); |
|
59 | 68 |
if (isset($test_data['bindpw']) && $test_data['bindpw'] != '') { |
60 | 69 |
$this->bindpw = ldap_servers_decrypt($this->bindpw); |
61 | 70 |
} |
62 | 71 |
} |
63 | 72 |
|
64 | 73 |
/** |
65 |
* Destructor Method |
|
74 |
* Destructor Method.
|
|
66 | 75 |
*/ |
67 |
function __destruct() { |
|
68 |
// if alterations to server configuration must be maintained throughout simpletest, variable_set('ldap_authorization_test_server__'. $sid, array()); |
|
76 |
public function __destruct() { |
|
77 |
// If alterations to server configuration must be maintained throughout |
|
78 |
// simpletest, Call: |
|
79 |
// variable_set('ldap_authorization_test_server__'. $sid, []);. |
|
69 | 80 |
} |
70 | 81 |
|
71 | 82 |
/** |
72 |
* Connect Method |
|
83 |
* Connect Method.
|
|
73 | 84 |
*/ |
74 |
function connect() { |
|
85 |
public function connect() {
|
|
75 | 86 |
return $this->methodResponses['connect']; |
76 | 87 |
} |
77 | 88 |
|
78 |
|
|
79 |
function bind($userdn = NULL, $pass = NULL, $anon_bind = FALSE) { |
|
89 |
/** |
|
90 |
* |
|
91 |
*/ |
|
92 |
public function bind($userdn = NULL, $pass = NULL, $anon_bind = FALSE) { |
|
80 | 93 |
$userdn = ($userdn != NULL) ? $userdn : $this->binddn; |
81 | 94 |
$pass = ($pass != NULL) ? $pass : $this->bindpw; |
82 | 95 |
|
83 |
if (! isset($this->entries[$userdn])) { |
|
84 |
$ldap_errno = LDAP_NO_SUCH_OBJECT; // 0x20 or 32 |
|
96 |
if (!isset($this->entries[$userdn])) { |
|
97 |
// 0x20 or 32. |
|
98 |
$ldap_errno = LDAP_NO_SUCH_OBJECT; |
|
85 | 99 |
if (function_exists('ldap_err2str')) { |
86 | 100 |
$ldap_error = ldap_err2str($ldap_errno); |
87 | 101 |
} |
... | ... | |
96 | 110 |
if (!$pass) { |
97 | 111 |
debug("Simpletest failure for $userdn. No password submitted"); |
98 | 112 |
} |
99 |
if (! isset($this->entries[$userdn]['password'][0])) {
|
|
113 |
if (!isset($this->entries[$userdn]['password'][0])) { |
|
100 | 114 |
debug("Simpletest failure for $userdn. No password in entry to test for bind"); debug($this->entries[$userdn]); |
101 | 115 |
} |
102 | 116 |
$ldap_errno = LDAP_INVALID_CREDENTIALS; |
... | ... | |
108 | 122 |
} |
109 | 123 |
} |
110 | 124 |
|
111 |
$watchdog_tokens = array('%user' => $userdn, '%errno' => $ldap_errno, '%error' => $ldap_error);
|
|
125 |
$watchdog_tokens = ['%user' => $userdn, '%errno' => $ldap_errno, '%error' => $ldap_error];
|
|
112 | 126 |
watchdog('ldap_servers', "LDAP bind failure for user %user. Error %errno: %error", $watchdog_tokens); |
113 | 127 |
return $ldap_errno; |
114 | 128 |
|
... | ... | |
117 | 131 |
/** |
118 | 132 |
* Disconnect (unbind) from an active LDAP server. |
119 | 133 |
*/ |
120 |
function disconnect() { |
|
134 |
public function disconnect() {
|
|
121 | 135 |
|
122 | 136 |
} |
123 | 137 |
|
... | ... | |
125 | 139 |
* Perform an LDAP search. |
126 | 140 |
* |
127 | 141 |
* @param string $filter |
128 |
* The search filter. such as sAMAccountName=jbarclay |
|
142 |
* The search filter. such as sAMAccountName=jbarclay.
|
|
129 | 143 |
* @param string $basedn |
130 |
* The search base. If NULL, we use $this->basedn |
|
144 |
* The search base. If NULL, we use $this->basedn.
|
|
131 | 145 |
* @param array $attributes |
132 | 146 |
* List of desired attributes. If omitted, we only return "dn". |
133 | 147 |
* |
... | ... | |
135 | 149 |
* An array of matching entries->attributes, or FALSE if the search is |
136 | 150 |
* empty. |
137 | 151 |
*/ |
138 |
function search($base_dn = NULL, $filter, $attributes = array(), $attrsonly = 0, $sizelimit = 0, $timelimit = 0, $deref = LDAP_DEREF_NEVER, $scope = LDAP_SCOPE_SUBTREE) {
|
|
152 |
public function search($base_dn = NULL, $filter, $attributes = [], $attrsonly = 0, $sizelimit = 0, $timelimit = 0, $deref = LDAP_DEREF_NEVER, $scope = LDAP_SCOPE_SUBTREE) {
|
|
139 | 153 |
|
140 |
$lcase_attribute = array();
|
|
154 |
$lcase_attribute = [];
|
|
141 | 155 |
foreach ($attributes as $i => $attribute_name) { |
142 | 156 |
$lcase_attribute[] = drupal_strtolower($attribute_name); |
143 | 157 |
} |
144 | 158 |
$attributes = $lcase_attribute; |
145 | 159 |
|
146 |
$filter = trim(str_replace(array("\n", " "), array('', ''), $filter)); // for test matching simplicity remove line breaks and tab spacing |
|
160 |
// For test matching simplicity remove line breaks and tab spacing. |
|
161 |
$filter = trim(str_replace(["\n", " "], ['', ''], $filter)); |
|
147 | 162 |
|
148 | 163 |
if ($base_dn == NULL) { |
149 | 164 |
if (count($this->basedn) == 1) { |
... | ... | |
177 | 192 |
*/ |
178 | 193 |
$base_dn = drupal_strtolower($base_dn); |
179 | 194 |
$filter = trim($filter, "()"); |
180 |
$subqueries = array();
|
|
195 |
$subqueries = [];
|
|
181 | 196 |
$operand = FALSE; |
182 | 197 |
|
183 | 198 |
if (strpos($filter, '&') === 0) { |
184 |
/** |
|
199 |
/**
|
|
185 | 200 |
* case 2.A.: filter of form (&(<attribute>=<value>)(<attribute>=<value>)(<attribute>=<value>)) |
186 | 201 |
* such as (&(samaccountname=hpotter)(samaccountname=hpotter)(samaccountname=hpotter)) |
187 | 202 |
*/ |
... | ... | |
194 | 209 |
} |
195 | 210 |
} |
196 | 211 |
elseif (strpos($filter, '|') === 0) { |
197 |
/** |
|
212 |
/**
|
|
198 | 213 |
* case 2.B: filter of form (|(<attribute>=<value>)(<attribute>=<value>)(<attribute>=<value>)) |
199 | 214 |
* such as (|(samaccountname=hpotter)(samaccountname=hpotter)(samaccountname=hpotter)) |
200 | 215 |
*/ |
... | ... | |
208 | 223 |
} |
209 | 224 |
} |
210 | 225 |
elseif (count(explode('=', $filter)) == 2) { |
211 |
/** |
|
226 |
/**
|
|
212 | 227 |
* case 2.C.: filter of form (<attribute>=<value>) |
213 | 228 |
* such as (samaccountname=hpotter) |
214 | 229 |
*/ |
... | ... | |
219 | 234 |
return FALSE; |
220 | 235 |
} |
221 | 236 |
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
// need to perform feaux ldap search here with data in |
|
226 |
$results = array(); |
|
237 |
// Need to perform feaux ldap search here with data in. |
|
238 |
$results = []; |
|
227 | 239 |
|
228 | 240 |
if ($operand == '|') { |
229 | 241 |
foreach ($subqueries as $i => $subquery) { |
... | ... | |
232 | 244 |
foreach ($this->entries as $dn => $entry) { |
233 | 245 |
$dn_lcase = drupal_strtolower($dn); |
234 | 246 |
|
235 |
// if not in basedn, skip
|
|
247 |
// If not in basedn, skip
|
|
236 | 248 |
// eg. basedn ou=campus accounts,dc=ad,dc=myuniversity,dc=edu |
237 | 249 |
// should be leftmost string in: |
238 |
// cn=jdoe,ou=campus accounts,dc=ad,dc=myuniversity,dc=edu |
|
239 |
//$pos = strpos($dn_lcase, $base_dn); |
|
250 |
// cn=jdoe,ou=campus accounts,dc=ad,dc=myuniversity,dc=edu. |
|
240 | 251 |
$substring = strrev(substr(strrev($dn_lcase), 0, strlen($base_dn))); |
241 | 252 |
$cascmp = strcasecmp($base_dn, $substring); |
242 | 253 |
if ($cascmp !== 0) { |
243 | 254 |
|
244 |
continue; // not in basedn |
|
255 |
// Not in basedn. |
|
256 |
continue; |
|
245 | 257 |
} |
246 |
// if doesn't filter attribute has no data, continue
|
|
258 |
// If doesn't filter attribute has no data, continue.
|
|
247 | 259 |
$attr_value_to_compare = FALSE; |
248 | 260 |
foreach ($entry as $attr_name => $attr_value) { |
249 | 261 |
if (drupal_strtolower($attr_name) == $filter_attribute) { |
... | ... | |
258 | 270 |
// match! |
259 | 271 |
$entry['dn'] = $dn; |
260 | 272 |
if ($attributes) { |
261 |
$selected_data = array();
|
|
273 |
$selected_data = [];
|
|
262 | 274 |
foreach ($attributes as $i => $attr_name) { |
263 | 275 |
$selected_data[$attr_name] = (isset($entry[$attr_name])) ? $entry[$attr_name] : NULL; |
264 | 276 |
} |
... | ... | |
270 | 282 |
} |
271 | 283 |
} |
272 | 284 |
} |
273 |
elseif ($operand == '&') { // reverse the loops |
|
285 |
// Reverse the loops. |
|
286 |
elseif ($operand == '&') { |
|
274 | 287 |
foreach ($this->entries as $dn => $entry) { |
275 | 288 |
$dn_lcase = drupal_strtolower($dn); |
276 |
$match = TRUE; // until 1 subquery fails |
|
289 |
// Until 1 subquery fails. |
|
290 |
$match = TRUE; |
|
277 | 291 |
foreach ($subqueries as $i => $subquery) { |
278 | 292 |
$filter_attribute = drupal_strtolower($subquery[0]); |
279 | 293 |
$filter_value = $subquery[1]; |
... | ... | |
282 | 296 |
$cascmp = strcasecmp($base_dn, $substring); |
283 | 297 |
if ($cascmp !== 0) { |
284 | 298 |
$match = FALSE; |
285 |
break; // not in basedn |
|
299 |
// Not in basedn. |
|
300 |
break; |
|
286 | 301 |
} |
287 |
// if doesn't filter attribute has no data, continue
|
|
302 |
// If doesn't filter attribute has no data, continue.
|
|
288 | 303 |
$attr_value_to_compare = FALSE; |
289 | 304 |
foreach ($entry as $attr_name => $attr_value) { |
290 | 305 |
if (drupal_strtolower($attr_name) == $filter_attribute) { |
... | ... | |
294 | 309 |
} |
295 | 310 |
if (!$attr_value_to_compare || drupal_strtolower($attr_value_to_compare[0]) != $filter_value) { |
296 | 311 |
$match = FALSE; |
297 |
break; // not in basedn |
|
312 |
// Not in basedn. |
|
313 |
break; |
|
298 | 314 |
} |
299 | 315 |
|
300 | 316 |
} |
301 | 317 |
if ($match === TRUE) { |
302 | 318 |
$entry['dn'] = $dn; |
303 | 319 |
if ($attributes) { |
304 |
$selected_data = array();
|
|
320 |
$selected_data = [];
|
|
305 | 321 |
foreach ($attributes as $i => $attr_name) { |
306 | 322 |
$selected_data[$attr_name] = (isset($entry[$attr_name])) ? $entry[$attr_name] : NULL; |
307 | 323 |
} |
... | ... | |
318 | 334 |
return $results; |
319 | 335 |
} |
320 | 336 |
|
321 |
/** |
|
322 |
* does dn exist for this server? |
|
323 |
* |
|
324 |
* @param string $dn |
|
325 |
* @param enum $return = 'boolean' or 'ldap_entry' |
|
326 |
* |
|
327 |
* @param return FALSE or ldap entry array |
|
328 |
*/ |
|
329 |
function dnExists($find_dn, $return = 'boolean', $attributes = array('objectclass')) { |
|
337 |
/** |
|
338 |
* Does dn exist for this server? |
|
339 |
* |
|
340 |
* @param string $dn |
|
341 |
* @param enum $return |
|
342 |
* = 'boolean' or 'ldap_entry'. |
|
343 |
* |
|
344 |
* @param return FALSE or ldap entry array |
|
345 |
*/ |
|
346 |
public function dnExists($find_dn, $return = 'boolean', $attributes = ['objectclass']) { |
|
330 | 347 |
$this->refreshFakeData(); |
331 |
$test_data = variable_get('ldap_test_server__' . $this->sid, array());
|
|
348 |
$test_data = variable_get('ldap_test_server__' . $this->sid, []);
|
|
332 | 349 |
foreach ($this->entries as $entry_dn => $entry) { |
333 | 350 |
$match = (strcasecmp($entry_dn, $find_dn) == 0); |
334 | 351 |
|
... | ... | |
336 | 353 |
return ($return == 'boolean') ? TRUE : $entry; |
337 | 354 |
} |
338 | 355 |
} |
339 |
return FALSE; // not match found in loop |
|
356 |
// Not match found in loop. |
|
357 |
return FALSE; |
|
340 | 358 |
|
341 | 359 |
} |
342 | 360 |
|
361 |
/** |
|
362 |
* |
|
363 |
*/ |
|
343 | 364 |
public function countEntries($ldap_result) { |
344 | 365 |
return ldap_count_entries($this->connection, $ldap_result); |
345 | 366 |
} |
346 | 367 |
|
347 |
|
|
368 |
/** |
|
369 |
* |
|
370 |
*/ |
|
348 | 371 |
public static function getLdapServerObjects($sid = NULL, $type = NULL, $flatten = FALSE) { |
349 |
$servers = array();
|
|
372 |
$servers = [];
|
|
350 | 373 |
if ($sid) { |
351 | 374 |
$servers[$sid] = new LdapServerTest($sid); |
352 | 375 |
} |
353 | 376 |
else { |
354 |
$server_ids = variable_get('ldap_test_servers', array());
|
|
377 |
$server_ids = variable_get('ldap_test_servers', []);
|
|
355 | 378 |
foreach ($server_ids as $sid => $_sid) { |
356 | 379 |
$servers[$sid] = new LdapServerTest($sid); |
357 | 380 |
} |
... | ... | |
365 | 388 |
} |
366 | 389 |
} |
367 | 390 |
|
368 |
|
|
369 | 391 |
/** |
370 |
* create ldap entry.
|
|
392 |
* Create ldap entry.
|
|
371 | 393 |
* |
372 |
* @param array $ldap_entry should follow the structure of ldap_add functions |
|
394 |
* @param array $ldap_entry |
|
395 |
* should follow the structure of ldap_add functions |
|
373 | 396 |
* entry array: http://us.php.net/manual/en/function.ldap-add.php |
374 |
$attributes["attribute1"] = "value"; |
|
375 |
$attributes["attribute2"][0] = "value1"; |
|
376 |
$attributes["attribute2"][1] = "value2"; |
|
397 |
* $attributes["attribute1"] = "value"; |
|
398 |
* $attributes["attribute2"][0] = "value1"; |
|
399 |
* $attributes["attribute2"][1] = "value2";. |
|
400 |
* |
|
377 | 401 |
* @return boolean result |
378 | 402 |
*/ |
379 |
|
|
380 | 403 |
public function createLdapEntry($ldap_entry, $dn = NULL) { |
381 | 404 |
$result = FALSE; |
382 |
$test_data = variable_get('ldap_test_server__' . $this->sid, array());
|
|
405 |
$test_data = variable_get('ldap_test_server__' . $this->sid, []);
|
|
383 | 406 |
|
384 | 407 |
if (isset($ldap_entry['dn'])) { |
385 | 408 |
$dn = $ldap_entry['dn']; |
... | ... | |
396 | 419 |
return $result; |
397 | 420 |
} |
398 | 421 |
|
399 |
function modifyLdapEntry($dn, $attributes = NULL, $old_attributes = FALSE) { |
|
422 |
/** |
|
423 |
* |
|
424 |
*/ |
|
425 |
public function modifyLdapEntry($dn, $attributes = NULL, $old_attributes = FALSE) { |
|
400 | 426 |
if (!$attributes) { |
401 |
$attributes = array();
|
|
427 |
$attributes = [];
|
|
402 | 428 |
} |
403 |
$test_data = variable_get('ldap_test_server__' . $this->sid, array());
|
|
429 |
$test_data = variable_get('ldap_test_server__' . $this->sid, []);
|
|
404 | 430 |
if (!isset($test_data['entries'][$dn])) { |
405 | 431 |
return FALSE; |
406 | 432 |
} |
... | ... | |
441 | 467 |
|
442 | 468 |
} |
443 | 469 |
|
444 |
/**
|
|
470 |
/** |
|
445 | 471 |
* Perform an LDAP delete. |
446 | 472 |
* |
447 | 473 |
* @param string $dn |
448 | 474 |
* |
449 | 475 |
* @return boolean result per ldap_delete |
450 | 476 |
*/ |
451 |
|
|
452 | 477 |
public function delete($dn) { |
453 | 478 |
|
454 |
$test_data = variable_get('ldap_test_server__' . $this->sid, array());
|
|
479 |
$test_data = variable_get('ldap_test_server__' . $this->sid, []);
|
|
455 | 480 |
$deleted = FALSE; |
456 |
foreach (array('entries', 'users', 'groups', 'ldap') as $test_data_sub_array) {
|
|
481 |
foreach (['entries', 'users', 'groups', 'ldap'] as $test_data_sub_array) {
|
|
457 | 482 |
if (isset($test_data[$test_data_sub_array][$dn])) { |
458 | 483 |
unset($test_data[$test_data_sub_array][$dn]); |
459 | 484 |
$deleted = TRUE; |
Formats disponibles : Unified diff
Weekly update of contrib modules