function _ldapdata_user_load in LDAP integration 6
Implements hook_user() load operation.
Parameters
Object $account User being loaded:
boolean $sync If true, force the user to be synced.:
Array $newentry ldapsync generated users to update info from.:
4 calls to _ldapdata_user_load()
- ldapdata_user in ./
ldapdata.module - Implements hook_user().
- _ldapdata_user_form in ./
ldapdata.module - Implements hook_user() categories operation. Only used for editable LDAP attributes with no Drupal equivalents.
- _ldapdata_user_login in ./
ldapdata.module - Implements hook_user() login operation.
- _ldapsync_process_entry in ./
ldapsync.module - Take an ldap object entry and determine if there is an existing account or a new account needs to be created.
File
- ./
ldapdata.module, line 198 - ldapdata provides data maping against ldap server.
Code
function _ldapdata_user_load(&$account, $sync = FALSE, $newentry = NULL) {
global $user, $_ldapdata_ldap;
// Setup the global $_ldapdata_ldap object.
// NOTE: Other functions assume this function will always initialize this
if (!_ldapdata_init($account)) {
return;
}
// sync not forced and sync on login set or sync on page load set and it's not the current user.
if (!$sync && (LDAPDATA_SYNC == 0 || LDAPDATA_SYNC == 1 && $user->uid != $account->uid)) {
return;
}
static $accounts_synced = array();
if (isset($accounts_synced[$account->uid])) {
return;
}
// See http://drupal.org/node/91786 about user_node().
// User can be edited by the user or by other authorized users.
if (!isset($account->ldap_dn) || _ldapdata_ldap_info($account, 'mapping_type') == LDAPDATA_MAP_NOTHING) {
return;
}
$accounts_synced[$account->uid] = TRUE;
if (is_null($newentry)) {
$bind_info = _ldapdata_edition($account);
if (!$_ldapdata_ldap
->connect($bind_info['dn'], $bind_info['pass'])) {
watchdog('ldapdata', "User load: user %name's data could not be read in the LDAP directory", array(
'%name' => $account->name,
), WATCHDOG_WARNING);
return;
}
$entry = ldapauth_user_lookup_by_dn($_ldapdata_ldap, $account->ldap_dn, LDAPAUTH_SYNC_CONTEXT_UPDATE_DRUPAL_USER);
}
else {
$i = 0;
foreach ($newentry as $users => $info) {
if ($account->ldap_dn == $info['dn']) {
$entry = $info['attribs'];
}
$i++;
}
}
if (isset($entry)) {
$ldap_drupal_reverse_mappings = _ldapdata_reverse_mappings($account->ldap_config);
// Retrieve profile fields list.
$content_profile_fields = _ldapdata_retrieve_content_profile_fields();
$profile_fields = _ldapdata_retrieve_profile_fields();
// Determine which profile fields are dates
if (!empty($profile_fields)) {
$placeholders = implode(',', array_fill(0, count($profile_fields), "'%s'"));
$result = db_query("SELECT name, options from {profile_fields} WHERE name IN ({$placeholders}) AND type = 'date'", $profile_fields);
$date_fields = array();
while ($row = db_fetch_object($result)) {
array_push($date_fields, $row->name);
}
}
// If needed, get the content profile nodes
$content_profile_nodes = array();
if (!empty($content_profile_fields)) {
$cp_types = content_profile_get_types('types');
foreach ($cp_types as $type_obj) {
$type = $type_obj->type;
$profile = content_profile_load($type, $account->uid, '', TRUE);
if (!$profile) {
$profile = new stdClass();
$profile->type = $type;
$profile->title = isset($account->name) ? $account->name : '';
$profile->uid = $account->uid;
node_save($profile);
// Create node to get CCK fields
}
$content_profile_nodes[] = $profile;
}
}
$updated_nodes = array();
$drupal_fields = array();
foreach (_ldapdata_reverse_mappings($account->ldap_config) as $drupal_field => $ldap_attr) {
$value = isset($entry[strtolower($ldap_attr)]) ? $entry[strtolower($ldap_attr)][0] : '';
// Is it a profile field?
if (!empty($profile_fields) && is_numeric($drupal_field)) {
if (in_array($profile_fields[$drupal_field], $date_fields)) {
$value = serialize(array(
"month" => (int) substr($value, 4, 2),
"day" => (int) substr($value, 6, 2),
"year" => (int) substr($value, 0, 4),
));
}
if ($profile_field = isset($profile_fields[$drupal_field]) ? $profile_fields[$drupal_field] : NULL) {
if ($row = db_fetch_array(db_query("SELECT value FROM {profile_values} WHERE fid = '%d' AND uid = '%d'", $drupal_field, $account->uid))) {
if ($row['value'] != $value) {
db_query("UPDATE {profile_values} SET value = '%s' WHERE fid = '%d' AND uid = '%d'", $value, $drupal_field, $account->uid);
}
}
else {
db_query("INSERT INTO {profile_values} (value, fid, uid) VALUES ('%s', '%d', '%d')", $value, $drupal_field, $account->uid);
}
$account->{$drupal_field} = $value;
}
}
elseif (isset($content_profile_fields[$drupal_field])) {
// Find a matching profile node.
foreach ($content_profile_nodes as $profile_key => $profile) {
$node_updated = FALSE;
if (isset($profile->{$drupal_field})) {
// Determine what kind of field we are dealing with
$field_lookup = content_fields($drupal_field);
$field_type = $field_lookup['type'];
switch ($field_type) {
case 'email':
if ($profile->{$drupal_field}[0]['email'] != $value) {
$profile->{$drupal_field}[0]['email'] = $value;
$node_updated = TRUE;
}
break;
case 'content_taxonomy':
// Check to see if there are any terms that match
if ($term = taxonomy_get_term_by_name($value)) {
// If so, check to make sure they match the vocabulary
if ($term[0]->vid == $field_lookup['vid']) {
if ($profile->{$drupal_field}[0]['value'] != $term[0]->tid) {
$profile->{$drupal_field}[0]['value'] = $term[0]->tid;
$node_updated = TRUE;
}
}
else {
$newtid = _ldapdata_add_taxonomy_term($value, $field_lookup['vid']);
$profile->{$drupal_field}[0]['value'] = $newtid;
$node_updated = TRUE;
}
}
else {
$newtid = _ldapdata_add_taxonomy_term($value, $field_lookup['vid']);
$profile->{$drupal_field}[0]['value'] = $newtid;
$node_updated = TRUE;
}
break;
default:
if ($profile->{$drupal_field}[0]['value'] != $value) {
$profile->{$drupal_field}[0]['value'] = $value;
$node_updated = TRUE;
}
}
// Only save node if something changed. Prevents node modified errors.
if ($node_updated) {
$updated_nodes[$profile_key] = $profile;
}
}
}
}
elseif (isset($account->{$drupal_field}) && !in_array($drupal_field, array(
'pass',
))) {
$drupal_fields = array_merge($drupal_fields, array(
$drupal_field => $value,
));
}
}
if (!empty($drupal_fields)) {
if (!empty($drupal_fields['picture'])) {
$fname = file_directory_path() . "/" . variable_get('user_picture_path', 'pictures') . "/picture-" . $account->uid . ".jpg";
if ($fhandle = fopen($fname, 'w')) {
fwrite($fhandle, $drupal_fields['picture']);
fclose($fhandle);
$drupal_fields['picture'] = $fname;
}
else {
watchdog('ldapdata', "Could not open user picture file for writing. File=%file!", array(
'%file' => $fname,
), WATCHDOG_WARNING);
unset($drupal_fields['picture']);
}
}
$account = user_save($account, $drupal_fields);
}
if (!empty($updated_nodes)) {
foreach ($updated_nodes as $profile) {
// Flag this profile node as already synched.
$profile->ldap_synched = TRUE;
node_save($profile);
node_load($profile->nid, $profile->vid, TRUE);
// Force cache refresh
}
}
}
$_ldapdata_ldap
->disconnect();
}