You are here

function simple_ldap_user_sync_user_to_drupal in Simple LDAP 7

Same name and namespace in other branches
  1. 7.2 simple_ldap_user/simple_ldap_user.module \simple_ldap_user_sync_user_to_drupal()

Synchronizes LDAP attributes to Drupal user properties.

1 call to simple_ldap_user_sync_user_to_drupal()
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 479
Main simple_ldap_user module file.

Code

function simple_ldap_user_sync_user_to_drupal($drupal_user) {

  // Load the LDAP user, force a cache reset.
  $ldap_user = SimpleLdapUser::singleton($drupal_user->name, TRUE);

  // Nothing to sync.
  if (!$ldap_user->exists || isset($drupal_user->uid) && $drupal_user->uid == 1) {
    return;
  }

  // Initialize array of attribute changes.
  $edit = array();
  $ldap_user->mapObject
    ->mapFromLdapToDrupal($ldap_user, $edit, $drupal_user);

  // Allow altering the Drupal user object before saving.
  drupal_alter('simple_ldap_user_to_drupal', $edit, $drupal_user, $ldap_user);

  // Save any changes.
  if (!empty($edit)) {
    if (!isset($drupal_user->original)) {

      // This avoids an infinite load/save loop.
      $drupal_user->original = clone $drupal_user;
    }
    $drupal_user->hook_sync_user_to_drupal = TRUE;
    $drupal_user = user_save($drupal_user, $edit);
  }

  // Synchronized user.
  return $drupal_user;
}