You are here

function ldap_profile_user_login in Lightweight Directory Access Protocol (LDAP) 7

Implements hook_user_login().

File

ldap_profile/ldap_profile.module, line 76
This module provides the LDAP package the ability populate drupal profile fields with ldap entry data such as last name, first name, etc.

Code

function ldap_profile_user_login(&$edit, $account) {
  $servers = ldap_servers_get_servers(NULL, 'enabled');
  $server = 0;
  $changes = array();
  $authuser = user_is_logged_in();
  $ldapuser = FALSE;
  $dn = "";
  module_load_include('functions.inc', 'ldap_servers');
  if (empty($account) || !$authuser) {
    return;
  }
  $ldapuser = ldap_servers_get_user_ldap_data($account);
  if (empty($ldapuser) || empty($ldapuser['dn'])) {
    return;
  }
  $dn = $ldapuser['dn'];
  if (is_array($account->data) && array_key_exists("ldap_authentication", $account->data) && isset($account->data["ldap_authentication"]['init']['sid'])) {
    $sid = $account->data["ldap_authentication"]['init']['sid'];
    $server = $servers[$sid];
  }
  else {
    return;
  }
  $ldapfields = array();
  $dnLookups = array();
  $mapping = ldap_profile_get_mapping();
  $derivedMapping = ldap_profile_get_derived_mapping();

  // Put all our mappings into the ldapfields array
  if ($mapping != NULL) {
    foreach (array_keys($mapping) as $field) {
      if (strpos($field, "field_") !== FALSE) {

        //We have a custom field
        if (strpos($mapping[$field], "[") !== FALSE) {
          $token_keys = ldap_server_tokens_needed_for_template($mapping[$field]);
          foreach ($token_keys as $token_key) {
            if (!in_array($token_key, $ldapfields)) {
              array_push($ldapfields, $token_key);
            }
          }
        }
        else {
          array_push($ldapfields, $mapping[$field]);
        }

        // If we want to derive this field from a DN search..
        if ($derivedMapping[$field]['derive']) {
          if (!in_array($derivedMapping[$field]['derive_value'], $dnLookups)) {
            array_push($dnLookups, $derivedMapping[$field]['derive_value']);
            array_push($ldapfields, $derivedMapping[$field]['derive_value']);
          }
        }
      }
    }
  }
  array_push($ldapfields, 'uid');

  // Acquire all fields from user's LDAP attributes
  $ldapdata = $server
    ->search($dn, "(objectClass=*)", $ldapfields);
  if ($ldapdata === FALSE) {
    $ldapdata = array();
  }
  $ldapdata = $ldapdata[0];
  $dnLdapdata = array();

  // Perform searches for all DNs set to derive information from
  foreach ($dnLookups as $curDnLookup) {
    $curDnLookupResult = $server
      ->search($ldapdata[$curDnLookup][0], "(objectClass=*)", $ldapfields);
    if ($curDnLookupResult && array_key_exists(0, $curDnLookupResult)) {
      $dnLdapdata[$curDnLookup] = $curDnLookupResult[0];
    }
  }

  // Set/Update profile field information
  if (is_array($mapping)) {
    foreach (array_keys($mapping) as $field) {
      if (strpos($field, "field_") !== FALSE) {
        $add = FALSE;
        $toAdd = '';
        $ldapEntry = $derivedMapping[$field]['derive'] ? $dnLdapdata[$derivedMapping[$field]['derive_value']] : $ldapdata;

        // If we havea tokenized mapping
        if (strpos($mapping[$field], "[") !== FALSE) {
          $toAdd = ldap_server_token_replace($ldapEntry, $mapping[$field]);
          $add = _ldap_profile_add_change($account, $field, $toAdd);
        }
        elseif (array_key_exists($mapping[$field], $ldapEntry)) {
          if (array_key_exists(0, $ldapEntry[$mapping[$field]])) {
            $toAdd = $ldapEntry[$mapping[$field]][0];
            $add = _ldap_profile_add_change($account, $field, $toAdd);
          }
        }
        if ($add) {
          $changes[$field] = array(
            'und' => array(
              0 => array(
                'value' => $toAdd,
              ),
            ),
          );
        }
      }
    }
  }
  if (count($changes) > 0) {

    // preload the previously created account as the original
    // this is to prevent a cycling condition with entitycache.module
    // which will continue until memory is exhausted
    // the new field changes are treated as updates in this case
    // (Do not reset the cache until after the changes are saved)
    $account->original = user_load($account->uid, FALSE);
    user_save($account, $changes);
  }
}