Index: ldapdata.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ldap_integration/ldapdata.admin.inc,v
retrieving revision 1.2
diff -u -r1.2 ldapdata.admin.inc
--- ldapdata.admin.inc	19 Feb 2009 16:56:16 -0000	1.2
+++ ldapdata.admin.inc	11 Mar 2009 21:15:07 -0000
@@ -10,6 +10,59 @@
 // ldapdata settings
 
 /**
+ * Implements the settings page.
+ *
+ * @return
+ *   The form structure.
+ */
+function ldapdata_admin_settings() {
+  $form['cache'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Caching'),
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
+  );
+  $form['cache']['ldapdata_cache'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Cache LDAP queries'),
+    '#default_value' => LDAPDATA_CACHE,
+    '#description' => t('Synchronize user\'s LDAP data with his drupal\'s data only when the user logs in to the drupal. Setting this option will save a lot of LDAP queries when drupal loads other users. If unchecked, the drupal will sync with the LDAP on each user object load.'),
+  );
+  $form['submit'] = array(
+   '#type'  => 'submit',
+    '#value' => t('Save configuration'),
+  );
+  $form['reset'] = array(
+    '#type'  => 'submit',
+    '#value' => t('Reset to defaults'),
+  );
+
+  $form['list']['#value'] = ldapdata_admin_list();
+
+  return $form;
+}
+
+/**
+ * Submit hook for the settings form.
+ */
+function ldapdata_admin_settings_submit($form, &$form_state) {
+  $op = $form_state['clicked_button']['#value'];
+  $values = $form_state['values'];
+  switch ($op) {
+    case t('Save configuration'):
+      variable_set('ldapdata_cache', $values['ldapdata_cache']);
+
+      drupal_set_message(t('The configuration options have been saved.'));
+      break;
+    case t('Reset to defaults'):
+      variable_del('ldapdata_cache');
+
+      drupal_set_message(t('The configuration options have been reset to their default values.'));
+      break;
+  }
+}
+
+/**
  * Implements the LDAP servers list.
  *
  * @return
@@ -81,7 +134,7 @@
     $form['ldap_attribute_mapping']['ldap_attr_mapping'] = array(
       '#type' => 'radios',
       '#title' => t('Should Drupal account fields be mapped to LDAP Attributes?'),
-      '#default_value' => $mappings['access'] ? $mappings['access'] : LDAPDATA_MAP_ATTRIBUTES,
+      '#default_value' => isset($mappings['access']) ? $mappings['access'] : LDAPDATA_MAP_ATTRIBUTES,
       '#options' => array(
         LDAPDATA_MAP_ATTRIBUTES => t('Changes in account fields will be mapped to LDAP attributes and back.'),
         LDAPDATA_MAP_ATTRIBUTES_READ_ONLY => t('Same, but read-only mode.'),
@@ -217,11 +270,10 @@
   switch ($op) {
     case t('Update'):
 
-      // Attribute mapping.
-      $attr_mapping_access = $values['ldap_attr_mapping'];
-      $attr_mappings['access'] = $attr_mapping_access;
+      // We store mapping type along with other attribute mapping. 
+      $attr_mappings['access'] = $values['ldap_attr_mapping'];
 
-      if ($attr_mapping_access >= 4) {
+      if ($attr_mappings['access'] >= 4) {
         foreach (element_children($values) as $attr) {
           if (preg_match("/ldap_amap/", $attr) && $values[$attr]) {
             $attr_mappings[$attr] = $values[$attr];
Index: ldapdata.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ldap_integration/ldapdata.install,v
retrieving revision 1.3
diff -u -r1.3 ldapdata.install
--- ldapdata.install	19 Feb 2009 16:56:16 -0000	1.3
+++ ldapdata.install	11 Mar 2009 21:15:08 -0000
@@ -50,6 +50,9 @@
   db_drop_field($ret, 'ldapauth', 'ldapdata_roattrs');
   db_drop_field($ret, 'ldapauth', 'ldapdata_mappings');
 
+  // Remove variables
+  variable_del('ldapdata_cache');
+
   return $ret;
 }
 
Index: ldapdata.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ldap_integration/ldapdata.module,v
retrieving revision 1.16
diff -u -r1.16 ldapdata.module
--- ldapdata.module	19 Feb 2009 16:56:16 -0000	1.16
+++ ldapdata.module	11 Mar 2009 21:15:10 -0000
@@ -8,6 +8,7 @@
 
 //////////////////////////////////////////////////////////////////////////////
 
+define('LDAPDATA_CACHE',          variable_get('ldapdata_cache', 1));
 define('LDAPDATA_PROFILE',        'LDAP attributes');
 define('LDAPDATA_PROFILE_WEIGHT', 5);
 define('LDAPDATA_USER_TAB',       'LDAP entry');
@@ -62,7 +63,8 @@
   $items['admin/settings/ldap/ldapdata'] = array(
     'title' => 'Data',
     'description' => 'Configure LDAP data to Drupal profiles synchronization settings.',
-    'page callback' => 'ldapdata_admin_list',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('ldapdata_admin_settings'),
     'access arguments' => array('administer ldap modules'),
     'file' => 'ldapdata.admin.inc',
   );
@@ -151,6 +153,7 @@
  */
 function _ldapdata_user_form(&$user, $category) {
   global $_ldapdata_ldap;
+
   $mapping_type = _ldapdata_ldap_info($user, 'mapping_type');
   $attributes = (array) _ldapdata_ldap_info($user, 'ldapdata_rwattrs');
 
@@ -187,40 +190,62 @@
 /**
  * Implements hook_user() load operation.
  */
-function _ldapdata_user_load(&$user) {
-  global $_ldapdata_ldap;
+function _ldapdata_user_load(&$account) {
+  global $user, $_ldapdata_ldap;
 
   // Setup the global $_ldapdata_ldap object.
-  if (!_ldapdata_init($user))
+  if (!_ldapdata_init($account))
     return;
 
-  $mapping_type = _ldapdata_ldap_info($user, 'mapping_type');
+  if (LDAPDATA_CACHE && $user->uid != $account->uid)
+    return;
 
-  $authmap = user_get_authmaps($user->name);
+  static $account_fields = array();
+  if (isset($account_fields[$account->uid])) {
+    foreach ($account_fields[$account->uid] as $field => $value)
+      $account->$field = $value;
+    return;
+  }
+
+  $mapping_type = _ldapdata_ldap_info($account, 'mapping_type');
+
+  $authmap = user_get_authmaps($account->name);
   // See http://drupal.org/node/91786 about user_node().
   // User can be edited by the user or by other authorized users.
   if (!isset($authmap['ldapauth']) || ($mapping_type == LDAPDATA_MAP_NOTHING)) {
     return;
   }
 
-  $bind_info = _ldapdata_edition($user);
+  $account_fields[$account->uid] = array();
+  $bind_info = _ldapdata_edition($account);
   if (!$_ldapdata_ldap->connect($bind_info['dn'], $bind_info['pass'])) {
-    watchdog('ldapdata', "User load: user %name's data could not be read in the LDAP directory", array('%name' => $user->name), WATCHDOG_WARNING);
+    watchdog('ldapdata', "User load: user %name's data could not be read in the LDAP directory", array('%name' => $account->name), WATCHDOG_WARNING);
     return;
   }
 
-  if ($entry = $_ldapdata_ldap->retrieveAttributes($user->ldap_dn)) {
-    $ldap_drupal_mappings = array_flip(_ldapdata_reverse_mappings($user->ldap_config));
+  if ($entry = $_ldapdata_ldap->retrieveAttributes($account->ldap_dn)) {
+    $ldap_drupal_reverse_mappings = _ldapdata_reverse_mappings($account->ldap_config);
+    $ldap_drupal_mappings = array_flip($ldap_drupal_reverse_mappings);
+
+    // Retrieve profile fields list.
+    $profile_fields = _ldapdata_retrieve_profile_fields();
+
     foreach ($ldap_drupal_mappings as $ldap_attr => $drupal_field) {
-      if (isset($user->$drupal_field) && $ldap_attr && $drupal_field != 'access') {
-        if ($drupal_field != 'pass' && $drupal_field != 'mail') {
-          $user->$drupal_field = $entry[strtolower($ldap_attr)][0];
-        }
+      $value = $entry[strtolower($ldap_attr)][0];
+   
+      // Is it a profile field?
+      if ($profile_field = is_numeric($drupal_field) && isset($profile_fields[$drupal_field]) ? $profile_fields[$drupal_field] : NULL) {
+        if ($row = db_fetch_array(db_query("SELECT value FROM {profile_values} WHERE fid = '%d' AND uid = '%d'", $drupal_field, $account->uid)))
+          db_query("UPDATE {profile_values} SET value = '%s' WHERE fid = '%d' AND uid = '%d'", $value, $drupal_field, $account->uid);
+        else
+          db_query("INSERT INTO {profile_values} (value, fid, uid) VALUES ('%s', '%d', '%d')", $value, $drupal_field, $account->uid);
+        $account->$profile_field = $account_fields[$account->uid][$profile_field] = $value;
+      } 
+      else if (isset($account->$drupal_field) && !in_array($drupal_field, array('pass', 'mail'))) {
+        $account->$drupal_field = $account_fields[$account->uid][$drupal_field] = $value;
       }
     }
-    _ldapdata_user_profile_load($user);
   }
-
   $_ldapdata_ldap->disconnect();
 }
 
@@ -358,48 +383,6 @@
 // Auxiliary functions
 
 /**
- * Sync LDAP data to profile.
- *
- * @param $user
- *   A user object.
- *
- * @return
- */
-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.
-  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.
-    $row = db_fetch_array(db_query("SELECT value FROM {profile_values} WHERE fid = '%d' AND uid = '%d'", $fid, $uid));
-    if ($row) {
-      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);
-    }
-  }
-}
-
-/**
  * Find out which LDAP attributes should be synced back to LDAP..
  *
  * @param $edit
@@ -597,11 +580,8 @@
 
   if (is_array($mappings)) {
     foreach ($mappings as $key => $value) {
-      $drupal_key = preg_replace('/^ldap_amap-(.*)$/', '$1', $key);
-      $ldap_attr = $value;
-      if ($drupal_key && $ldap_attr) {
-        $map[$drupal_key] = $ldap_attr;
-      }
+      if (($drupal_key = preg_replace('/^ldap_amap-(.*)$/', '$1', $key)) && $drupal_key != 'access')
+        $map[$drupal_key] = $value;
     }
   }
   return $map;
