You are here

function ldapdata_user_profile_load in LDAP integration 5

Same name and namespace in other branches
  1. 5.2 ldapdata.module \ldapdata_user_profile_load()
1 call to ldapdata_user_profile_load()
ldapdata_user_load in ./ldapdata.module

File

./ldapdata.module, line 506

Code

function ldapdata_user_profile_load(&$user) {
  global $ldapdata_ldap;
  $ldap_drupal_reverse_mappings = _ldapdata_reverse_mappings($user->ldap_config);
  $ldap_drupal_mappings = array_flip($ldap_drupal_reverse_mappings);

  // Retrieve profile fields list
  $profile_fields = _ldapdata_retrieve_profile_fields();

  // compare against mapped fields list
  $writeout = array();
  $entry = $ldapdata_ldap
    ->retrieveAttributes($user->ldap_dn);
  foreach ($profile_fields as $key => $field) {
    if (in_array($key, $ldap_drupal_mappings)) {
      $writeout[$field] = $entry[strtolower($ldap_drupal_reverse_mappings[$key])][0];
    }
  }

  // Write coincidences to Drupal DB
  foreach ($writeout as $field => $value) {
    $result = db_fetch_array(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $field));
    $fid = $result['fid'];
    $uid = $user->uid;

    // does the user have a value for this field ? then update it : otherwise create it
    $result = db_fetch_array(db_query("SELECT value FROM {profile_values} WHERE fid = '%d' AND uid = '%d'", $fid, $uid));
    if ($result) {
      db_query("UPDATE {profile_values} SET value = '%s' WHERE fid = '%d' AND uid = '%d'", $value, $fid, $uid);
    }
    else {
      db_query("INSERT INTO {profile_values} (value, fid, uid) VALUES ('%s', '%d', '%d')", $value, $fid, $uid);
    }
  }
}