From 27af3f78f9eed59244fe66161ede3302ac0fd4ee Mon Sep 17 00:00:00 2001
From: antgiant <antgiant@gmail.com>
Date: Wed, 8 Feb 2012 12:08:34 -0500
Subject: [PATCH 2/2] Fix bad sid in multi-server setup bug.

---
 ldapsync.module |   48 ++++++++++++++++++++++++++----------------------
 1 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/ldapsync.module b/ldapsync.module
index a99cfab..14fc455 100644
--- a/ldapsync.module
+++ b/ldapsync.module
@@ -90,29 +90,32 @@ function _ldapsync_sync() {
     // Does user exist in Drupal (find by username)? If not, create it (using process in ldapauth).
     $account = user_load(array('name' => $name));
     if (!$account->uid) {
+      if (!_ldapsync_init($data['sid'])) {
+        watchdog('ldapsync', 'ldapsync init failed for ldap server %sid on user %user.', array('%sid' => $row->sid, '%user' => $name));
+      }
+      else {
+        // User does not exist in Drupal. Let's create it.
+        $pass = user_password(20);  // Generate a random password (Drupal will auth against ldap anyway).
+        $mail = $data['mail'];
+        $init = $mail = key_exists(($_ldapsync_ldap->getOption('mail_attr') ? $_ldapsync_ldap->getOption('mail_attr') : LDAPAUTH_DEFAULT_MAIL_ATTR), $data) ? $data[$_ldapsync_ldap->getOption('mail_attr')] : $name;
+        $userinfo = array(
+          // new row to table "users":
+          'name' => $name,
+          'pass' => $pass,
+          'mail' => $mail,
+          'init' => $init,
+          'status' => 1,
+          'ldap_authentified' => TRUE,
+          'ldap_dn' => $dn,
+          'ldap_config' => $_ldapsync_ldap->getOption('sid'),
+          // new row to table "authmap": module='ldapauth', authname=$name
+          'authname_ldapauth' => $name,
+        );
 
-      // User does not exist in Drupal. Let's create it.
-      $pass = user_password(20);  // Generate a random password (Drupal will auth against ldap anyway).
-      $mail = $data['mail'];
-      $init = $mail = key_exists(($_ldapsync_ldap->getOption('mail_attr') ? $_ldapsync_ldap->getOption('mail_attr') : LDAPAUTH_DEFAULT_MAIL_ATTR), $data) ? $data[$_ldapsync_ldap->getOption('mail_attr')] : $name;
-      $userinfo = array(
-        // new row to table "users":
-        'name' => $name,
-        'pass' => $pass,
-        'mail' => $mail,
-        'init' => $init,
-        'status' => 1,
-        'ldap_authentified' => TRUE,
-        'ldap_dn' => $dn,
-        'ldap_config' => $_ldapsync_ldap->getOption('sid'),
-        // new row to table "authmap": module='ldapauth', authname=$name
-        'authname_ldapauth' => $name,
-      );
-
-      // Save user.
-      $account = user_save('', $userinfo);
-      $count_new_users++;
-
+        // Save user.
+        $account = user_save('', $userinfo);
+        $count_new_users++;
+      }
     }
     else {
       // User exists in Drupal -- check a few things, but most users will require no further action.
@@ -248,6 +251,7 @@ function _ldapsync_search() {
           'dn' => $entry['dn'],
           'mail' => $entry[$mail_attr][0],
           'attribs' => $entry,
+          'sid' => $_ldapsync_ldap->getOption('sid'),
 
         );
       }
-- 
1.7.7.1.msysgit.0

