function simple_ldap_user_sync_user_to_ldap in Simple LDAP 7
Same name and namespace in other branches
- 7.2 simple_ldap_user/simple_ldap_user.module \simple_ldap_user_sync_user_to_ldap()
Synchronizes Drupal user properties to LDAP.
2 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.
- simple_ldap_user_sync_user in simple_ldap_user/
simple_ldap_user.module - Synchronize a user from or to LDAP, depending on the settings.
File
- simple_ldap_user/
simple_ldap_user.module, line 406 - Main simple_ldap_user module file.
Code
function simple_ldap_user_sync_user_to_ldap($drupal_user) {
// Don't try to sync if the server is read-only.
$server = SimpleLdapServer::singleton();
if ($server->readonly) {
return;
}
// Don't try to sync anonymous or user 1.
if ($drupal_user->uid == 0 || $drupal_user->uid == 1) {
return;
}
// simple_ldap_user configuration.
$attribute_name = simple_ldap_user_variable_get('simple_ldap_user_attribute_name');
$attribute_mail = simple_ldap_user_variable_get('simple_ldap_user_attribute_mail');
$attribute_pass = simple_ldap_user_variable_get('simple_ldap_user_attribute_pass');
// Load the LDAP user.
$ldap_user = SimpleLdapUser::singleton($drupal_user->name);
// Mail is a special attribute.
$ldap_user->{$attribute_mail} = $drupal_user->mail;
// Password is a special attribute.
$ldap_user->{$attribute_pass} = $drupal_user->pass;
// Perform additional property and field mappings based on the user map.
$ldap_user->mapObject
->mapFromDrupalToLdap($drupal_user, $ldap_user);
// Set the DN.
$attribute_rdn = simple_ldap_user_variable_get('simple_ldap_user_attribute_rdn');
if (empty($attribute_rdn)) {
$attribute_rdn = $attribute_name;
}
if ($ldap_user->{$attribute_rdn}['count'] > 0) {
//If drupal username have changed and is used for RDN, retrieve old ldap
//user already loaded by controller and set previous DN temporary to move it.
if (isset($drupal_user->original) && $drupal_user->original->name !== $drupal_user->name && $attribute_rdn === $attribute_name) {
$original_ldap_user = SimpleLdapUser::singleton($drupal_user->original->name);
$ldap_user->dn = $original_ldap_user->dn;
}
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', $ldap_user, $drupal_user);
// Save any changes.
try {
$ldap_user
->save();
} catch (SimpleLdapException $e) {
drupal_set_message(t('Failed to save the user to LDAP.') . ' ' . format_string('%error', array(
'%error' => $e
->getMessage(),
)), 'error');
}
}