1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
require_once('ldap_user.module');
|
10 |
|
|
|
11 |
|
|
class LdapUserConf {
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
public $drupalAcctProvisionServer = LDAP_USER_NO_SERVER_SID;
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
public $ldapEntryProvisionServer = LDAP_USER_NO_SERVER_SID;
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
public $provisionSidFromDirection = array(
|
37 |
|
|
LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER => LDAP_USER_NO_SERVER_SID,
|
38 |
|
|
LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY => LDAP_USER_NO_SERVER_SID,
|
39 |
|
|
);
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
public $drupalAcctProvisionTriggers = array(LDAP_USER_DRUPAL_USER_PROV_ON_AUTHENTICATE, LDAP_USER_DRUPAL_USER_PROV_ON_USER_UPDATE_CREATE, LDAP_USER_DRUPAL_USER_PROV_ON_ALLOW_MANUAL_CREATE);
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
public $ldapEntryProvisionTriggers = array();
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
public $userConflictResolve = LDAP_USER_CONFLICT_RESOLVE_DEFAULT;
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
public $acctCreation = LDAP_USER_ACCT_CREATION_LDAP_BEHAVIOR_DEFAULT;
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
public $inDatabase = FALSE;
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
public $manualAccountConflict = LDAP_USER_MANUAL_ACCT_CONFLICT_REJECT;
|
98 |
|
|
|
99 |
|
|
public $setsLdapPassword = TRUE;
|
100 |
|
|
|
101 |
|
|
public $loginConflictResolve = FALSE;
|
102 |
|
|
|
103 |
|
|
public $disableAdminPasswordField = FALSE;
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
public $synchMapping = NULL;
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
public $ldapUserSynchMappings = NULL;
|
150 |
|
|
|
151 |
|
|
public $detailedWatchdog = FALSE;
|
152 |
|
|
public $provisionsDrupalAccountsFromLdap = FALSE;
|
153 |
|
|
public $provisionsLdapEntriesFromDrupalUsers = FALSE;
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
public $orphanedDrupalAcctBehavior = 'ldap_user_orphan_email';
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
public $orphanedCheckQty = 100;
|
168 |
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
|
|
public $provisionsLdapEvents = array();
|
174 |
|
|
public $provisionsDrupalEvents = array();
|
175 |
|
|
|
176 |
|
|
public $saveable = array(
|
177 |
|
|
'drupalAcctProvisionServer',
|
178 |
|
|
'ldapEntryProvisionServer',
|
179 |
|
|
'drupalAcctProvisionTriggers',
|
180 |
|
|
'ldapEntryProvisionTriggers',
|
181 |
|
|
'orphanedDrupalAcctBehavior',
|
182 |
|
|
'orphanedCheckQty',
|
183 |
|
|
'userConflictResolve',
|
184 |
|
|
'manualAccountConflict',
|
185 |
|
|
'acctCreation',
|
186 |
|
|
'ldapUserSynchMappings',
|
187 |
|
|
'disableAdminPasswordField',
|
188 |
|
|
);
|
189 |
|
|
|
190 |
|
|
function __construct() {
|
191 |
|
|
$this->load();
|
192 |
|
|
|
193 |
|
|
$this->provisionSidFromDirection[LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER] = $this->drupalAcctProvisionServer;
|
194 |
|
|
$this->provisionSidFromDirection[LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY] = $this->ldapEntryProvisionServer;
|
195 |
|
|
|
196 |
|
|
$this->provisionsLdapEvents = array(
|
197 |
|
|
LDAP_USER_EVENT_CREATE_LDAP_ENTRY => t('On LDAP Entry Creation'),
|
198 |
|
|
LDAP_USER_EVENT_SYNCH_TO_LDAP_ENTRY => t('On Synch to LDAP Entry'),
|
199 |
|
|
);
|
200 |
|
|
|
201 |
|
|
$this->provisionsDrupalEvents = array(
|
202 |
|
|
LDAP_USER_EVENT_CREATE_DRUPAL_USER => t('On Drupal User Creation'),
|
203 |
|
|
LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER => t('On Synch to Drupal User'),
|
204 |
|
|
);
|
205 |
|
|
|
206 |
|
|
$this->provisionsDrupalAccountsFromLdap = (
|
207 |
|
|
$this->drupalAcctProvisionServer &&
|
208 |
|
|
$this->drupalAcctProvisionServer &&
|
209 |
|
|
(count(array_filter(array_values($this->drupalAcctProvisionTriggers))) > 0)
|
210 |
|
|
);
|
211 |
|
|
|
212 |
|
|
$this->provisionsLdapEntriesFromDrupalUsers = (
|
213 |
|
|
$this->ldapEntryProvisionServer
|
214 |
|
|
&& $this->ldapEntryProvisionServer
|
215 |
|
|
&& (count(array_filter(array_values($this->ldapEntryProvisionTriggers))) > 0)
|
216 |
|
|
);
|
217 |
|
|
|
218 |
|
|
$this->setSynchMapping(TRUE);
|
219 |
|
|
$this->detailedWatchdog = variable_get('ldap_help_watchdog_detail', 0);
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
function load() {
|
223 |
|
|
|
224 |
|
|
if ($saved = variable_get("ldap_user_conf", FALSE)) {
|
225 |
|
|
$this->inDatabase = TRUE;
|
226 |
|
|
foreach ($this->saveable as $property) {
|
227 |
|
|
if (isset($saved[$property])) {
|
228 |
|
|
$this->{$property} = $saved[$property];
|
229 |
|
|
}
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
|
|
else {
|
233 |
|
|
$this->inDatabase = FALSE;
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
$user_register = variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL);
|
237 |
|
|
if ($this->acctCreation == LDAP_USER_ACCT_CREATION_LDAP_BEHAVIOR_DEFAULT || $user_register == USER_REGISTER_VISITORS) {
|
238 |
|
|
$this->createLDAPAccounts = TRUE;
|
239 |
|
|
$this->createLDAPAccountsAdminApproval = FALSE;
|
240 |
|
|
}
|
241 |
|
|
elseif ($user_register == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) {
|
242 |
|
|
$this->createLDAPAccounts = FALSE;
|
243 |
|
|
$this->createLDAPAccountsAdminApproval = TRUE;
|
244 |
|
|
}
|
245 |
|
|
else {
|
246 |
|
|
$this->createLDAPAccounts = FALSE;
|
247 |
|
|
$this->createLDAPAccountsAdminApproval = FALSE;
|
248 |
|
|
}
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
function __destruct() { }
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
public function getSynchMappings($direction = LDAP_USER_PROV_DIRECTION_ALL, $prov_events = NULL) {
|
269 |
|
|
if (!$prov_events) {
|
270 |
|
|
$prov_events = ldap_user_all_events();
|
271 |
|
|
}
|
272 |
|
|
|
273 |
|
|
$mappings = array();
|
274 |
|
|
if ($direction == LDAP_USER_PROV_DIRECTION_ALL) {
|
275 |
|
|
$directions = array(LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY);
|
276 |
|
|
}
|
277 |
|
|
else {
|
278 |
|
|
$directions = array($direction);
|
279 |
|
|
}
|
280 |
|
|
foreach ($directions as $direction) {
|
281 |
|
|
if (!empty($this->ldapUserSynchMappings[$direction])) {
|
282 |
|
|
foreach ($this->ldapUserSynchMappings[$direction] as $attribute => $mapping) {
|
283 |
|
|
if (!empty($mapping['prov_events'])) {
|
284 |
|
|
$result = count(array_intersect($prov_events, $mapping['prov_events']));
|
285 |
|
|
if ($result) {
|
286 |
|
|
$mappings[$attribute] = $mapping;
|
287 |
|
|
}
|
288 |
|
|
}
|
289 |
|
|
}
|
290 |
|
|
}
|
291 |
|
|
}
|
292 |
|
|
return $mappings;
|
293 |
|
|
}
|
294 |
|
|
|
295 |
|
|
public function isDrupalAcctProvisionServer($sid) {
|
296 |
|
|
if (!$sid || !$this->drupalAcctProvisionServer) {
|
297 |
|
|
return FALSE;
|
298 |
|
|
}
|
299 |
|
|
elseif ($this->ldapEntryProvisionServer == $sid) {
|
300 |
|
|
return TRUE;
|
301 |
|
|
}
|
302 |
|
|
else {
|
303 |
|
|
return FALSE;
|
304 |
|
|
}
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
public function isLdapEntryProvisionServer($sid) {
|
308 |
|
|
if (!$sid || !$this->ldapEntryProvisionServer) {
|
309 |
|
|
return FALSE;
|
310 |
|
|
}
|
311 |
|
|
elseif ($this->ldapEntryProvisionServer == $sid) {
|
312 |
|
|
return TRUE;
|
313 |
|
|
}
|
314 |
|
|
else {
|
315 |
|
|
return FALSE;
|
316 |
|
|
}
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
|
|
|
325 |
|
|
|
326 |
|
|
public function getLdapUserRequiredAttributes($direction = LDAP_USER_PROV_DIRECTION_ALL, $ldap_context = NULL) {
|
327 |
|
|
|
328 |
|
|
$attributes_map = array();
|
329 |
|
|
$required_attributes = array();
|
330 |
|
|
if ($this->drupalAcctProvisionServer) {
|
331 |
|
|
$prov_events = $this->ldapContextToProvEvents($ldap_context);
|
332 |
|
|
$attributes_map = $this->getSynchMappings($direction, $prov_events);
|
333 |
|
|
$required_attributes = array();
|
334 |
|
|
foreach ($attributes_map as $detail) {
|
335 |
|
|
if (count(array_intersect($prov_events, $detail['prov_events']))) {
|
336 |
|
|
|
337 |
|
|
if ($detail['ldap_attr']) {
|
338 |
|
|
ldap_servers_token_extract_attributes($required_attributes, $detail['ldap_attr']);
|
339 |
|
|
}
|
340 |
|
|
}
|
341 |
|
|
}
|
342 |
|
|
}
|
343 |
|
|
return $required_attributes;
|
344 |
|
|
}
|
345 |
|
|
|
346 |
|
|
|
347 |
|
|
|
348 |
|
|
|
349 |
|
|
|
350 |
|
|
public function ldapContextToProvEvents($ldap_context = NULL) {
|
351 |
|
|
|
352 |
|
|
switch ($ldap_context) {
|
353 |
|
|
|
354 |
|
|
case 'ldap_user_prov_to_drupal':
|
355 |
|
|
$result = array(LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER, LDAP_USER_EVENT_CREATE_DRUPAL_USER, LDAP_USER_EVENT_LDAP_ASSOCIATE_DRUPAL_ACCT);
|
356 |
|
|
break;
|
357 |
|
|
|
358 |
|
|
case 'ldap_user_prov_to_ldap':
|
359 |
|
|
$result = array(LDAP_USER_EVENT_SYNCH_TO_LDAP_ENTRY, LDAP_USER_EVENT_CREATE_LDAP_ENTRY);
|
360 |
|
|
break;
|
361 |
|
|
|
362 |
|
|
default:
|
363 |
|
|
$result = ldap_user_all_events();
|
364 |
|
|
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
return $result;
|
368 |
|
|
|
369 |
|
|
}
|
370 |
|
|
|
371 |
|
|
|
372 |
|
|
|
373 |
|
|
|
374 |
|
|
|
375 |
|
|
|
376 |
|
|
public function ldapContextToProvDirection($ldap_context = NULL) {
|
377 |
|
|
|
378 |
|
|
switch ($ldap_context) {
|
379 |
|
|
|
380 |
|
|
case 'ldap_user_prov_to_drupal':
|
381 |
|
|
$result = LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER;
|
382 |
|
|
break;
|
383 |
|
|
|
384 |
|
|
case 'ldap_user_prov_to_ldap':
|
385 |
|
|
case 'ldap_user_delete_drupal_user':
|
386 |
|
|
$result = LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY;
|
387 |
|
|
break;
|
388 |
|
|
|
389 |
|
|
|
390 |
|
|
case 'ldap_user_insert_drupal_user':
|
391 |
|
|
case 'ldap_user_update_drupal_user':
|
392 |
|
|
case 'ldap_authentication_authenticate':
|
393 |
|
|
case 'ldap_user_insert_drupal_user':
|
394 |
|
|
case 'ldap_user_disable_drupal_user':
|
395 |
|
|
$result = LDAP_USER_PROV_DIRECTION_ALL;
|
396 |
|
|
break;
|
397 |
|
|
|
398 |
|
|
default:
|
399 |
|
|
$result = LDAP_USER_PROV_DIRECTION_ALL;
|
400 |
|
|
|
401 |
|
|
}
|
402 |
|
|
|
403 |
|
|
return $result;
|
404 |
|
|
}
|
405 |
|
|
|
406 |
|
|
|
407 |
|
|
|
408 |
|
|
|
409 |
|
|
|
410 |
|
|
|
411 |
|
|
|
412 |
|
|
|
413 |
|
|
|
414 |
|
|
|
415 |
|
|
|
416 |
|
|
function setSynchMapping($reset = TRUE) {
|
417 |
|
|
$synch_mapping_cache = cache_get('ldap_user_synch_mapping');
|
418 |
|
|
if (!$reset && $synch_mapping_cache) {
|
419 |
|
|
$this->synchMapping = $synch_mapping_cache->data;
|
420 |
|
|
}
|
421 |
|
|
else {
|
422 |
|
|
$available_user_attrs = array();
|
423 |
|
|
foreach (array(LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY) as $direction) {
|
424 |
|
|
$sid = $this->provisionSidFromDirection[$direction];
|
425 |
|
|
$available_user_attrs[$direction] = array();
|
426 |
|
|
$ldap_server = ($sid) ? ldap_servers_get_servers($sid, NULL, TRUE) : FALSE;
|
427 |
|
|
|
428 |
|
|
$params = array(
|
429 |
|
|
'ldap_server' => $ldap_server,
|
430 |
|
|
'ldap_user_conf' => $this,
|
431 |
|
|
'direction' => $direction,
|
432 |
|
|
);
|
433 |
|
|
|
434 |
|
|
drupal_alter('ldap_user_attrs_list', $available_user_attrs[$direction], $params);
|
435 |
|
|
}
|
436 |
|
|
}
|
437 |
|
|
$this->synchMapping = $available_user_attrs;
|
438 |
|
|
|
439 |
|
|
cache_set('ldap_user_synch_mapping', $this->synchMapping);
|
440 |
|
|
}
|
441 |
|
|
|
442 |
|
|
|
443 |
|
|
|
444 |
|
|
|
445 |
|
|
|
446 |
|
|
|
447 |
|
|
|
448 |
|
|
|
449 |
|
|
|
450 |
|
|
|
451 |
|
|
|
452 |
|
|
|
453 |
|
|
|
454 |
|
|
|
455 |
|
|
|
456 |
|
|
|
457 |
|
|
|
458 |
|
|
public function provisionEnabled($direction, $provision_trigger) {
|
459 |
|
|
$result = FALSE;
|
460 |
|
|
|
461 |
|
|
if ($direction == LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY) {
|
462 |
|
|
|
463 |
|
|
if (!$this->ldapEntryProvisionServer) {
|
464 |
|
|
$result = FALSE;
|
465 |
|
|
}
|
466 |
|
|
else {
|
467 |
|
|
$result = in_array($provision_trigger, $this->ldapEntryProvisionTriggers);
|
468 |
|
|
}
|
469 |
|
|
|
470 |
|
|
}
|
471 |
|
|
elseif ($direction == LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER) {
|
472 |
|
|
if (!$this->drupalAcctProvisionServer) {
|
473 |
|
|
$result = FALSE;
|
474 |
|
|
}
|
475 |
|
|
else {
|
476 |
|
|
$result = in_array($provision_trigger, $this->drupalAcctProvisionTriggers);
|
477 |
|
|
}
|
478 |
|
|
}
|
479 |
|
|
|
480 |
|
|
return $result;
|
481 |
|
|
}
|
482 |
|
|
|
483 |
|
|
|
484 |
|
|
|
485 |
|
|
|
486 |
|
|
|
487 |
|
|
|
488 |
|
|
|
489 |
|
|
|
490 |
|
|
|
491 |
|
|
|
492 |
|
|
|
493 |
|
|
|
494 |
|
|
|
495 |
|
|
|
496 |
|
|
|
497 |
|
|
|
498 |
|
|
public function provisionLdapEntry($account, $ldap_user = NULL, $test_query = FALSE) {
|
499 |
|
|
|
500 |
|
|
$watchdog_tokens = array();
|
501 |
|
|
$result = array(
|
502 |
|
|
'status' => NULL,
|
503 |
|
|
'ldap_server' => NULL,
|
504 |
|
|
'proposed' => NULL,
|
505 |
|
|
'existing' => NULL,
|
506 |
|
|
'description' => NULL,
|
507 |
|
|
);
|
508 |
|
|
|
509 |
|
|
if (is_scalar($account)) {
|
510 |
|
|
$username = $account;
|
511 |
|
|
$account = new stdClass();
|
512 |
|
|
$acount->name = $username;
|
513 |
|
|
}
|
514 |
|
|
|
515 |
|
|
list($account, $user_entity) = ldap_user_load_user_acct_and_entity($account->name);
|
516 |
|
|
|
517 |
|
|
if (is_object($account) && property_exists($account, 'uid') && $account->uid == 1) {
|
518 |
|
|
$result['status'] = 'fail';
|
519 |
|
|
$result['error_description'] = 'can not provision drupal user 1';
|
520 |
|
|
return $result;
|
521 |
|
|
}
|
522 |
|
|
|
523 |
|
|
if ($account == FALSE || $account->uid == 0) {
|
524 |
|
|
$result['status'] = 'fail';
|
525 |
|
|
$result['error_description'] = 'can not provision ldap user unless corresponding drupal account exists first.';
|
526 |
|
|
return $result;
|
527 |
|
|
}
|
528 |
|
|
|
529 |
|
|
if (!$this->ldapEntryProvisionServer || !$this->ldapEntryProvisionServer) {
|
530 |
|
|
$result['status'] = 'fail';
|
531 |
|
|
$result['error_description'] = 'no provisioning server enabled';
|
532 |
|
|
return $result;
|
533 |
|
|
}
|
534 |
|
|
|
535 |
|
|
$ldap_server = ldap_servers_get_servers($this->ldapEntryProvisionServer, NULL, TRUE);
|
536 |
|
|
$params = array(
|
537 |
|
|
'direction' => LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY,
|
538 |
|
|
'prov_events' => array(LDAP_USER_EVENT_CREATE_LDAP_ENTRY),
|
539 |
|
|
'module' => 'ldap_user',
|
540 |
|
|
'function' => 'provisionLdapEntry',
|
541 |
|
|
'include_count' => FALSE,
|
542 |
|
|
);
|
543 |
|
|
|
544 |
|
|
list($proposed_ldap_entry, $error) = $this->drupalUserToLdapEntry($account, $ldap_server, $params, $ldap_user);
|
545 |
|
|
$proposed_dn = (is_array($proposed_ldap_entry) && isset($proposed_ldap_entry['dn']) && $proposed_ldap_entry['dn']) ? $proposed_ldap_entry['dn'] : NULL;
|
546 |
|
|
$proposed_dn_lcase = drupal_strtolower($proposed_dn);
|
547 |
|
|
$existing_ldap_entry = ($proposed_dn) ? $ldap_server->dnExists($proposed_dn, 'ldap_entry') : NULL;
|
548 |
|
|
|
549 |
|
|
if ($error == LDAP_USER_PROV_RESULT_NO_PWD) {
|
550 |
|
|
$result['status'] = 'fail';
|
551 |
|
|
$result['description'] = 'Can not provision ldap account without user provided password.';
|
552 |
|
|
$result['existing'] = $existing_ldap_entry;
|
553 |
|
|
$result['proposed'] = $proposed_ldap_entry;
|
554 |
|
|
$result['ldap_server'] = $ldap_server;
|
555 |
|
|
}
|
556 |
|
|
elseif (!$proposed_dn) {
|
557 |
|
|
$result['status'] = 'fail';
|
558 |
|
|
$result['description'] = t('failed to derive dn and or mappings');
|
559 |
|
|
return $result;
|
560 |
|
|
}
|
561 |
|
|
elseif ($existing_ldap_entry) {
|
562 |
|
|
$result['status'] = 'conflict';
|
563 |
|
|
$result['description'] = 'can not provision ldap entry because exists already';
|
564 |
|
|
$result['existing'] = $existing_ldap_entry;
|
565 |
|
|
$result['proposed'] = $proposed_ldap_entry;
|
566 |
|
|
$result['ldap_server'] = $ldap_server;
|
567 |
|
|
}
|
568 |
|
|
elseif ($test_query) {
|
569 |
|
|
$result['status'] = 'fail';
|
570 |
|
|
$result['description'] = 'not created because flagged as test query';
|
571 |
|
|
$result['proposed'] = $proposed_ldap_entry;
|
572 |
|
|
$result['ldap_server'] = $ldap_server;
|
573 |
|
|
}
|
574 |
|
|
else {
|
575 |
|
|
|
576 |
|
|
$ldap_entries = array($proposed_dn_lcase => $proposed_ldap_entry);
|
577 |
|
|
$context = array(
|
578 |
|
|
'action' => 'add',
|
579 |
|
|
'corresponding_drupal_data' => array($proposed_dn_lcase => $account),
|
580 |
|
|
'corresponding_drupal_data_type' => 'user',
|
581 |
|
|
);
|
582 |
|
|
drupal_alter('ldap_entry_pre_provision', $ldap_entries, $ldap_server, $context);
|
583 |
|
|
|
584 |
|
|
$proposed_ldap_entry = $ldap_entries[$proposed_dn_lcase];
|
585 |
|
|
|
586 |
|
|
$ldap_entry_created = $ldap_server->createLdapEntry($proposed_ldap_entry, $proposed_dn);
|
587 |
|
|
if ($ldap_entry_created) {
|
588 |
|
|
module_invoke_all('ldap_entry_post_provision', $ldap_entries, $ldap_server, $context);
|
589 |
|
|
$result['status'] = 'success';
|
590 |
|
|
$result['description'] = 'ldap account created';
|
591 |
|
|
$result['proposed'] = $proposed_ldap_entry;
|
592 |
|
|
$result['created'] = $ldap_entry_created;
|
593 |
|
|
$result['ldap_server'] = $ldap_server;
|
594 |
|
|
|
595 |
|
|
|
596 |
|
|
$ldap_user_prov_entry = $ldap_server->sid . '|' . $proposed_ldap_entry['dn'];
|
597 |
|
|
if (!isset($user_entity->ldap_user_prov_entries['und'])) {
|
598 |
|
|
$user_entity->ldap_user_prov_entries = array('und' => array());
|
599 |
|
|
}
|
600 |
|
|
$ldap_user_prov_entry_exists = FALSE;
|
601 |
|
|
foreach ($user_entity->ldap_user_prov_entries['und'] as $i => $field_value_instance) {
|
602 |
|
|
if ($field_value_instance == $ldap_user_prov_entry) {
|
603 |
|
|
$ldap_user_prov_entry_exists = TRUE;
|
604 |
|
|
}
|
605 |
|
|
}
|
606 |
|
|
if (!$ldap_user_prov_entry_exists) {
|
607 |
|
|
$user_entity->ldap_user_prov_entries['und'][] = array(
|
608 |
|
|
'value' => $ldap_user_prov_entry,
|
609 |
|
|
'format' => NULL,
|
610 |
|
|
'save_value' => $ldap_user_prov_entry,
|
611 |
|
|
);
|
612 |
|
|
$edit = array(
|
613 |
|
|
'ldap_user_prov_entries' => $user_entity->ldap_user_prov_entries,
|
614 |
|
|
);
|
615 |
|
|
$account = user_load($account->uid);
|
616 |
|
|
$account = user_save($account, $edit);
|
617 |
|
|
}
|
618 |
|
|
|
619 |
|
|
}
|
620 |
|
|
else {
|
621 |
|
|
$result['status'] = 'fail';
|
622 |
|
|
$result['proposed'] = $proposed_ldap_entry;
|
623 |
|
|
$result['created'] = $ldap_entry_created;
|
624 |
|
|
$result['ldap_server'] = $ldap_server;
|
625 |
|
|
$result['existing'] = NULL;
|
626 |
|
|
}
|
627 |
|
|
}
|
628 |
|
|
|
629 |
|
|
$tokens = array(
|
630 |
|
|
'%dn' => isset($result['proposed']['dn']) ? $result['proposed']['dn'] : NULL,
|
631 |
|
|
'%sid' => (isset($result['ldap_server']) && $result['ldap_server']) ? $result['ldap_server']->sid : 0,
|
632 |
|
|
'%username' => @$account->name,
|
633 |
|
|
'%uid' => @$account->uid,
|
634 |
|
|
'%description' => @$result['description'],
|
635 |
|
|
);
|
636 |
|
|
if (!$test_query && isset($result['status'])) {
|
637 |
|
|
if ($result['status'] == 'success') {
|
638 |
|
|
if ($this->detailedWatchdog) {
|
639 |
|
|
watchdog('ldap_user', 'LDAP entry on server %sid created dn=%dn. %description. username=%username, uid=%uid', $tokens, WATCHDOG_INFO);
|
640 |
|
|
}
|
641 |
|
|
}
|
642 |
|
|
elseif ($result['status'] == 'conflict') {
|
643 |
|
|
if ($this->detailedWatchdog) {
|
644 |
|
|
watchdog('ldap_user', 'LDAP entry on server %sid not created because of existing ldap entry. %description. username=%username, uid=%uid', $tokens, WATCHDOG_WARNING);
|
645 |
|
|
}
|
646 |
|
|
}
|
647 |
|
|
elseif ($result['status'] == 'fail') {
|
648 |
|
|
watchdog('ldap_user', 'LDAP entry on server %sid not created because error. %description. username=%username, uid=%uid', $tokens, WATCHDOG_ERROR);
|
649 |
|
|
}
|
650 |
|
|
}
|
651 |
|
|
return $result;
|
652 |
|
|
}
|
653 |
|
|
|
654 |
|
|
|
655 |
|
|
|
656 |
|
|
|
657 |
|
|
|
658 |
|
|
|
659 |
|
|
|
660 |
|
|
|
661 |
|
|
|
662 |
|
|
|
663 |
|
|
|
664 |
|
|
|
665 |
|
|
public function synchToLdapEntry($account, $user_edit = NULL, $ldap_user = array(), $test_query = FALSE) {
|
666 |
|
|
|
667 |
|
|
if (is_object($account) && property_exists($account, 'uid') && $account->uid == 1) {
|
668 |
|
|
return FALSE;
|
669 |
|
|
}
|
670 |
|
|
|
671 |
|
|
$watchdog_tokens = array();
|
672 |
|
|
$result = FALSE;
|
673 |
|
|
$proposed_ldap_entry = FALSE;
|
674 |
|
|
|
675 |
|
|
if ($this->ldapEntryProvisionServer) {
|
676 |
|
|
$ldap_server = ldap_servers_get_servers($this->ldapEntryProvisionServer, NULL, TRUE);
|
677 |
|
|
|
678 |
|
|
$params = array(
|
679 |
|
|
'direction' => LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY,
|
680 |
|
|
'prov_events' => array(LDAP_USER_EVENT_SYNCH_TO_LDAP_ENTRY),
|
681 |
|
|
'module' => 'ldap_user',
|
682 |
|
|
'function' => 'synchToLdapEntry',
|
683 |
|
|
'include_count' => FALSE,
|
684 |
|
|
);
|
685 |
|
|
|
686 |
|
|
list($proposed_ldap_entry, $error) = $this->drupalUserToLdapEntry($account, $ldap_server, $params, $ldap_user);
|
687 |
|
|
if ($error != LDAP_USER_PROV_RESULT_NO_ERROR) {
|
688 |
|
|
$result = FALSE;
|
689 |
|
|
}
|
690 |
|
|
elseif (is_array($proposed_ldap_entry) && isset($proposed_ldap_entry['dn'])) {
|
691 |
|
|
$existing_ldap_entry = $ldap_server->dnExists($proposed_ldap_entry['dn'], 'ldap_entry');
|
692 |
|
|
$attributes = array();
|
693 |
|
|
foreach ($proposed_ldap_entry as $attr_name => $attr_values) {
|
694 |
|
|
if ($attr_name != 'dn') {
|
695 |
|
|
if (isset($attr_values['count'])) {
|
696 |
|
|
unset($attr_values['count']);
|
697 |
|
|
}
|
698 |
|
|
if (count($attr_values) == 1) {
|
699 |
|
|
$attributes[$attr_name] = $attr_values[0];
|
700 |
|
|
}
|
701 |
|
|
else {
|
702 |
|
|
$attributes[$attr_name] = $attr_values;
|
703 |
|
|
}
|
704 |
|
|
}
|
705 |
|
|
}
|
706 |
|
|
|
707 |
|
|
if ($test_query) {
|
708 |
|
|
$proposed_ldap_entry = $attributes;
|
709 |
|
|
$result = array(
|
710 |
|
|
'proposed' => $proposed_ldap_entry,
|
711 |
|
|
'server' => $ldap_server,
|
712 |
|
|
);
|
713 |
|
|
}
|
714 |
|
|
else {
|
715 |
|
|
|
716 |
|
|
|
717 |
|
|
$proposed_dn_lcase = drupal_strtolower($proposed_ldap_entry['dn']);
|
718 |
|
|
$ldap_entries = array($proposed_dn_lcase => $attributes);
|
719 |
|
|
$context = array(
|
720 |
|
|
'action' => 'update',
|
721 |
|
|
'corresponding_drupal_data' => array($proposed_dn_lcase => $attributes),
|
722 |
|
|
'corresponding_drupal_data_type' => 'user',
|
723 |
|
|
);
|
724 |
|
|
drupal_alter('ldap_entry_pre_provision', $ldap_entries, $ldap_server, $context);
|
725 |
|
|
|
726 |
|
|
$attributes = $ldap_entries[$proposed_dn_lcase];
|
727 |
|
|
$result = $ldap_server->modifyLdapEntry($proposed_ldap_entry['dn'], $attributes);
|
728 |
|
|
if ($result) {
|
729 |
|
|
module_invoke_all('ldap_entry_post_provision', $ldap_entries, $ldap_server, $context);
|
730 |
|
|
}
|
731 |
|
|
}
|
732 |
|
|
}
|
733 |
|
|
else {
|
734 |
|
|
$result = FALSE;
|
735 |
|
|
}
|
736 |
|
|
}
|
737 |
|
|
|
738 |
|
|
$tokens = array(
|
739 |
|
|
'%dn' => isset($proposed_ldap_entry['dn']) ? $proposed_ldap_entry['dn'] : NULL,
|
740 |
|
|
'%sid' => $this->ldapEntryProvisionServer,
|
741 |
|
|
'%username' => $account->name,
|
742 |
|
|
'%uid' => ($test_query || !property_exists($account, 'uid')) ? '' : $account->uid,
|
743 |
|
|
);
|
744 |
|
|
|
745 |
|
|
if ($result) {
|
746 |
|
|
watchdog('ldap_user', 'LDAP entry on server %sid synched dn=%dn. username=%username, uid=%uid', $tokens, WATCHDOG_INFO);
|
747 |
|
|
}
|
748 |
|
|
else {
|
749 |
|
|
watchdog('ldap_user', 'LDAP entry on server %sid not synched because error. username=%username, uid=%uid', $tokens, WATCHDOG_ERROR);
|
750 |
|
|
}
|
751 |
|
|
|
752 |
|
|
return $result;
|
753 |
|
|
|
754 |
|
|
}
|
755 |
|
|
|
756 |
|
|
|
757 |
|
|
|
758 |
|
|
|
759 |
|
|
|
760 |
|
|
|
761 |
|
|
|
762 |
|
|
|
763 |
|
|
|
764 |
|
|
|
765 |
|
|
|
766 |
|
|
|
767 |
|
|
|
768 |
|
|
|
769 |
|
|
public function synchToDrupalAccount($drupal_user, &$user_edit, $prov_event = LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER, $ldap_user = NULL, $save = FALSE) {
|
770 |
dd54aff9
|
Assos Assos
|
|
771 |
85ad3d82
|
Assos Assos
|
$debug = array(
|
772 |
|
|
'account' => $drupal_user,
|
773 |
|
|
'user_edit' => $user_edit,
|
774 |
|
|
'ldap_user' => $ldap_user,
|
775 |
|
|
);
|
776 |
|
|
|
777 |
|
|
if (
|
778 |
|
|
(!$ldap_user && !isset($drupal_user->name)) ||
|
779 |
|
|
(!$drupal_user && $save) ||
|
780 |
|
|
($ldap_user && !isset($ldap_user['sid']))
|
781 |
|
|
) {
|
782 |
|
|
|
783 |
|
|
return FALSE;
|
784 |
|
|
}
|
785 |
|
|
|
786 |
|
|
if (!$ldap_user && $this->drupalAcctProvisionServer) {
|
787 |
|
|
$ldap_user = ldap_servers_get_user_ldap_data($drupal_user->name, $this->drupalAcctProvisionServer, 'ldap_user_prov_to_drupal');
|
788 |
|
|
}
|
789 |
|
|
|
790 |
|
|
if (!$ldap_user) {
|
791 |
|
|
return FALSE;
|
792 |
|
|
}
|
793 |
|
|
|
794 |
|
|
if ($this->drupalAcctProvisionServer) {
|
795 |
|
|
$ldap_server = ldap_servers_get_servers($this->drupalAcctProvisionServer, NULL, TRUE);
|
796 |
|
|
$this->entryToUserEdit($ldap_user, $user_edit, $ldap_server, LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, array($prov_event));
|
797 |
|
|
}
|
798 |
|
|
|
799 |
|
|
if ($save) {
|
800 |
|
|
$account = user_load($drupal_user->uid);
|
801 |
|
|
$result = user_save($account, $user_edit, 'ldap_user');
|
802 |
|
|
return $result;
|
803 |
|
|
}
|
804 |
|
|
else {
|
805 |
|
|
return TRUE;
|
806 |
|
|
}
|
807 |
|
|
}
|
808 |
|
|
|
809 |
|
|
|
810 |
|
|
|
811 |
|
|
|
812 |
|
|
|
813 |
|
|
|
814 |
|
|
|
815 |
|
|
|
816 |
|
|
public function deleteDrupalAccount($username) {
|
817 |
|
|
$user = user_load_by_name($username);
|
818 |
|
|
if (is_object($user)) {
|
819 |
|
|
user_delete($user->uid);
|
820 |
|
|
return TRUE;
|
821 |
|
|
}
|
822 |
|
|
else {
|
823 |
|
|
return FALSE;
|
824 |
|
|
}
|
825 |
|
|
}
|
826 |
|
|
|
827 |
|
|
|
828 |
|
|
|
829 |
|
|
|
830 |
|
|
|
831 |
|
|
|
832 |
|
|
|
833 |
|
|
|
834 |
|
|
public function getProvisionRelatedLdapEntry($account, $prov_events = NULL) {
|
835 |
|
|
if (!$prov_events) {
|
836 |
|
|
$prov_events = ldap_user_all_events();
|
837 |
|
|
}
|
838 |
|
|
$sid = $this->ldapEntryProvisionServer;
|
839 |
|
|
|
840 |
|
|
if (!$sid) {
|
841 |
|
|
return FALSE;
|
842 |
|
|
}
|
843 |
|
|
|
844 |
|
|
$ldap_server = ldap_servers_get_servers($sid, NULL, TRUE);
|
845 |
|
|
$params = array(
|
846 |
|
|
'direction' => LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY,
|
847 |
|
|
'prov_events' => $prov_events,
|
848 |
|
|
'module' => 'ldap_user',
|
849 |
|
|
'function' => 'getProvisionRelatedLdapEntry',
|
850 |
|
|
'include_count' => FALSE,
|
851 |
|
|
);
|
852 |
|
|
list($proposed_ldap_entry, $error) = $this->drupalUserToLdapEntry($account, $ldap_server, $params);
|
853 |
|
|
if (!(is_array($proposed_ldap_entry) && isset($proposed_ldap_entry['dn']) && $proposed_ldap_entry['dn'])) {
|
854 |
|
|
return FALSE;
|
855 |
|
|
}
|
856 |
|
|
$ldap_entry = $ldap_server->dnExists($proposed_ldap_entry['dn'], 'ldap_entry', array());
|
857 |
|
|
return $ldap_entry;
|
858 |
|
|
|
859 |
|
|
}
|
860 |
|
|
|
861 |
|
|
|
862 |
|
|
|
863 |
|
|
|
864 |
|
|
|
865 |
|
|
|
866 |
|
|
|
867 |
|
|
|
868 |
|
|
|
869 |
|
|
public function deleteProvisionedLdapEntries($account) {
|
870 |
|
|
|
871 |
|
|
|
872 |
|
|
$boolean_result = FALSE;
|
873 |
|
|
$language = ($account->language) ? $account->language : 'und';
|
874 |
|
|
if (isset($account->ldap_user_prov_entries[$language][0])) {
|
875 |
|
|
foreach ($account->ldap_user_prov_entries[$language] as $i => $field_instance) {
|
876 |
|
|
$parts = explode('|', $field_instance['value']);
|
877 |
|
|
if (count($parts) == 2) {
|
878 |
|
|
|
879 |
|
|
list($sid, $dn) = $parts;
|
880 |
|
|
$ldap_server = ldap_servers_get_servers($sid, NULL, TRUE);
|
881 |
|
|
if (is_object($ldap_server) && $dn) {
|
882 |
|
|
$boolean_result = $ldap_server->delete($dn);
|
883 |
|
|
$tokens = array('%sid' => $sid, '%dn' => $dn, '%username' => $account->name, '%uid' => $account->uid);
|
884 |
|
|
if ($boolean_result) {
|
885 |
|
|
watchdog('ldap_user', 'LDAP entry on server %sid deleted dn=%dn. username=%username, uid=%uid', $tokens, WATCHDOG_INFO);
|
886 |
|
|
}
|
887 |
|
|
else {
|
888 |
|
|
watchdog('ldap_user', 'LDAP entry on server %sid not deleted because error. username=%username, uid=%uid', $tokens, WATCHDOG_ERROR);
|
889 |
|
|
}
|
890 |
|
|
}
|
891 |
|
|
else {
|
892 |
|
|
$boolean_result = FALSE;
|
893 |
|
|
}
|
894 |
|
|
}
|
895 |
|
|
}
|
896 |
|
|
}
|
897 |
|
|
return $boolean_result;
|
898 |
|
|
|
899 |
|
|
}
|
900 |
|
|
|
901 |
|
|
|
902 |
|
|
|
903 |
|
|
|
904 |
|
|
|
905 |
|
|
|
906 |
|
|
|
907 |
|
|
|
908 |
|
|
|
909 |
|
|
|
910 |
|
|
|
911 |
|
|
|
912 |
|
|
|
913 |
|
|
|
914 |
|
|
|
915 |
|
|
|
916 |
|
|
|
917 |
|
|
function drupalUserToLdapEntry($account, $ldap_server, $params, $ldap_user_entry = NULL) {
|
918 |
|
|
|
919 |
|
|
|
920 |
|
|
$provision = (isset($params['function']) && $params['function'] == 'provisionLdapEntry');
|
921 |
|
|
$result = LDAP_USER_PROV_RESULT_NO_ERROR;
|
922 |
|
|
if (!$ldap_user_entry) {
|
923 |
|
|
$ldap_user_entry = array();
|
924 |
|
|
}
|
925 |
|
|
|
926 |
|
|
if (!is_object($account) || !is_object($ldap_server)) {
|
927 |
|
|
return array(NULL, LDAP_USER_PROV_RESULT_BAD_PARAMS);
|
928 |
|
|
}
|
929 |
|
|
$watchdog_tokens = array(
|
930 |
|
|
'%drupal_username' => $account->name,
|
931 |
|
|
);
|
932 |
|
|
$include_count = (isset($params['include_count']) && $params['include_count']);
|
933 |
|
|
|
934 |
|
|
$direction = isset($params['direction']) ? $params['direction'] : LDAP_USER_PROV_DIRECTION_ALL;
|
935 |
|
|
$prov_events = empty($params['prov_events']) ? ldap_user_all_events() : $params['prov_events'];
|
936 |
|
|
|
937 |
|
|
$mappings = $this->getSynchMappings($direction, $prov_events);
|
938 |
|
|
|
939 |
|
|
|
940 |
|
|
|
941 |
|
|
foreach ($mappings as $field_key => $field_detail) {
|
942 |
|
|
list($ldap_attr_name, $ordinal, $conversion) = ldap_servers_token_extract_parts($field_key, TRUE);
|
943 |
|
|
$ordinal = (!$ordinal) ? 0 : $ordinal;
|
944 |
|
|
if ($ldap_user_entry && isset($ldap_user_entry[$ldap_attr_name]) && is_array($ldap_user_entry[$ldap_attr_name]) && isset($ldap_user_entry[$ldap_attr_name][$ordinal]) ) {
|
945 |
|
|
continue;
|
946 |
|
|
}
|
947 |
|
|
|
948 |
|
|
$synched = $this->isSynched($field_key, $params['prov_events'], LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY);
|
949 |
|
|
|
950 |
|
|
if ($synched) {
|
951 |
|
|
$token = ($field_detail['user_attr'] == 'user_tokens') ? $field_detail['user_tokens'] : $field_detail['user_attr'];
|
952 |
|
|
$value = ldap_servers_token_replace($account, $token, 'user_account');
|
953 |
|
|
|
954 |
|
|
if (substr($token, 0, 10) == '[password.' && (!$value || $value == $token)) {
|
955 |
|
|
if (!$provision) {
|
956 |
|
|
continue;
|
957 |
|
|
}
|
958 |
|
|
}
|
959 |
|
|
|
960 |
|
|
if ($ldap_attr_name == 'dn' && $value) {
|
961 |
|
|
$ldap_user_entry['dn'] = $value;
|
962 |
|
|
}
|
963 |
|
|
elseif ($value) {
|
964 |
|
|
if (!isset($ldap_user_entry[$ldap_attr_name]) || !is_array($ldap_user_entry[$ldap_attr_name])) {
|
965 |
|
|
$ldap_user_entry[$ldap_attr_name] = array();
|
966 |
|
|
}
|
967 |
|
|
$ldap_user_entry[$ldap_attr_name][$ordinal] = $value;
|
968 |
|
|
if ($include_count) {
|
969 |
|
|
$ldap_user_entry[$ldap_attr_name]['count'] = count($ldap_user_entry[$ldap_attr_name]);
|
970 |
|
|
}
|
971 |
|
|
|
972 |
|
|
}
|
973 |
|
|
|
974 |
|
|
}
|
975 |
|
|
|
976 |
|
|
}
|
977 |
|
|
|
978 |
|
|
|
979 |
|
|
|
980 |
|
|
|
981 |
|
|
|
982 |
|
|
drupal_alter('ldap_entry', $ldap_user_entry, $params);
|
983 |
|
|
|
984 |
|
|
return array($ldap_user_entry, $result);
|
985 |
|
|
|
986 |
|
|
}
|
987 |
|
|
|
988 |
|
|
|
989 |
|
|
|
990 |
|
|
|
991 |
|
|
|
992 |
|
|
|
993 |
|
|
|
994 |
|
|
|
995 |
|
|
|
996 |
|
|
|
997 |
|
|
|
998 |
|
|
|
999 |
|
|
|
1000 |
|
|
|
1001 |
|
|
|
1002 |
|
|
|
1003 |
|
|
|
1004 |
|
|
public function provisionDrupalAccount($account = FALSE, &$user_edit, $ldap_user = NULL, $save = TRUE) {
|
1005 |
|
|
|
1006 |
|
|
$watchdog_tokens = array();
|
1007 |
|
|
|
1008 |
|
|
|
1009 |
|
|
|
1010 |
|
|
|
1011 |
|
|
|
1012 |
|
|
|
1013 |
|
|
if (!$account) {
|
1014 |
|
|
$account = new stdClass();
|
1015 |
|
|
}
|
1016 |
|
|
$account->is_new = TRUE;
|
1017 |
|
|
|
1018 |
|
|
if (!$ldap_user && !isset($user_edit['name'])) {
|
1019 |
|
|
return FALSE;
|
1020 |
|
|
}
|
1021 |
|
|
|
1022 |
|
|
if (!$ldap_user) {
|
1023 |
|
|
$watchdog_tokens['%username'] = $user_edit['name'];
|
1024 |
|
|
if ($this->drupalAcctProvisionServer) {
|
1025 |
|
|
$ldap_user = ldap_servers_get_user_ldap_data($user_edit['name'], $this->drupalAcctProvisionServer, 'ldap_user_prov_to_drupal');
|
1026 |
|
|
}
|
1027 |
|
|
if (!$ldap_user) {
|
1028 |
|
|
if ($this->detailedWatchdog) {
|
1029 |
|
|
watchdog('ldap_user', '%username : failed to find associated ldap entry for username in provision.', $watchdog_tokens, WATCHDOG_DEBUG);
|
1030 |
|
|
}
|
1031 |
|
|
return FALSE;
|
1032 |
|
|
}
|
1033 |
|
|
}
|
1034 |
|
|
|
1035 |
|
|
if (!isset($user_edit['name']) && isset($account->name)) {
|
1036 |
|
|
$user_edit['name'] = $account->name;
|
1037 |
|
|
$watchdog_tokens['%username'] = $user_edit['name'];
|
1038 |
|
|
}
|
1039 |
dd54aff9
|
Assos Assos
|
|
1040 |
|
|
|
1041 |
|
|
|
1042 |
|
|
if ($this->drupalAcctProvisionServer && $this->drupalAcctProvisionServer != LDAP_USER_AUTH_SERVER_SID) {
|
1043 |
85ad3d82
|
Assos Assos
|
|
1044 |
|
|
$ldap_server = ldap_servers_get_servers($this->drupalAcctProvisionServer, 'enabled', TRUE);
|
1045 |
|
|
|
1046 |
|
|
$params = array(
|
1047 |
|
|
'account' => $account,
|
1048 |
|
|
'user_edit' => $user_edit,
|
1049 |
|
|
'prov_event' => LDAP_USER_EVENT_CREATE_DRUPAL_USER,
|
1050 |
|
|
'module' => 'ldap_user',
|
1051 |
|
|
'function' => 'provisionDrupalAccount',
|
1052 |
|
|
'direction' => LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER,
|
1053 |
|
|
);
|
1054 |
|
|
|
1055 |
|
|
drupal_alter('ldap_entry', $ldap_user, $params);
|
1056 |
|
|
|
1057 |
|
|
|
1058 |
|
|
$puid = $ldap_server->userPuidFromLdapEntry($ldap_user['attr']);
|
1059 |
|
|
$account2 = ($puid) ? $ldap_server->userUserEntityFromPuid($puid) : FALSE;
|
1060 |
|
|
|
1061 |
|
|
if ($account2) {
|
1062 |
|
|
|
1063 |
|
|
$this->entryToUserEdit($ldap_user, $user_edit, $ldap_server, LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, array(LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER));
|
1064 |
|
|
$account = user_save($account2, $user_edit, 'ldap_user');
|
1065 |
|
|
user_set_authmaps($account, array("authname_ldap_user" => $user_edit['name']));
|
1066 |
|
|
|
1067 |
|
|
if ($account) {
|
1068 |
|
|
$account = $this->synchToDrupalAccount($account, $user_edit, LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER, $ldap_user, TRUE);
|
1069 |
|
|
}
|
1070 |
|
|
return $account;
|
1071 |
|
|
}
|
1072 |
|
|
else {
|
1073 |
|
|
$this->entryToUserEdit($ldap_user, $user_edit, $ldap_server, LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, array(LDAP_USER_EVENT_CREATE_DRUPAL_USER));
|
1074 |
|
|
if ($save) {
|
1075 |
|
|
$watchdog_tokens = array('%drupal_username' => $user_edit['name']);
|
1076 |
|
|
if (empty($user_edit['name'])) {
|
1077 |
|
|
drupal_set_message(t('User account creation failed because of invalid, empty derived Drupal username.'), 'error');
|
1078 |
|
|
watchdog('ldap_user',
|
1079 |
|
|
'Failed to create Drupal account %drupal_username because drupal username could not be derived.',
|
1080 |
|
|
$watchdog_tokens,
|
1081 |
|
|
WATCHDOG_ERROR
|
1082 |
|
|
);
|
1083 |
|
|
return FALSE;
|
1084 |
|
|
}
|
1085 |
|
|
if (!isset($user_edit['mail']) || !$user_edit['mail']) {
|
1086 |
|
|
drupal_set_message(t('User account creation failed because of invalid, empty derived email address.'), 'error');
|
1087 |
|
|
watchdog('ldap_user',
|
1088 |
|
|
'Failed to create Drupal account %drupal_username because email address could not be derived by LDAP User module',
|
1089 |
|
|
$watchdog_tokens,
|
1090 |
|
|
WATCHDOG_ERROR
|
1091 |
|
|
);
|
1092 |
|
|
return FALSE;
|
1093 |
|
|
}
|
1094 |
|
|
if ($account_with_same_email = user_load_by_mail($user_edit['mail'])) {
|
1095 |
|
|
$watchdog_tokens['%email'] = $user_edit['mail'];
|
1096 |
|
|
$watchdog_tokens['%duplicate_name'] = $account_with_same_email->name;
|
1097 |
|
|
watchdog('ldap_user', 'LDAP user %drupal_username has email address
|
1098 |
|
|
(%email) conflict with a drupal user %duplicate_name', $watchdog_tokens, WATCHDOG_ERROR);
|
1099 |
|
|
drupal_set_message(t('Another user already exists in the system with the same email address. You should contact the system administrator in order to solve this conflict.'), 'error');
|
1100 |
|
|
return FALSE;
|
1101 |
|
|
}
|
1102 |
|
|
$account = user_save(NULL, $user_edit, 'ldap_user');
|
1103 |
|
|
if (!$account) {
|
1104 |
|
|
drupal_set_message(t('User account creation failed because of system problems.'), 'error');
|
1105 |
|
|
}
|
1106 |
|
|
else {
|
1107 |
|
|
user_set_authmaps($account, array('authname_ldap_user' => $account->name));
|
1108 |
|
|
ldap_user_ldap_provision_semaphore('drupal_created', 'set', $account->name);
|
1109 |
|
|
}
|
1110 |
|
|
return $account;
|
1111 |
|
|
}
|
1112 |
|
|
return TRUE;
|
1113 |
|
|
}
|
1114 |
|
|
}
|
1115 |
|
|
}
|
1116 |
|
|
|
1117 |
|
|
|
1118 |
|
|
|
1119 |
|
|
|
1120 |
|
|
|
1121 |
|
|
|
1122 |
|
|
|
1123 |
|
|
|
1124 |
|
|
|
1125 |
|
|
function ldapAssociateDrupalAccount($drupal_username) {
|
1126 |
|
|
|
1127 |
|
|
if ($this->drupalAcctProvisionServer) {
|
1128 |
|
|
$prov_events = array(LDAP_USER_EVENT_LDAP_ASSOCIATE_DRUPAL_ACCT);
|
1129 |
|
|
$ldap_server = ldap_servers_get_servers($this->drupalAcctProvisionServer, 'enabled', TRUE);
|
1130 |
|
|
$account = user_load_by_name($drupal_username);
|
1131 |
|
|
$ldap_user = ldap_servers_get_user_ldap_data($drupal_username, $this->drupalAcctProvisionServer, 'ldap_user_prov_to_drupal');
|
1132 |
|
|
if (!$account) {
|
1133 |
|
|
watchdog(
|
1134 |
|
|
'ldap_user',
|
1135 |
|
|
'Failed to LDAP associate drupal account %drupal_username because account not found',
|
1136 |
|
|
array('%drupal_username' => $drupal_username),
|
1137 |
|
|
WATCHDOG_ERROR
|
1138 |
|
|
);
|
1139 |
|
|
return FALSE;
|
1140 |
|
|
}
|
1141 |
|
|
elseif (!$ldap_user) {
|
1142 |
|
|
watchdog(
|
1143 |
|
|
'ldap_user',
|
1144 |
|
|
'Failed to LDAP associate drupal account %drupal_username because corresponding LDAP entry not found',
|
1145 |
|
|
array('%drupal_username' => $drupal_username),
|
1146 |
|
|
WATCHDOG_ERROR
|
1147 |
|
|
);
|
1148 |
|
|
return FALSE;
|
1149 |
|
|
}
|
1150 |
|
|
else {
|
1151 |
|
|
$user_edit = array();
|
1152 |
|
|
$user_edit['data']['ldap_user']['init'] = array(
|
1153 |
|
|
'sid' => $ldap_user['sid'],
|
1154 |
|
|
'dn' => $ldap_user['dn'],
|
1155 |
|
|
'mail' => $account->mail,
|
1156 |
|
|
);
|
1157 |
|
|
$ldap_user_puid = $ldap_server->userPuidFromLdapEntry($ldap_user['attr']);
|
1158 |
|
|
if ($ldap_user_puid) {
|
1159 |
|
|
$user_edit['ldap_user_puid'][LANGUAGE_NONE][0]['value'] = $ldap_user_puid;
|
1160 |
|
|
}
|
1161 |
|
|
$user_edit['ldap_user_puid_property'][LANGUAGE_NONE][0]['value'] = $ldap_server->unique_persistent_attr;
|
1162 |
|
|
$user_edit['ldap_user_puid_sid'][LANGUAGE_NONE][0]['value'] = $ldap_server->sid;
|
1163 |
|
|
$user_edit['ldap_user_current_dn'][LANGUAGE_NONE][0]['value'] = $ldap_user['dn'];
|
1164 |
|
|
$account = user_save($account, $user_edit, 'ldap_user');
|
1165 |
|
|
return (boolean)$account;
|
1166 |
|
|
}
|
1167 |
|
|
}
|
1168 |
|
|
else {
|
1169 |
|
|
return FALSE;
|
1170 |
|
|
}
|
1171 |
|
|
}
|
1172 |
|
|
|
1173 |
|
|
|
1174 |
|
|
|
1175 |
|
|
|
1176 |
|
|
|
1177 |
|
|
|
1178 |
|
|
|
1179 |
|
|
|
1180 |
|
|
|
1181 |
|
|
|
1182 |
|
|
|
1183 |
|
|
|
1184 |
|
|
function entryToUserEdit($ldap_user, &$edit, $ldap_server, $direction = LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, $prov_events = NULL) {
|
1185 |
|
|
|
1186 |
|
|
|
1187 |
|
|
if (!$prov_events) {
|
1188 |
|
|
$prov_events = ldap_user_all_events();
|
1189 |
|
|
}
|
1190 |
|
|
$mail_synched = $this->isSynched('[property.mail]', $prov_events, $direction);
|
1191 |
|
|
if (!isset($edit['mail']) && $mail_synched) {
|
1192 |
|
|
$derived_mail = $ldap_server->userEmailFromLdapEntry($ldap_user['attr']);
|
1193 |
|
|
if ($derived_mail) {
|
1194 |
|
|
$edit['mail'] = $derived_mail;
|
1195 |
|
|
}
|
1196 |
|
|
}
|
1197 |
|
|
|
1198 |
|
|
$drupal_username = $ldap_server->userUsernameFromLdapEntry($ldap_user['attr']);
|
1199 |
|
|
if ($this->isSynched('[property.picture]', $prov_events, $direction)){
|
1200 |
|
|
|
1201 |
|
|
$picture = $ldap_server->userPictureFromLdapEntry($ldap_user['attr'], $drupal_username);
|
1202 |
|
|
|
1203 |
|
|
if ($picture){
|
1204 |
|
|
$edit['picture'] = $picture;
|
1205 |
|
|
if(isset($picture->md5Sum)){
|
1206 |
|
|
$edit['data']['ldap_user']['init']['thumb5md'] = $picture->md5Sum;
|
1207 |
|
|
}
|
1208 |
|
|
}
|
1209 |
|
|
}
|
1210 |
|
|
|
1211 |
|
|
if ($this->isSynched('[property.name]', $prov_events, $direction) && !isset($edit['name']) && $drupal_username) {
|
1212 |
|
|
$edit['name'] = $drupal_username;
|
1213 |
|
|
}
|
1214 |
|
|
|
1215 |
|
|
if ($direction == LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER && in_array(LDAP_USER_EVENT_CREATE_DRUPAL_USER, $prov_events)) {
|
1216 |
|
|
$edit['mail'] = isset($edit['mail']) ? $edit['mail'] : $ldap_user['mail'];
|
1217 |
|
|
if (!isset($edit['pass'])) {
|
1218 |
|
|
$edit['pass'] = user_password(20);
|
1219 |
|
|
watchdog('ldap_user', '20 character random password generated for the %username account that has been created.', array('%username' => $drupal_username), WATCHDOG_INFO);
|
1220 |
|
|
}
|
1221 |
|
|
$edit['init'] = isset($edit['init']) ? $edit['init'] : $edit['mail'];
|
1222 |
|
|
$edit['status'] = isset($edit['status']) ? $edit['status'] : 1;
|
1223 |
|
|
$edit['signature'] = isset($edit['signature']) ? $edit['signature'] : '';
|
1224 |
|
|
|
1225 |
|
|
$edit['data']['ldap_user']['init'] = array(
|
1226 |
|
|
'sid' => $ldap_user['sid'],
|
1227 |
|
|
'dn' => $ldap_user['dn'],
|
1228 |
|
|
'mail' => $edit['mail'],
|
1229 |
|
|
);
|
1230 |
|
|
}
|
1231 |
|
|
|
1232 |
|
|
|
1233 |
|
|
|
1234 |
|
|
|
1235 |
|
|
|
1236 |
|
|
if ($direction == LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER && in_array(LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER, $prov_events)) {
|
1237 |
|
|
$edit['data']['ldap_user']['init'] = array(
|
1238 |
|
|
'sid' => $ldap_user['sid'],
|
1239 |
|
|
'dn' => $ldap_user['dn'],
|
1240 |
|
|
'mail' => isset($edit['mail']) && !empty($edit['mail']) ? $edit['mail'] : $ldap_user['mail'],
|
1241 |
|
|
);
|
1242 |
|
|
}
|
1243 |
|
|
|
1244 |
|
|
|
1245 |
|
|
|
1246 |
|
|
|
1247 |
|
|
if ($this->isSynched('[field.ldap_user_puid]', $prov_events, $direction)) {
|
1248 |
|
|
$ldap_user_puid = $ldap_server->userPuidFromLdapEntry($ldap_user['attr']);
|
1249 |
|
|
if ($ldap_user_puid) {
|
1250 |
|
|
$edit['ldap_user_puid'][LANGUAGE_NONE][0]['value'] = $ldap_user_puid;
|
1251 |
|
|
}
|
1252 |
|
|
}
|
1253 |
|
|
if ($this->isSynched('[field.ldap_user_puid_property]', $prov_events, $direction)) {
|
1254 |
|
|
$edit['ldap_user_puid_property'][LANGUAGE_NONE][0]['value'] = $ldap_server->unique_persistent_attr;
|
1255 |
|
|
}
|
1256 |
|
|
if ($this->isSynched('[field.ldap_user_puid_sid]', $prov_events, $direction)) {
|
1257 |
|
|
$edit['ldap_user_puid_sid'][LANGUAGE_NONE][0]['value'] = $ldap_server->sid;
|
1258 |
|
|
}
|
1259 |
|
|
if ($this->isSynched('[field.ldap_user_current_dn]', $prov_events, $direction)) {
|
1260 |
|
|
$edit['ldap_user_current_dn'][LANGUAGE_NONE][0]['value'] = $ldap_user['dn'];
|
1261 |
|
|
}
|
1262 |
|
|
|
1263 |
|
|
|
1264 |
|
|
$mappings = $this->getSynchMappings($direction, $prov_events);
|
1265 |
|
|
|
1266 |
|
|
|
1267 |
|
|
foreach ($mappings as $user_attr_key => $field_detail) {
|
1268 |
|
|
|
1269 |
|
|
|
1270 |
|
|
if (!$this->isSynched($user_attr_key, $prov_events, $direction)) {
|
1271 |
|
|
continue;
|
1272 |
|
|
}
|
1273 |
|
|
|
1274 |
|
|
|
1275 |
|
|
|
1276 |
|
|
|
1277 |
|
|
if ($field_detail['convert'] && strpos($field_detail['ldap_attr'], ';') === FALSE) {
|
1278 |
|
|
$field_detail['ldap_attr'] = str_replace(']', ';binary]', $field_detail['ldap_attr']);
|
1279 |
|
|
}
|
1280 |
|
|
$value = ldap_servers_token_replace($ldap_user['attr'], $field_detail['ldap_attr'], 'ldap_entry');
|
1281 |
|
|
list($value_type, $value_name, $value_instance) = ldap_servers_parse_user_attr_name($user_attr_key);
|
1282 |
|
|
|
1283 |
|
|
|
1284 |
|
|
|
1285 |
|
|
|
1286 |
|
|
if ($value_type == 'field') {
|
1287 |
|
|
|
1288 |
|
|
$field = field_info_field($value_name);
|
1289 |
|
|
|
1290 |
|
|
$columns = array_keys($field['columns']);
|
1291 |
|
|
|
1292 |
|
|
$values = $field['cardinality'] == 1 ? array($value) : (array) $value;
|
1293 |
|
|
|
1294 |
|
|
$items = array();
|
1295 |
|
|
|
1296 |
|
|
foreach ($values as $delta => $value) {
|
1297 |
|
|
if (isset($value)) {
|
1298 |
|
|
|
1299 |
|
|
|
1300 |
|
|
$items[$delta][$columns[0]] = $value;
|
1301 |
|
|
}
|
1302 |
|
|
}
|
1303 |
|
|
|
1304 |
|
|
$edit[$value_name][LANGUAGE_NONE] = $items;
|
1305 |
|
|
}
|
1306 |
|
|
elseif ($value_type == 'property') {
|
1307 |
|
|
|
1308 |
|
|
$edit[$value_name] = $value;
|
1309 |
|
|
}
|
1310 |
|
|
}
|
1311 |
|
|
|
1312 |
|
|
|
1313 |
|
|
|
1314 |
|
|
drupal_alter('ldap_user_edit_user', $edit, $ldap_user, $ldap_server, $prov_events);
|
1315 |
|
|
if (isset($edit['name']) && $edit['name'] == '') {
|
1316 |
|
|
unset($edit['name']);
|
1317 |
|
|
}
|
1318 |
|
|
|
1319 |
|
|
}
|
1320 |
|
|
|
1321 |
|
|
|
1322 |
|
|
|
1323 |
|
|
|
1324 |
|
|
|
1325 |
|
|
|
1326 |
|
|
|
1327 |
|
|
|
1328 |
|
|
|
1329 |
|
|
public function isSynched($attr_token, $prov_events, $direction) {
|
1330 |
|
|
$result = (boolean)(
|
1331 |
|
|
isset($this->synchMapping[$direction][$attr_token]['prov_events']) &&
|
1332 |
|
|
count(array_intersect($prov_events, $this->synchMapping[$direction][$attr_token]['prov_events']))
|
1333 |
|
|
);
|
1334 |
|
|
if (!$result) {
|
1335 |
|
|
if (isset($this->synchMapping[$direction][$attr_token])) {
|
1336 |
|
|
|
1337 |
|
|
}
|
1338 |
|
|
else {
|
1339 |
|
|
|
1340 |
|
|
}
|
1341 |
|
|
}
|
1342 |
|
|
return $result;
|
1343 |
|
|
}
|
1344 |
|
|
|
1345 |
|
|
|
1346 |
|
|
} |