function simple_ldap_user_sync_user_to_ldap in Simple LDAP 7.2
Same name and namespace in other branches
- 7 simple_ldap_user/simple_ldap_user.module \simple_ldap_user_sync_user_to_ldap()
Synchronizes Drupal user properties to LDAP.
5 calls to simple_ldap_user_sync_user_to_ldap()
- simple_ldap_user_export_user in simple_ldap_user/
simple_ldap_user.module - Batch process function for mass user export. &$context comes from batch_set().
- simple_ldap_user_sync_user_to_drupal in simple_ldap_user/
simple_ldap_user.module - Synchronizes LDAP attributes to Drupal user properties.
- simple_ldap_user_user_load in simple_ldap_user/
simple_ldap_user.module - Implements hook_user_load().
- simple_ldap_user_user_login in simple_ldap_user/
simple_ldap_user.module - Implements hook_user_login().
- user_check_password in simple_ldap_user/
simple_ldap_user.password.inc - Check whether a plain text password matches a stored hashed password.
File
- simple_ldap_user/
simple_ldap_user.module, line 970 - Main simple_ldap_user module file.
Code
function simple_ldap_user_sync_user_to_ldap($drupal_user) {
// Don't try to sync anonymous or user 1.
if ($drupal_user->uid == 0 || $drupal_user->uid == 1) {
return;
}
// Don't try to sync if the server is read-only.
$server = SimpleLdapServer::singleton();
if ($server->readonly) {
return;
}
// simple_ldap_user configuration.
$user_fields = simple_ldap_user_user_fields();
$attribute_map = simple_ldap_user_variable_get('simple_ldap_user_attribute_map');
// Load the LDAP user. If $drupal_user->orignal is set, this may be a move. Use the old name.
$search_name = !empty($drupal_user->original) ? $drupal_user->original->name : $drupal_user->name;
$ldap_user = SimpleLdapUser::singleton($search_name);
// Synchronize the fields in the attribute map.
foreach ($attribute_map as $ldap_attr_name => $drupal_fields) {
// Initialize the Drupal value array.
$drupal_values = array();
// Parse the drupal attribute name.
foreach ($drupal_fields as $drupal_field_name) {
// Get the Drupal value(s) based on the field type.
if (!array_key_exists($drupal_field_name, $user_fields)) {
continue;
}
switch ($user_fields[$drupal_field_name]['module']) {
case 'field':
simple_ldap_user_translate_drupal_attr_to_ldap($drupal_values, $drupal_user, $drupal_field_name);
break;
case 'user':
// Get the value directly from the user object.
simple_ldap_user_base_field_to_ldap($drupal_values, $drupal_user, $drupal_field_name);
break;
default:
watchdog('Simple LDAP', 'Field from unknown module %module. (not supported yet)', array(
'%module' => $field['module'],
), WATCHDOG_WARNING);
}
}
// Finally, add the values to the LDAP user.
if (!empty($drupal_fields['#delimiter'])) {
$drupal_values = implode($drupal_fields['#delimiter'], $drupal_values);
}
$ldap_user->{$ldap_attr_name} = $drupal_values;
}
// Set the DN.
$attribute_rdn = simple_ldap_user_variable_get('simple_ldap_user_attribute_rdn');
if (empty($attribute_rdn)) {
$attribute_rdn = simple_ldap_user_variable_get('simple_ldap_user_attribute_name');
}
if ($ldap_user->{$attribute_rdn}['count'] > 0) {
if ($ldap_user->dn) {
// Reconstruct an existing DN.
$parts = SimpleLdap::ldap_explode_dn($ldap_user->dn);
$basedn = '';
for ($i = 1; $i < $parts['count']; $i++) {
$basedn .= ',' . $parts[$i];
}
}
else {
// Default to using the configured basedn.
$basedn = ',' . simple_ldap_user_variable_get('simple_ldap_user_basedn');
}
$ldap_user->dn = $attribute_rdn . '=' . $ldap_user->{$attribute_rdn}[0] . $basedn;
}
// Allow altering the LDAP user object before saving.
drupal_alter('simple_ldap_user_to_ldap', $drupal_user, $ldap_user);
// Save any changes.
try {
$ldap_user
->save();
} catch (SimpleLdapException $e) {
drupal_set_message(t('Failed to save the user to LDAP: %error', array(
'%error' => $e
->getMessage(),
)), 'error');
}
}