root / drupal7 / sites / all / modules / ldap / ldap_user / LdapUserConf.class.php @ 59ae487e
1 |
<?php
|
---|---|
2 |
|
3 |
/**
|
4 |
* @file
|
5 |
* This class represents a ldap_user module's configuration
|
6 |
* It is extended by LdapUserConfAdmin for configuration and other admin functions
|
7 |
*/
|
8 |
|
9 |
require_once('ldap_user.module'); |
10 |
|
11 |
class LdapUserConf { |
12 |
|
13 |
/**
|
14 |
* server providing Drupal account provisioning
|
15 |
*
|
16 |
* @var string
|
17 |
*
|
18 |
* @see LdapServer::sid
|
19 |
*/
|
20 |
public $drupalAcctProvisionServer = LDAP_USER_NO_SERVER_SID; |
21 |
|
22 |
/**
|
23 |
* server providing LDAP entry provisioning
|
24 |
*
|
25 |
* @var string
|
26 |
*
|
27 |
* @see LdapServer::sid
|
28 |
*/
|
29 |
public $ldapEntryProvisionServer = LDAP_USER_NO_SERVER_SID; |
30 |
|
31 |
/**
|
32 |
* Associative array mapping synch directions to ldap server instances.
|
33 |
*
|
34 |
* @var array
|
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 |
* Array of events that trigger provisioning of Drupal Accounts
|
43 |
* Valid constants are:
|
44 |
* LDAP_USER_DRUPAL_USER_PROV_ON_AUTHENTICATE
|
45 |
* LDAP_USER_DRUPAL_USER_PROV_ON_USER_UPDATE_CREATE
|
46 |
* LDAP_USER_DRUPAL_USER_PROV_ON_ALLOW_MANUAL_CREATE
|
47 |
*
|
48 |
* @var array
|
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 |
* Array of events that trigger provisioning of LDAP Entries
|
54 |
* Valid constants are:
|
55 |
* LDAP_USER_LDAP_ENTRY_PROV_ON_USER_UPDATE_CREATE
|
56 |
* LDAP_USER_LDAP_ENTRY_PROV_ON_AUTHENTICATE
|
57 |
* LDAP_USER_LDAP_ENTRY_DELETE_ON_USER_DELETE
|
58 |
*
|
59 |
* @var array
|
60 |
*/
|
61 |
public $ldapEntryProvisionTriggers = array(); |
62 |
|
63 |
/**
|
64 |
* server providing LDAP entry provisioning
|
65 |
*
|
66 |
* @var string
|
67 |
*
|
68 |
* @see LdapServer::sid
|
69 |
*/
|
70 |
public $userConflictResolve = LDAP_USER_CONFLICT_RESOLVE_DEFAULT; |
71 |
|
72 |
/**
|
73 |
* drupal account creation model
|
74 |
*
|
75 |
* @var int
|
76 |
* LDAP_USER_ACCT_CREATION_LDAP_BEHAVIOR /admin/config/people/accounts/settings do not affect "LDAP Associated" Drupal accounts.
|
77 |
* LDAP_USER_ACCT_CREATION_USER_SETTINGS_FOR_LDAP use Account creation settings at /admin/config/people/accounts/settings
|
78 |
*/
|
79 |
public $acctCreation = LDAP_USER_ACCT_CREATION_LDAP_BEHAVIOR_DEFAULT; |
80 |
|
81 |
/**
|
82 |
* has current object been saved to the database?
|
83 |
*
|
84 |
* @var boolean
|
85 |
*
|
86 |
*/
|
87 |
public $inDatabase = FALSE; |
88 |
|
89 |
/**
|
90 |
* what to do when an ldap provisioned username conflicts with existing drupal user?
|
91 |
*
|
92 |
* @var int
|
93 |
* LDAP_USER_CONFLICT_LOG - log the conflict
|
94 |
* LDAP_USER_CONFLICT_RESOLVE - LDAP associate the existing drupal user
|
95 |
*
|
96 |
*/
|
97 |
public $manualAccountConflict = LDAP_USER_MANUAL_ACCT_CONFLICT_REJECT; |
98 |
|
99 |
public $setsLdapPassword = TRUE; // @todo default to FALSE and check for mapping to set to true |
100 |
|
101 |
public $loginConflictResolve = FALSE; |
102 |
|
103 |
public $disableAdminPasswordField = FALSE; |
104 |
/**
|
105 |
* array of field synch mappings provided by all modules (via hook_ldap_user_attrs_list_alter())
|
106 |
* array of the form: array(
|
107 |
* LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER | LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY => array(
|
108 |
* <server_id> => array(
|
109 |
* 'sid' => <server_id> (redundant)
|
110 |
* 'ldap_attr' => e.g. [sn]
|
111 |
* 'user_attr' => e.g. [field.field_user_lname] (when this value is set to 'user_tokens', 'user_tokens' value is used.)
|
112 |
* 'user_tokens' => e.g. [field.field_user_lname], [field.field_user_fname]
|
113 |
* 'convert' => 1|0 boolean indicating need to covert from binary
|
114 |
* 'direction' => LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER | LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY (redundant)
|
115 |
* 'config_module' => 'ldap_user'
|
116 |
* 'prov_module' => 'ldap_user'
|
117 |
* 'enabled' => 1|0 boolean
|
118 |
* prov_events' => array( of LDAP_USER_EVENT_* constants indicating during which synch actions field should be synched)
|
119 |
* - four permutations available
|
120 |
* to ldap: LDAP_USER_EVENT_CREATE_LDAP_ENTRY, LDAP_USER_EVENT_SYNCH_TO_LDAP_ENTRY,
|
121 |
* to drupal: LDAP_USER_EVENT_CREATE_DRUPAL_USER, LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER
|
122 |
* )
|
123 |
* )
|
124 |
*/
|
125 |
public $synchMapping = NULL; // array of field synching directions for each operation. should include ldapUserSynchMappings |
126 |
// keyed on direction => property, ldap, or field token such as '[field.field_lname] with brackets in them.
|
127 |
|
128 |
/**
|
129 |
* synch mappings configured in ldap user module (not in other modules)
|
130 |
* array of the form: array(
|
131 |
LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER | LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY => array(
|
132 |
'sid' => <server_id> (redundant)
|
133 |
'ldap_attr' => e.g. [sn]
|
134 |
'user_attr' => e.g. [field.field_user_lname] (when this value is set to 'user_tokens', 'user_tokens' value is used.)
|
135 |
'user_tokens' => e.g. [field.field_user_lname], [field.field_user_fname]
|
136 |
'convert' => 1|0 boolean indicating need to covert from binary
|
137 |
'direction' => LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER | LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY (redundant)
|
138 |
'config_module' => 'ldap_user'
|
139 |
'prov_module' => 'ldap_user'
|
140 |
'enabled' => 1|0 boolean
|
141 |
prov_events' => array( of LDAP_USER_EVENT_* constants indicating during which synch actions field should be synched)
|
142 |
- four permutations available
|
143 |
to ldap: LDAP_USER_EVENT_CREATE_LDAP_ENTRY, LDAP_USER_EVENT_SYNCH_TO_LDAP_ENTRY,
|
144 |
to drupal: LDAP_USER_EVENT_CREATE_DRUPAL_USER, LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER
|
145 |
)
|
146 |
)
|
147 |
)
|
148 |
*/
|
149 |
public $ldapUserSynchMappings = NULL; // |
150 |
// keyed on property, ldap, or field token such as '[field.field_lname] with brackets in them.
|
151 |
public $detailedWatchdog = FALSE; |
152 |
public $provisionsDrupalAccountsFromLdap = FALSE; |
153 |
public $provisionsLdapEntriesFromDrupalUsers = FALSE; |
154 |
|
155 |
// what should be done with ldap provisioned accounts that no longer have associated drupal accounts.
|
156 |
public $orphanedDrupalAcctBehavior = 'ldap_user_orphan_email'; |
157 |
/** options are partially derived from user module account cancel options:
|
158 |
*
|
159 |
'ldap_user_orphan_do_not_check' => Do not check for orphaned Drupal accounts.)
|
160 |
'ldap_user_orphan_email' => Perform no action, but email list of orphaned accounts. (All the other options will send email summaries also.)
|
161 |
'user_cancel_block' => Disable the account and keep its content.
|
162 |
'user_cancel_block_unpublish' => Disable the account and unpublish its content.
|
163 |
'user_cancel_reassign' => Delete the account and make its content belong to the Anonymous user.
|
164 |
'user_cancel_delete' => Delete the account and its content.
|
165 |
*/
|
166 |
|
167 |
public $orphanedCheckQty = 100; |
168 |
|
169 |
// public $wsKey = NULL;
|
170 |
// public $wsEnabled = 0;
|
171 |
// public $wsUserIps = array();
|
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 |
// 'wsKey','wsEnabled','wsUserIps',
|
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 |
// determine account creation configuration
|
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 |
* Destructor Method
|
253 |
*/
|
254 |
function __destruct() { } |
255 |
|
256 |
|
257 |
/**
|
258 |
* Util to fetch mappings for a given direction
|
259 |
*
|
260 |
* @param string $sid
|
261 |
* The server id
|
262 |
* @param string $direction LDAP_USER_PROV_DIRECTION_* constant
|
263 |
* @param array $prov_events
|
264 |
*
|
265 |
* @return array/bool
|
266 |
* Array of mappings (may be empty array)
|
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 |
* Util to fetch attributes required for this user conf, not other modules.
|
321 |
*
|
322 |
* @param enum $direction LDAP_USER_PROV_DIRECTION_* constants
|
323 |
* @param string $ldap_context
|
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 |
// Add the attribute to our array.
|
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 |
* converts the more general ldap_context string to its associated ldap user event
|
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 |
* converts the more general ldap_context string to its associated ldap user prov direction
|
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 |
// provisioning is can hapen in both directions in most contexts
|
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 |
derive mapping array from ldap user configuration and other configurations.
|
408 |
if this becomes a resource hungry function should be moved to ldap_user functions
|
409 |
and stored with static variable. should be cached also.
|
410 |
|
411 |
this should be cached and modules implementing ldap_user_synch_mapping_alter
|
412 |
should know when to invalidate cache.
|
413 |
|
414 |
*/
|
415 |
|
416 |
function setSynchMapping($reset = TRUE) { // @todo change default to false after development |
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 |
* given a $prov_event determine if ldap user configuration supports it.
|
444 |
* this is overall, not per field synching configuration
|
445 |
*
|
446 |
* @param enum $direction LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER or LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY
|
447 |
*
|
448 |
* @param enum $prov_event
|
449 |
* LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER, LDAP_USER_EVENT_CREATE_DRUPAL_USER
|
450 |
* LDAP_USER_EVENT_SYNCH_TO_LDAP_ENTRY LDAP_USER_EVENT_CREATE_LDAP_ENTRY
|
451 |
* LDAP_USER_EVENT_LDAP_ASSOCIATE_DRUPAL_ACCT
|
452 |
* LDAP_USER_EVENT_ALL
|
453 |
*
|
454 |
* @param enum $action 'synch', 'provision', 'delete_ldap_entry', 'delete_drupal_entry', 'cancel_drupal_entry'
|
455 |
* @return boolean
|
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 |
* given a drupal account, provision an ldap entry if none exists. if one exists do nothing
|
485 |
*
|
486 |
* @param object $account drupal account object with minimum of name property
|
487 |
* @param array $ldap_user as prepopulated ldap entry. usually not provided
|
488 |
*
|
489 |
* @return array of form:
|
490 |
* array('status' => 'success', 'fail', or 'conflict'),
|
491 |
* array('ldap_server' => ldap server object),
|
492 |
* array('proposed' => proposed ldap entry),
|
493 |
* array('existing' => existing ldap entry),
|
494 |
* array('description' = > blah blah)
|
495 |
*
|
496 |
*/
|
497 |
|
498 |
public function provisionLdapEntry($account, $ldap_user = NULL, $test_query = FALSE) { |
499 |
$watchdog_tokens = array(); |
500 |
$result = array( |
501 |
'status' => NULL, |
502 |
'ldap_server' => NULL, |
503 |
'proposed' => NULL, |
504 |
'existing' => NULL, |
505 |
'description' => NULL, |
506 |
); |
507 |
|
508 |
if (is_scalar($account)) { |
509 |
$username = $account; |
510 |
$account = new stdClass(); |
511 |
$account->name = $username; |
512 |
} |
513 |
|
514 |
list($account, $user_entity) = ldap_user_load_user_acct_and_entity($account->name); |
515 |
|
516 |
if (is_object($account) && property_exists($account, 'uid') && $account->uid == 1) { |
517 |
$result['status'] = 'fail'; |
518 |
$result['error_description'] = 'can not provision drupal user 1'; |
519 |
return $result; // do not provision or synch user 1 |
520 |
} |
521 |
|
522 |
if ($account == FALSE || $account->uid == 0) { |
523 |
$result['status'] = 'fail'; |
524 |
$result['error_description'] = 'can not provision ldap user unless corresponding drupal account exists first.'; |
525 |
return $result; |
526 |
} |
527 |
|
528 |
if (!$this->ldapEntryProvisionServer || !$this->ldapEntryProvisionServer) { |
529 |
$result['status'] = 'fail'; |
530 |
$result['error_description'] = 'no provisioning server enabled'; |
531 |
return $result; |
532 |
} |
533 |
|
534 |
$ldap_server = ldap_servers_get_servers($this->ldapEntryProvisionServer, NULL, TRUE); |
535 |
$params = array( |
536 |
'direction' => LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY, |
537 |
'prov_events' => array(LDAP_USER_EVENT_CREATE_LDAP_ENTRY), |
538 |
'module' => 'ldap_user', |
539 |
'function' => 'provisionLdapEntry', |
540 |
'include_count' => FALSE, |
541 |
); |
542 |
|
543 |
list($proposed_ldap_entry, $error) = $this->drupalUserToLdapEntry($account, $ldap_server, $params, $ldap_user); |
544 |
$proposed_dn = (is_array($proposed_ldap_entry) && isset($proposed_ldap_entry['dn']) && $proposed_ldap_entry['dn']) ? $proposed_ldap_entry['dn'] : NULL; |
545 |
$proposed_dn_lcase = drupal_strtolower($proposed_dn); |
546 |
$existing_ldap_entry = ($proposed_dn) ? $ldap_server->dnExists($proposed_dn, 'ldap_entry') : NULL; |
547 |
|
548 |
if ($error == LDAP_USER_PROV_RESULT_NO_PWD) { |
549 |
$result['status'] = 'fail'; |
550 |
$result['description'] = 'Can not provision ldap account without user provided password.'; |
551 |
$result['existing'] = $existing_ldap_entry; |
552 |
$result['proposed'] = $proposed_ldap_entry; |
553 |
$result['ldap_server'] = $ldap_server; |
554 |
} |
555 |
elseif (!$proposed_dn) { |
556 |
$result['status'] = 'fail'; |
557 |
$result['description'] = t('failed to derive dn and or mappings'); |
558 |
return $result; |
559 |
} |
560 |
elseif ($existing_ldap_entry) { |
561 |
$result['status'] = 'conflict'; |
562 |
$result['description'] = 'can not provision ldap entry because exists already'; |
563 |
$result['existing'] = $existing_ldap_entry; |
564 |
$result['proposed'] = $proposed_ldap_entry; |
565 |
$result['ldap_server'] = $ldap_server; |
566 |
} |
567 |
elseif ($test_query) { |
568 |
$result['status'] = 'fail'; |
569 |
$result['description'] = 'not created because flagged as test query'; |
570 |
$result['proposed'] = $proposed_ldap_entry; |
571 |
$result['ldap_server'] = $ldap_server; |
572 |
} |
573 |
else {
|
574 |
// stick $proposed_ldap_entry in $ldap_entries array for drupal_alter call
|
575 |
$ldap_entries = array($proposed_dn_lcase => $proposed_ldap_entry); |
576 |
$context = array( |
577 |
'action' => 'add', |
578 |
'corresponding_drupal_data' => array($proposed_dn_lcase => $account), |
579 |
'corresponding_drupal_data_type' => 'user', |
580 |
); |
581 |
drupal_alter('ldap_entry_pre_provision', $ldap_entries, $ldap_server, $context); |
582 |
// remove altered $proposed_ldap_entry from $ldap_entries array
|
583 |
$proposed_ldap_entry = $ldap_entries[$proposed_dn_lcase]; |
584 |
|
585 |
$ldap_entry_created = $ldap_server->createLdapEntry($proposed_ldap_entry, $proposed_dn); |
586 |
if ($ldap_entry_created) { |
587 |
module_invoke_all('ldap_entry_post_provision', $ldap_entries, $ldap_server, $context); |
588 |
$result['status'] = 'success'; |
589 |
$result['description'] = 'ldap account created'; |
590 |
$result['proposed'] = $proposed_ldap_entry; |
591 |
$result['created'] = $ldap_entry_created; |
592 |
$result['ldap_server'] = $ldap_server; |
593 |
|
594 |
// need to store <sid>|<dn> in ldap_user_prov_entries field, which may contain more than one
|
595 |
$ldap_user_prov_entry = $ldap_server->sid . '|' . $proposed_ldap_entry['dn']; |
596 |
if (!isset($user_entity->ldap_user_prov_entries[LANGUAGE_NONE])) { |
597 |
$user_entity->ldap_user_prov_entries = array(LANGUAGE_NONE => array()); |
598 |
} |
599 |
$ldap_user_prov_entry_exists = FALSE; |
600 |
foreach ($user_entity->ldap_user_prov_entries[LANGUAGE_NONE] as $i => $field_value_instance) { |
601 |
if ($field_value_instance == $ldap_user_prov_entry) { |
602 |
$ldap_user_prov_entry_exists = TRUE; |
603 |
} |
604 |
} |
605 |
if (!$ldap_user_prov_entry_exists) { |
606 |
$user_entity->ldap_user_prov_entries[LANGUAGE_NONE][] = array( |
607 |
'value' => $ldap_user_prov_entry, |
608 |
); |
609 |
|
610 |
// Save the field without calling user_save()
|
611 |
field_attach_presave('user', $user_entity); |
612 |
field_attach_update('user', $user_entity); |
613 |
} |
614 |
|
615 |
} |
616 |
else {
|
617 |
$result['status'] = 'fail'; |
618 |
$result['proposed'] = $proposed_ldap_entry; |
619 |
$result['created'] = $ldap_entry_created; |
620 |
$result['ldap_server'] = $ldap_server; |
621 |
$result['existing'] = NULL; |
622 |
} |
623 |
} |
624 |
|
625 |
$tokens = array( |
626 |
'%dn' => isset($result['proposed']['dn']) ? $result['proposed']['dn'] : NULL, |
627 |
'%sid' => (isset($result['ldap_server']) && $result['ldap_server']) ? $result['ldap_server']->sid : 0, |
628 |
'%username' => @$account->name, |
629 |
'%uid' => @$account->uid, |
630 |
'%description' => @$result['description'], |
631 |
); |
632 |
if (!$test_query && isset($result['status'])) { |
633 |
if ($result['status'] == 'success') { |
634 |
if ($this->detailedWatchdog) { |
635 |
watchdog('ldap_user', 'LDAP entry on server %sid created dn=%dn. %description. username=%username, uid=%uid', $tokens, WATCHDOG_INFO); |
636 |
} |
637 |
} |
638 |
elseif ($result['status'] == 'conflict') { |
639 |
if ($this->detailedWatchdog) { |
640 |
watchdog('ldap_user', 'LDAP entry on server %sid not created because of existing ldap entry. %description. username=%username, uid=%uid', $tokens, WATCHDOG_WARNING); |
641 |
} |
642 |
} |
643 |
elseif ($result['status'] == 'fail') { |
644 |
watchdog('ldap_user', 'LDAP entry on server %sid not created because error. %description. username=%username, uid=%uid', $tokens, WATCHDOG_ERROR); |
645 |
} |
646 |
} |
647 |
return $result; |
648 |
} |
649 |
|
650 |
|
651 |
/**
|
652 |
* given a drupal account, synch to related ldap entry
|
653 |
*
|
654 |
* @param drupal user object $account. Drupal user object
|
655 |
* @param array $user_edit. Edit array for user_save. generally null unless user account is being created or modified in same synching
|
656 |
* @param array $ldap_user. current ldap data of user. @see README.developers.txt for structure
|
657 |
*
|
658 |
* @return TRUE on success or FALSE on fail.
|
659 |
*/
|
660 |
|
661 |
public function synchToLdapEntry($account, $user_edit = NULL, $ldap_user = array(), $test_query = FALSE) { |
662 |
|
663 |
if (is_object($account) && property_exists($account, 'uid') && $account->uid == 1) { |
664 |
return FALSE; // do not provision or synch user 1 |
665 |
} |
666 |
|
667 |
$watchdog_tokens = array(); |
668 |
$result = FALSE; |
669 |
$proposed_ldap_entry = FALSE; |
670 |
|
671 |
if ($this->ldapEntryProvisionServer) { |
672 |
$ldap_server = ldap_servers_get_servers($this->ldapEntryProvisionServer, NULL, TRUE); |
673 |
|
674 |
$params = array( |
675 |
'direction' => LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY, |
676 |
'prov_events' => array(LDAP_USER_EVENT_SYNCH_TO_LDAP_ENTRY), |
677 |
'module' => 'ldap_user', |
678 |
'function' => 'synchToLdapEntry', |
679 |
'include_count' => FALSE, |
680 |
); |
681 |
|
682 |
list($proposed_ldap_entry, $error) = $this->drupalUserToLdapEntry($account, $ldap_server, $params, $ldap_user); |
683 |
if ($error != LDAP_USER_PROV_RESULT_NO_ERROR) { |
684 |
$result = FALSE; |
685 |
} |
686 |
elseif (is_array($proposed_ldap_entry) && isset($proposed_ldap_entry['dn'])) { |
687 |
$existing_ldap_entry = $ldap_server->dnExists($proposed_ldap_entry['dn'], 'ldap_entry'); |
688 |
$attributes = array(); // this array represents attributes to be modified; not comprehensive list of attributes |
689 |
foreach ($proposed_ldap_entry as $attr_name => $attr_values) { |
690 |
if ($attr_name != 'dn') { |
691 |
if (isset($attr_values['count'])) { |
692 |
unset($attr_values['count']); |
693 |
} |
694 |
if (count($attr_values) == 1) { |
695 |
$attributes[$attr_name] = $attr_values[0]; |
696 |
} |
697 |
else {
|
698 |
$attributes[$attr_name] = $attr_values; |
699 |
} |
700 |
} |
701 |
} |
702 |
|
703 |
if ($test_query) { |
704 |
$proposed_ldap_entry = $attributes; |
705 |
$result = array( |
706 |
'proposed' => $proposed_ldap_entry, |
707 |
'server' => $ldap_server, |
708 |
); |
709 |
} |
710 |
else {
|
711 |
// stick $proposed_ldap_entry in $ldap_entries array for drupal_alter call
|
712 |
$proposed_dn_lcase = drupal_strtolower($proposed_ldap_entry['dn']); |
713 |
$ldap_entries = array($proposed_dn_lcase => $attributes); |
714 |
$context = array( |
715 |
'action' => 'update', |
716 |
'corresponding_drupal_data' => array($proposed_dn_lcase => $attributes), |
717 |
'corresponding_drupal_data_type' => 'user', |
718 |
); |
719 |
drupal_alter('ldap_entry_pre_provision', $ldap_entries, $ldap_server, $context); |
720 |
// remove altered $proposed_ldap_entry from $ldap_entries array
|
721 |
$attributes = $ldap_entries[$proposed_dn_lcase]; |
722 |
$result = $ldap_server->modifyLdapEntry($proposed_ldap_entry['dn'], $attributes); |
723 |
if ($result) { // success |
724 |
module_invoke_all('ldap_entry_post_provision', $ldap_entries, $ldap_server, $context); |
725 |
} |
726 |
} |
727 |
} |
728 |
else { // failed to get acceptable proposed ldap entry |
729 |
$result = FALSE; |
730 |
} |
731 |
} |
732 |
|
733 |
$tokens = array( |
734 |
'%dn' => isset($proposed_ldap_entry['dn']) ? $proposed_ldap_entry['dn'] : NULL, |
735 |
'%sid' => $this->ldapEntryProvisionServer, |
736 |
'%username' => $account->name, |
737 |
'%uid' => ($test_query || !property_exists($account, 'uid')) ? '' : $account->uid, |
738 |
); |
739 |
|
740 |
if ($result) { |
741 |
watchdog('ldap_user', 'LDAP entry on server %sid synched dn=%dn. username=%username, uid=%uid', $tokens, WATCHDOG_INFO); |
742 |
} |
743 |
else {
|
744 |
watchdog('ldap_user', 'LDAP entry on server %sid not synched because error. username=%username, uid=%uid', $tokens, WATCHDOG_ERROR); |
745 |
} |
746 |
|
747 |
return $result; |
748 |
|
749 |
} |
750 |
|
751 |
/**
|
752 |
* given a drupal account, query ldap and get all user fields and create user account
|
753 |
*
|
754 |
* @param array $account drupal account array with minimum of name
|
755 |
* @param array $user_edit drupal edit array in form user_save($account, $user_edit) would take,
|
756 |
* generally empty unless overriding synchToDrupalAccount derived values
|
757 |
* @param array $ldap_user as user's ldap entry. passed to avoid requerying ldap in cases where already present
|
758 |
* @param boolean $save indicating if drupal user should be saved. generally depends on where function is called from.
|
759 |
*
|
760 |
* @return result of user_save() function is $save is true, otherwise return TRUE
|
761 |
* $user_edit data returned by reference
|
762 |
*
|
763 |
*/
|
764 |
public function synchToDrupalAccount($drupal_user, &$user_edit, $prov_event = LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER, $ldap_user = NULL, $save = FALSE) { |
765 |
|
766 |
$debug = array( |
767 |
'account' => $drupal_user, |
768 |
'user_edit' => $user_edit, |
769 |
'ldap_user' => $ldap_user, |
770 |
); |
771 |
|
772 |
if (
|
773 |
(!$ldap_user && !isset($drupal_user->name)) || |
774 |
(!$drupal_user && $save) || |
775 |
($ldap_user && !isset($ldap_user['sid'])) |
776 |
) { |
777 |
// should throw watchdog error also
|
778 |
return FALSE; |
779 |
} |
780 |
|
781 |
if (!$ldap_user && $this->drupalAcctProvisionServer) { |
782 |
$ldap_user = ldap_servers_get_user_ldap_data($drupal_user->name, $this->drupalAcctProvisionServer, 'ldap_user_prov_to_drupal'); |
783 |
} |
784 |
|
785 |
if (!$ldap_user) { |
786 |
return FALSE; |
787 |
} |
788 |
|
789 |
if ($this->drupalAcctProvisionServer) { |
790 |
$ldap_server = ldap_servers_get_servers($this->drupalAcctProvisionServer, NULL, TRUE); |
791 |
$this->entryToUserEdit($ldap_user, $user_edit, $ldap_server, LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, array($prov_event)); |
792 |
} |
793 |
|
794 |
if ($save) { |
795 |
$account = user_load($drupal_user->uid); |
796 |
$result = user_save($account, $user_edit, 'ldap_user'); |
797 |
return $result; |
798 |
} |
799 |
else {
|
800 |
return TRUE; |
801 |
} |
802 |
} |
803 |
|
804 |
|
805 |
/**
|
806 |
* given a drupal account, delete user account
|
807 |
*
|
808 |
* @param string $username drupal account name
|
809 |
* @return TRUE or FALSE. FALSE indicates failed or action not enabled in ldap user configuration
|
810 |
*/
|
811 |
public function deleteDrupalAccount($username) { |
812 |
$user = user_load_by_name($username); |
813 |
if (is_object($user)) { |
814 |
user_delete($user->uid);
|
815 |
return TRUE; |
816 |
} |
817 |
else {
|
818 |
return FALSE; |
819 |
} |
820 |
} |
821 |
|
822 |
/**
|
823 |
* given a drupal account, find the related ldap entry.
|
824 |
*
|
825 |
* @param drupal user object $account
|
826 |
*
|
827 |
* @return FALSE or ldap entry
|
828 |
*/
|
829 |
public function getProvisionRelatedLdapEntry($account, $prov_events = NULL) { |
830 |
if (!$prov_events) { |
831 |
$prov_events = ldap_user_all_events();
|
832 |
} |
833 |
$sid = $this->ldapEntryProvisionServer; // |
834 |
if (!$sid) { |
835 |
return FALSE; |
836 |
} |
837 |
// $user_entity->ldap_user_prov_entries,
|
838 |
$ldap_server = ldap_servers_get_servers($sid, NULL, TRUE); |
839 |
$params = array( |
840 |
'direction' => LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY, |
841 |
'prov_events' => $prov_events, |
842 |
'module' => 'ldap_user', |
843 |
'function' => 'getProvisionRelatedLdapEntry', |
844 |
'include_count' => FALSE, |
845 |
); |
846 |
list($proposed_ldap_entry, $error) = $this->drupalUserToLdapEntry($account, $ldap_server, $params); |
847 |
if (!(is_array($proposed_ldap_entry) && isset($proposed_ldap_entry['dn']) && $proposed_ldap_entry['dn'])) { |
848 |
return FALSE; |
849 |
} |
850 |
$ldap_entry = $ldap_server->dnExists($proposed_ldap_entry['dn'], 'ldap_entry', array()); |
851 |
return $ldap_entry; |
852 |
|
853 |
} |
854 |
|
855 |
/**
|
856 |
* given a drupal account, delete ldap entry that was provisioned based on it
|
857 |
* normally this will be 0 or 1 entry, but the ldap_user_provisioned_ldap_entries
|
858 |
* field attached to the user entity track each ldap entry provisioned
|
859 |
*
|
860 |
* @param object $account drupal account
|
861 |
* @return TRUE or FALSE. FALSE indicates failed or action not enabled in ldap user configuration
|
862 |
*/
|
863 |
public function deleteProvisionedLdapEntries($account) { |
864 |
// determine server that is associated with user
|
865 |
|
866 |
$boolean_result = FALSE; |
867 |
if (isset($account->ldap_user_prov_entries[LANGUAGE_NONE][0])) { |
868 |
foreach ($account->ldap_user_prov_entries[LANGUAGE_NONE] as $i => $field_instance) { |
869 |
$parts = explode('|', $field_instance['value']); |
870 |
if (count($parts) == 2) { |
871 |
|
872 |
list($sid, $dn) = $parts; |
873 |
$ldap_server = ldap_servers_get_servers($sid, NULL, TRUE); |
874 |
if (is_object($ldap_server) && $dn) { |
875 |
$boolean_result = $ldap_server->delete($dn); |
876 |
$tokens = array('%sid' => $sid, '%dn' => $dn, '%username' => $account->name, '%uid' => $account->uid); |
877 |
if ($boolean_result) { |
878 |
watchdog('ldap_user', 'LDAP entry on server %sid deleted dn=%dn. username=%username, uid=%uid', $tokens, WATCHDOG_INFO); |
879 |
} |
880 |
else {
|
881 |
watchdog('ldap_user', 'LDAP entry on server %sid not deleted because error. username=%username, uid=%uid', $tokens, WATCHDOG_ERROR); |
882 |
} |
883 |
} |
884 |
else {
|
885 |
$boolean_result = FALSE; |
886 |
} |
887 |
} |
888 |
} |
889 |
} |
890 |
return $boolean_result; |
891 |
|
892 |
} |
893 |
|
894 |
/**
|
895 |
* populate ldap entry array for provisioning
|
896 |
*
|
897 |
* @param array $account drupal account
|
898 |
* @param object $ldap_server
|
899 |
* @param array $ldap_user ldap entry of user, returned by reference
|
900 |
* @param array $params with the following key values:
|
901 |
* 'ldap_context' =>
|
902 |
'module' => module calling function, e.g. 'ldap_user'
|
903 |
'function' => function calling function, e.g. 'provisionLdapEntry'
|
904 |
'include_count' => should 'count' array key be included
|
905 |
'direction' => LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY || LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER
|
906 |
*
|
907 |
* @return array(ldap entry, $result) in ldap extension array format.!THIS IS NOT THE ACTUAL LDAP ENTRY
|
908 |
*/
|
909 |
|
910 |
function drupalUserToLdapEntry($account, $ldap_server, $params, $ldap_user_entry = NULL) { |
911 |
$provision = (isset($params['function']) && $params['function'] == 'provisionLdapEntry'); |
912 |
$result = LDAP_USER_PROV_RESULT_NO_ERROR; |
913 |
if (!$ldap_user_entry) { |
914 |
$ldap_user_entry = array(); |
915 |
} |
916 |
|
917 |
if (!is_object($account) || !is_object($ldap_server)) { |
918 |
return array(NULL, LDAP_USER_PROV_RESULT_BAD_PARAMS); |
919 |
} |
920 |
$watchdog_tokens = array( |
921 |
'%drupal_username' => $account->name, |
922 |
); |
923 |
$include_count = (isset($params['include_count']) && $params['include_count']); |
924 |
|
925 |
$direction = isset($params['direction']) ? $params['direction'] : LDAP_USER_PROV_DIRECTION_ALL; |
926 |
$prov_events = empty($params['prov_events']) ? ldap_user_all_events() : $params['prov_events']; |
927 |
|
928 |
$mappings = $this->getSynchMappings($direction, $prov_events); |
929 |
foreach ($mappings as $field_key => $field_detail) { |
930 |
list($ldap_attr_name, $ordinal, $conversion) = ldap_servers_token_extract_parts($field_key, TRUE); //trim($field_key, '[]'); |
931 |
$ordinal = (!$ordinal) ? 0 : $ordinal; |
932 |
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]) ) { |
933 |
continue; // don't override values passed in; |
934 |
} |
935 |
|
936 |
$synched = $this->isSynched($field_key, $params['prov_events'], LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY); |
937 |
if ($synched) { |
938 |
$token = ($field_detail['user_attr'] == 'user_tokens') ? $field_detail['user_tokens'] : $field_detail['user_attr']; |
939 |
$value = ldap_servers_token_replace($account, $token, 'user_account'); |
940 |
|
941 |
if (substr($token, 0, 10) == '[password.' && (!$value || $value == $token)) { // deal with empty/unresolved password |
942 |
if (!$provision) { |
943 |
continue; //don't overwrite password on synch if no value provided |
944 |
} |
945 |
} |
946 |
|
947 |
if ($ldap_attr_name == 'dn' && $value) { |
948 |
$ldap_user_entry['dn'] = $value; |
949 |
} |
950 |
elseif ($value) { |
951 |
if (!isset($ldap_user_entry[$ldap_attr_name]) || !is_array($ldap_user_entry[$ldap_attr_name])) { |
952 |
$ldap_user_entry[$ldap_attr_name] = array(); |
953 |
} |
954 |
$ldap_user_entry[$ldap_attr_name][$ordinal] = $value; |
955 |
if ($include_count) { |
956 |
$ldap_user_entry[$ldap_attr_name]['count'] = count($ldap_user_entry[$ldap_attr_name]); |
957 |
} |
958 |
|
959 |
} |
960 |
|
961 |
} |
962 |
|
963 |
} |
964 |
|
965 |
/**
|
966 |
* 4. call drupal_alter() to allow other modules to alter $ldap_user
|
967 |
*/
|
968 |
|
969 |
drupal_alter('ldap_entry', $ldap_user_entry, $params); |
970 |
|
971 |
return array($ldap_user_entry, $result); |
972 |
|
973 |
} |
974 |
|
975 |
|
976 |
|
977 |
/**
|
978 |
* given a drupal account, query ldap and get all user fields and save user account
|
979 |
* (note: parameters are in odd order to match synchDrupalAccount handle)
|
980 |
*
|
981 |
* @param array $account drupal account object or null
|
982 |
* @param array $user_edit drupal edit array in form user_save($account, $user_edit) would take.
|
983 |
* @param array $ldap_user as user's ldap entry. passed to avoid requerying ldap in cases where already present
|
984 |
* @param boolean $save indicating if drupal user should be saved. generally depends on where function is called from and if the
|
985 |
*
|
986 |
* @return result of user_save() function is $save is true, otherwise return TRUE on success or FALSE on any problem
|
987 |
* $user_edit data returned by reference
|
988 |
*
|
989 |
*/
|
990 |
|
991 |
public function provisionDrupalAccount($account = FALSE, &$user_edit, $ldap_user = NULL, $save = TRUE) { |
992 |
|
993 |
$watchdog_tokens = array(); |
994 |
/**
|
995 |
* @todo
|
996 |
* -- add error catching for conflicts, conflicts should be checked before calling this function.
|
997 |
*
|
998 |
*/
|
999 |
|
1000 |
if (!$account) { |
1001 |
$account = new stdClass(); |
1002 |
} |
1003 |
$account->is_new = TRUE; |
1004 |
|
1005 |
if (!$ldap_user && !isset($user_edit['name'])) { |
1006 |
return FALSE; |
1007 |
} |
1008 |
|
1009 |
if (!$ldap_user) { |
1010 |
$watchdog_tokens['%username'] = $user_edit['name']; |
1011 |
if ($this->drupalAcctProvisionServer) { |
1012 |
$ldap_user = ldap_servers_get_user_ldap_data($user_edit['name'], $this->drupalAcctProvisionServer, 'ldap_user_prov_to_drupal'); |
1013 |
} |
1014 |
if (!$ldap_user) { |
1015 |
if ($this->detailedWatchdog) { |
1016 |
watchdog('ldap_user', '%username : failed to find associated ldap entry for username in provision.', $watchdog_tokens, WATCHDOG_DEBUG); |
1017 |
} |
1018 |
return FALSE; |
1019 |
} |
1020 |
} |
1021 |
|
1022 |
if (!isset($user_edit['name']) && isset($account->name)) { |
1023 |
$user_edit['name'] = $account->name; |
1024 |
$watchdog_tokens['%username'] = $user_edit['name']; |
1025 |
} |
1026 |
//When using the multi-domain last authentication option
|
1027 |
//$ldap_server breaks beacause $this->drupalAcctProvisionServer is set on LDAP_USER_AUTH_SERVER_SID
|
1028 |
//So we need to check it's not the case before using ldap_servers_get_servers
|
1029 |
if ($this->drupalAcctProvisionServer && $this->drupalAcctProvisionServer != LDAP_USER_AUTH_SERVER_SID) { |
1030 |
|
1031 |
$ldap_server = ldap_servers_get_servers($this->drupalAcctProvisionServer, 'enabled', TRUE); // $ldap_user['sid'] |
1032 |
|
1033 |
$params = array( |
1034 |
'account' => $account, |
1035 |
'user_edit' => $user_edit, |
1036 |
'prov_event' => LDAP_USER_EVENT_CREATE_DRUPAL_USER, |
1037 |
'module' => 'ldap_user', |
1038 |
'function' => 'provisionDrupalAccount', |
1039 |
'direction' => LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, |
1040 |
); |
1041 |
|
1042 |
drupal_alter('ldap_entry', $ldap_user, $params); |
1043 |
|
1044 |
// look for existing drupal account with same puid. if so update username and attempt to synch in current context
|
1045 |
$puid = $ldap_server->userPuidFromLdapEntry($ldap_user['attr']); |
1046 |
$account2 = ($puid) ? $ldap_server->userUserEntityFromPuid($puid) : FALSE; |
1047 |
|
1048 |
if ($account2) { // synch drupal account, since drupal account exists |
1049 |
// 1. correct username and authmap
|
1050 |
$this->entryToUserEdit($ldap_user, $user_edit, $ldap_server, LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, array(LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER)); |
1051 |
$account = user_save($account2, $user_edit, 'ldap_user'); |
1052 |
user_set_authmaps($account, array("authname_ldap_user" => $user_edit['name'])); |
1053 |
// 2. attempt synch if appropriate for current context
|
1054 |
if ($account) { |
1055 |
$account = $this->synchToDrupalAccount($account, $user_edit, LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER, $ldap_user, TRUE); |
1056 |
} |
1057 |
return $account; |
1058 |
} |
1059 |
else { // create drupal account |
1060 |
$this->entryToUserEdit($ldap_user, $user_edit, $ldap_server, LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, array(LDAP_USER_EVENT_CREATE_DRUPAL_USER)); |
1061 |
if ($save) { |
1062 |
$watchdog_tokens = array('%drupal_username' => $user_edit['name']); |
1063 |
if (empty($user_edit['name'])) { |
1064 |
drupal_set_message(t('User account creation failed because of invalid, empty derived Drupal username.'), 'error'); |
1065 |
watchdog('ldap_user',
|
1066 |
'Failed to create Drupal account %drupal_username because drupal username could not be derived.',
|
1067 |
$watchdog_tokens,
|
1068 |
WATCHDOG_ERROR
|
1069 |
); |
1070 |
return FALSE; |
1071 |
} |
1072 |
if (!isset($user_edit['mail']) || !$user_edit['mail']) { |
1073 |
drupal_set_message(t('User account creation failed because of invalid, empty derived email address.'), 'error'); |
1074 |
watchdog('ldap_user',
|
1075 |
'Failed to create Drupal account %drupal_username because email address could not be derived by LDAP User module',
|
1076 |
$watchdog_tokens,
|
1077 |
WATCHDOG_ERROR
|
1078 |
); |
1079 |
return FALSE; |
1080 |
} |
1081 |
if ($account_with_same_email = user_load_by_mail($user_edit['mail'])) { |
1082 |
$watchdog_tokens['%email'] = $user_edit['mail']; |
1083 |
$watchdog_tokens['%duplicate_name'] = $account_with_same_email->name; |
1084 |
watchdog('ldap_user', 'LDAP user %drupal_username has email address |
1085 |
(%email) conflict with a drupal user %duplicate_name', $watchdog_tokens, WATCHDOG_ERROR); |
1086 |
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'); |
1087 |
return FALSE; |
1088 |
} |
1089 |
$account = user_save(NULL, $user_edit, 'ldap_user'); |
1090 |
if (!$account) { |
1091 |
drupal_set_message(t('User account creation failed because of system problems.'), 'error'); |
1092 |
} |
1093 |
else {
|
1094 |
user_set_authmaps($account, array('authname_ldap_user' => $account->name)); |
1095 |
ldap_user_ldap_provision_semaphore('drupal_created', 'set', $account->name); |
1096 |
} |
1097 |
return $account; |
1098 |
} |
1099 |
return TRUE; |
1100 |
} |
1101 |
} |
1102 |
} |
1103 |
|
1104 |
/**
|
1105 |
* set ldap associations of a drupal account by altering user fields
|
1106 |
*
|
1107 |
* @param string $drupal_username
|
1108 |
*
|
1109 |
* @return boolean TRUE on success, FALSE on error or failure because of invalid user or ldap accounts
|
1110 |
*
|
1111 |
*/
|
1112 |
function ldapAssociateDrupalAccount($drupal_username) { |
1113 |
|
1114 |
if ($this->drupalAcctProvisionServer) { |
1115 |
$prov_events = array(LDAP_USER_EVENT_LDAP_ASSOCIATE_DRUPAL_ACCT); |
1116 |
$ldap_server = ldap_servers_get_servers($this->drupalAcctProvisionServer, 'enabled', TRUE); // $ldap_user['sid'] |
1117 |
$account = user_load_by_name($drupal_username); |
1118 |
$ldap_user = ldap_servers_get_user_ldap_data($drupal_username, $this->drupalAcctProvisionServer, 'ldap_user_prov_to_drupal'); |
1119 |
if (!$account) { |
1120 |
watchdog( |
1121 |
'ldap_user',
|
1122 |
'Failed to LDAP associate drupal account %drupal_username because account not found',
|
1123 |
array('%drupal_username' => $drupal_username), |
1124 |
WATCHDOG_ERROR
|
1125 |
); |
1126 |
return FALSE; |
1127 |
} |
1128 |
elseif (!$ldap_user) { |
1129 |
watchdog( |
1130 |
'ldap_user',
|
1131 |
'Failed to LDAP associate drupal account %drupal_username because corresponding LDAP entry not found',
|
1132 |
array('%drupal_username' => $drupal_username), |
1133 |
WATCHDOG_ERROR
|
1134 |
); |
1135 |
return FALSE; |
1136 |
} |
1137 |
else {
|
1138 |
$user_edit = array(); |
1139 |
$user_edit['data']['ldap_user']['init'] = array( |
1140 |
'sid' => $ldap_user['sid'], |
1141 |
'dn' => $ldap_user['dn'], |
1142 |
'mail' => $account->mail, |
1143 |
); |
1144 |
$ldap_user_puid = $ldap_server->userPuidFromLdapEntry($ldap_user['attr']); |
1145 |
if ($ldap_user_puid) { |
1146 |
$user_edit['ldap_user_puid'][LANGUAGE_NONE][0]['value'] = $ldap_user_puid; // |
1147 |
} |
1148 |
$user_edit['ldap_user_puid_property'][LANGUAGE_NONE][0]['value'] = $ldap_server->unique_persistent_attr; |
1149 |
$user_edit['ldap_user_puid_sid'][LANGUAGE_NONE][0]['value'] = $ldap_server->sid; |
1150 |
$user_edit['ldap_user_current_dn'][LANGUAGE_NONE][0]['value'] = $ldap_user['dn']; |
1151 |
$account = user_save($account, $user_edit, 'ldap_user'); |
1152 |
return (boolean)$account; |
1153 |
} |
1154 |
} |
1155 |
else {
|
1156 |
return FALSE; |
1157 |
} |
1158 |
} |
1159 |
|
1160 |
/** populate $user edit array (used in hook_user_save, hook_user_update, etc)
|
1161 |
* ... should not assume all attribues are present in ldap entry
|
1162 |
*
|
1163 |
* @param array ldap entry $ldap_user
|
1164 |
* @param array $edit see hook_user_save, hook_user_update, etc
|
1165 |
* @param object $ldap_server
|
1166 |
* @param enum $direction
|
1167 |
* @param array $prov_events
|
1168 |
*
|
1169 |
*/
|
1170 |
|
1171 |
function entryToUserEdit($ldap_user, &$edit, $ldap_server, $direction = LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, $prov_events = NULL) { |
1172 |
|
1173 |
// need array of user fields and which direction and when they should be synched.
|
1174 |
if (!$prov_events) { |
1175 |
$prov_events = ldap_user_all_events();
|
1176 |
} |
1177 |
$mail_synched = $this->isSynched('[property.mail]', $prov_events, $direction); |
1178 |
if (!isset($edit['mail']) && $mail_synched) { |
1179 |
$derived_mail = $ldap_server->userEmailFromLdapEntry($ldap_user['attr']); |
1180 |
if ($derived_mail) { |
1181 |
$edit['mail'] = $derived_mail; |
1182 |
} |
1183 |
} |
1184 |
|
1185 |
$drupal_username = $ldap_server->userUsernameFromLdapEntry($ldap_user['attr']); |
1186 |
if ($this->isSynched('[property.picture]', $prov_events, $direction)){ |
1187 |
|
1188 |
$picture = $ldap_server->userPictureFromLdapEntry($ldap_user['attr'], $drupal_username); |
1189 |
|
1190 |
if ($picture){ |
1191 |
$edit['picture'] = $picture; |
1192 |
if(isset($picture->md5Sum)){ |
1193 |
$edit['data']['ldap_user']['init']['thumb5md'] = $picture->md5Sum; |
1194 |
} |
1195 |
} |
1196 |
} |
1197 |
|
1198 |
if ($this->isSynched('[property.name]', $prov_events, $direction) && !isset($edit['name']) && $drupal_username) { |
1199 |
$edit['name'] = $drupal_username; |
1200 |
} |
1201 |
|
1202 |
if ($direction == LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER && in_array(LDAP_USER_EVENT_CREATE_DRUPAL_USER, $prov_events)) { |
1203 |
$edit['mail'] = isset($edit['mail']) ? $edit['mail'] : $ldap_user['mail']; |
1204 |
if (!isset($edit['pass'])) { |
1205 |
$edit['pass'] = user_password(20); |
1206 |
watchdog('ldap_user', '20 character random password generated for the %username account that has been created.', array('%username' => $drupal_username), WATCHDOG_INFO); |
1207 |
} |
1208 |
$edit['init'] = isset($edit['init']) ? $edit['init'] : $edit['mail']; |
1209 |
$edit['status'] = isset($edit['status']) ? $edit['status'] : 1; |
1210 |
$edit['signature'] = isset($edit['signature']) ? $edit['signature'] : ''; |
1211 |
|
1212 |
$edit['data']['ldap_user']['init'] = array( |
1213 |
'sid' => $ldap_user['sid'], |
1214 |
'dn' => $ldap_user['dn'], |
1215 |
'mail' => $edit['mail'], |
1216 |
); |
1217 |
} |
1218 |
|
1219 |
/*
|
1220 |
* Make sure the user account has the latest ldap_user settings
|
1221 |
* when syncing the profile.
|
1222 |
*/
|
1223 |
if ($direction == LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER && in_array(LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER, $prov_events)) { |
1224 |
$edit['data']['ldap_user']['init'] = array( |
1225 |
'sid' => $ldap_user['sid'], |
1226 |
'dn' => $ldap_user['dn'], |
1227 |
'mail' => isset($edit['mail']) && !empty($edit['mail']) ? $edit['mail'] : $ldap_user['mail'], |
1228 |
); |
1229 |
} |
1230 |
|
1231 |
/**
|
1232 |
* basic $user ldap fields
|
1233 |
*/
|
1234 |
if ($this->isSynched('[field.ldap_user_puid]', $prov_events, $direction)) { |
1235 |
$ldap_user_puid = $ldap_server->userPuidFromLdapEntry($ldap_user['attr']); |
1236 |
if ($ldap_user_puid) { |
1237 |
$edit['ldap_user_puid'][LANGUAGE_NONE][0]['value'] = $ldap_user_puid; // |
1238 |
} |
1239 |
} |
1240 |
if ($this->isSynched('[field.ldap_user_puid_property]', $prov_events, $direction)) { |
1241 |
$edit['ldap_user_puid_property'][LANGUAGE_NONE][0]['value'] = $ldap_server->unique_persistent_attr; |
1242 |
} |
1243 |
if ($this->isSynched('[field.ldap_user_puid_sid]', $prov_events, $direction)) { |
1244 |
$edit['ldap_user_puid_sid'][LANGUAGE_NONE][0]['value'] = $ldap_server->sid; |
1245 |
} |
1246 |
if ($this->isSynched('[field.ldap_user_current_dn]', $prov_events, $direction)) { |
1247 |
$edit['ldap_user_current_dn'][LANGUAGE_NONE][0]['value'] = $ldap_user['dn']; |
1248 |
} |
1249 |
|
1250 |
// Get any additional mappings.
|
1251 |
$mappings = $this->getSynchMappings($direction, $prov_events); |
1252 |
|
1253 |
// Loop over the mappings.
|
1254 |
foreach ($mappings as $user_attr_key => $field_detail) { |
1255 |
|
1256 |
// Make sure this mapping is relevant to the sync context.
|
1257 |
if (!$this->isSynched($user_attr_key, $prov_events, $direction)) { |
1258 |
continue;
|
1259 |
} |
1260 |
/**
|
1261 |
* if "convert from binary is selected" and no particular method is in token,
|
1262 |
* default to ldap_servers_binary() function
|
1263 |
*/
|
1264 |
if ($field_detail['convert'] && strpos($field_detail['ldap_attr'], ';') === FALSE) { |
1265 |
$field_detail['ldap_attr'] = str_replace(']', ';binary]', $field_detail['ldap_attr']); |
1266 |
} |
1267 |
$value = ldap_servers_token_replace($ldap_user['attr'], $field_detail['ldap_attr'], 'ldap_entry'); |
1268 |
list($value_type, $value_name, $value_instance) = ldap_servers_parse_user_attr_name($user_attr_key); |
1269 |
|
1270 |
// $value_instance not used, may have future use case
|
1271 |
|
1272 |
// Are we dealing with a field?
|
1273 |
if ($value_type == 'field') { |
1274 |
// Field api field - first we get the field.
|
1275 |
$field = field_info_field($value_name); |
1276 |
// Then the columns for the field in the schema.
|
1277 |
$columns = array_keys($field['columns']); |
1278 |
// Then we convert the value into an array if it's scalar.
|
1279 |
$values = $field['cardinality'] == 1 ? array($value) : (array) $value; |
1280 |
|
1281 |
$items = array(); |
1282 |
// Loop over the values and set them in our $items array.
|
1283 |
foreach ($values as $delta => $value) { |
1284 |
if (isset($value)) { |
1285 |
// We set the first column value only, this is consistent with
|
1286 |
// the Entity Api (@see entity_metadata_field_property_set).
|
1287 |
$items[$delta][$columns[0]] = $value; |
1288 |
} |
1289 |
} |
1290 |
// Add them to our edited item.
|
1291 |
$edit[$value_name][LANGUAGE_NONE] = $items; |
1292 |
} |
1293 |
elseif ($value_type == 'property') { |
1294 |
// Straight property.
|
1295 |
$edit[$value_name] = $value; |
1296 |
} |
1297 |
} |
1298 |
|
1299 |
// Allow other modules to have a say.
|
1300 |
|
1301 |
drupal_alter('ldap_user_edit_user', $edit, $ldap_user, $ldap_server, $prov_events); |
1302 |
if (isset($edit['name']) && $edit['name'] == '') { // don't let empty 'name' value pass for user |
1303 |
unset($edit['name']); |
1304 |
} |
1305 |
|
1306 |
} |
1307 |
/**
|
1308 |
* given configuration of synching, determine is a given synch should occur
|
1309 |
*
|
1310 |
* @param string $attr_token e.g. [property.mail], [field.ldap_user_puid_property]
|
1311 |
* @param object $ldap_server
|
1312 |
* @param array $prov_events e.g. array(LDAP_USER_EVENT_CREATE_DRUPAL_USER). typically array with 1 element
|
1313 |
* @param scalar $direction LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER or LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY
|
1314 |
*/
|
1315 |
|
1316 |
public function isSynched($attr_token, $prov_events, $direction) { |
1317 |
$result = (boolean)( |
1318 |
isset($this->synchMapping[$direction][$attr_token]['prov_events']) && |
1319 |
count(array_intersect($prov_events, $this->synchMapping[$direction][$attr_token]['prov_events'])) |
1320 |
); |
1321 |
if (!$result) { |
1322 |
if (isset($this->synchMapping[$direction][$attr_token])) { |
1323 |
} |
1324 |
else {
|
1325 |
} |
1326 |
} |
1327 |
return $result; |
1328 |
} |
1329 |
|
1330 |
} |