You are here

function simple_ldap_user_sync_user_to_drupal in Simple LDAP 7.2

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

Synchronizes LDAP attributes to Drupal user properties.

2 calls to simple_ldap_user_sync_user_to_drupal()
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().

File

simple_ldap_user/simple_ldap_user.module, line 1060
Main simple_ldap_user module file.

Code

function simple_ldap_user_sync_user_to_drupal($drupal_user) {

  // Skip UID 1
  if (property_exists($drupal_user, 'uid') && $drupal_user->uid == 1) {
    return;
  }

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

  // Nothing to sync.
  if (!$ldap_user->exists) {
    return;
  }

  // Initialize array of attribute changes.
  $edit = array();

  // Process the LDAP user to generate the edit array that gets passed to user_save();
  simple_ldap_user_generate_edit_ldap_to_drupal($edit, $ldap_user, $drupal_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);

    // If any conversion added a #ignored tag, that means
    // the Drupal values of some fields were not replaced
    // by values from the LDAP record and need to be written
    // back to the LDAP server.  Used by the timestamps to
    // ensure the latest values hold in both.
    if (!empty($edit['#ignored'])) {
      simple_ldap_user_sync_user_to_ldap($drupal_user);
    }
  }

  // Synchronized user.
  return $drupal_user;
}