0010622: user contact email is not updated during LDAP sync
authorPhilipp Schüle <p.schuele@metaways.de>
Fri, 2 Jan 2015 12:57:12 +0000 (13:57 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 2 Jan 2015 17:11:34 +0000 (18:11 +0100)
* includes email, name and other data from user object in LDAP contact
sync
* adds some logging
* only update contact if diff is not empty

https://forge.tine20.org/view.php?id=10622

Change-Id: I2168197676cfdd2a5c68359bf23b9781691e885a
Reviewed-on: http://gerrit.tine20.com/customers/1498
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Lars Kneschke <l.kneschke@metaways.de>
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Admin/Controller/User.php
tine20/Tinebase/User.php
tine20/Tinebase/User/Ldap.php

index e4715e4..e467cf3 100644 (file)
@@ -400,6 +400,17 @@ class Admin_Controller_User extends Tinebase_Controller_Abstract
     }
     
     /**
+     * returns default internal addressbook container
+     * 
+     * @return string|int ID
+     */
+    public function getDefaultInternalAddressbook()
+    {
+        $appConfigDefaults = Admin_Controller::getInstance()->getConfigSettings();
+        return $appConfigDefaults[Admin_Model_Config::DEFAULTINTERNALADDRESSBOOK];
+    }
+    
+    /**
      * create or update contact in addressbook backend
      * 
      * @param  Tinebase_Model_FullUser $_user
@@ -411,8 +422,7 @@ class Admin_Controller_User extends Tinebase_Controller_Abstract
         $contactsBackend->setGetDisabledContacts(true);
         
         if (empty($_user->container_id)) {
-            $appConfigDefaults = Admin_Controller::getInstance()->getConfigSettings();
-            $_user->container_id = $appConfigDefaults[Admin_Model_Config::DEFAULTINTERNALADDRESSBOOK];
+            $_user->container_id = $this->getDefaultInternalAddressbook();
         }
         
         try {
index 0f26c2f..f3afffa 100644 (file)
@@ -50,7 +50,16 @@ class Tinebase_User
      *
      */
     const DEFAULT_ADMIN_GROUP_NAME_KEY = 'defaultAdminGroupName';
-
+    
+    protected static $_contact2UserMapping = array(
+        'n_family'      => 'accountLastName',
+        'n_given'       => 'accountFirstName',
+        'n_fn'          => 'accountFullName',
+        'n_fileas'      => 'accountDisplayName',
+        'email'         => 'accountEmailAddress',
+        'container_id'  => 'container_id',
+    );
+    
     /**
      * the constructor
      *
@@ -529,20 +538,28 @@ class Tinebase_User
         
         try {
             $contact = $addressbook->getByUserId($syncedUser->getId());
+            $originalContact = clone $contact;
             
             Tinebase_User::getInstance()->updateContactFromSyncBackend($syncedUser, $contact);
-            if (! empty($syncedUser->container_id)) {
-                $contact->container_id = $syncedUser->container_id;
-            }
+            $contact = self::_user2Contact($syncedUser, $contact);
             
-            // add modlog info
-            // Tinebase_Timemachine_ModificationLog::setRecordMetaData($contact, 'update');
-            // Tinebase_Container::getInstance()->increaseContentSequence($contact->container_id);
-            
-            if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
-                . ' Updating contact data for user ' . $syncedUser->accountLoginName);
-            
-            $addressbook->update($contact);
+            // TODO allow to diff jpegphoto, too
+            $diff = $contact->diff($originalContact, array('jpegphoto'));
+            if (! $diff->isEmpty()) {
+                // add modlog info
+                Tinebase_Timemachine_ModificationLog::setRecordMetaData($contact, 'update');
+                Tinebase_Container::getInstance()->increaseContentSequence($contact->container_id);
+                
+                if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                    . ' Updating contact data for user ' . $syncedUser->accountLoginName);
+                if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                    . ' Diff: ' . print_r($diff->toArray(), true));
+                
+                $addressbook->update($contact);
+            } else {
+                if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                    . ' User contact is up to date.');
+            }
         } catch (Addressbook_Exception_NotFound $aenf) {
             self::createContactForSyncedUser($syncedUser);
             $syncedUser = Tinebase_User::getInstance()->updateUserInSqlBackend($syncedUser);
@@ -628,31 +645,53 @@ class Tinebase_User
             return;
         }
         
-        $addressbook = Addressbook_Backend_Factory::factory(Addressbook_Backend_Factory::SQL);
-        $internalAddressbook = Tinebase_Container::getInstance()->getContainerByName('Addressbook', 'Internal Contacts', Tinebase_Model_Container::TYPE_SHARED);
-    
-        $contact = new Addressbook_Model_Contact(array(
-            'n_family'      => $user->accountLastName,
-            'n_given'       => $user->accountFirstName,
-            'n_fn'          => $user->accountFullName,
-            'n_fileas'      => $user->accountDisplayName,
-            'email'         => $user->accountEmailAddress,
-            'type'          => Addressbook_Model_Contact::CONTACTTYPE_USER,
-            'container_id'  => (! empty($user->container_id)) ? $user->container_id : $internalAddressbook->getId()
-        ));
-    
+        $contact = self::_user2Contact($user);
+        
         // add modlog info
         Tinebase_Timemachine_ModificationLog::setRecordMetaData($contact, 'create');
-    
+        
+        $addressbook = Addressbook_Backend_Factory::factory(Addressbook_Backend_Factory::SQL);
         $contact = $addressbook->create($contact);
         
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ 
             . " Added contact " . $contact->n_given);
-    
+        
         $user->contact_id = $contact->getId();
     }
     
     /**
+     * sync user data to contact
+     * 
+     * @param Tinebase_Model_FullUser $user
+     * @param Addressbook_Model_Contact $contact
+     * @return Addressbook_Model_Contact
+     */
+    protected static function _user2Contact($user, $contact = null)
+    {
+        if ($contact === null) {
+            $contact = new Addressbook_Model_Contact(array(), true);
+        }
+        
+        $contact->type = Addressbook_Model_Contact::CONTACTTYPE_USER;
+        
+        foreach (self::$_contact2UserMapping as $contactKey => $userKey) {
+            if (! empty($contact->{$contactKey}) && $contact->{$contactKey} == $user->{$userKey}) {
+                continue;
+            }
+            
+            switch ($contactKey) {
+                case 'container_id':
+                    $contact->container_id = (! empty($user->container_id)) ? $user->container_id : Admin_Controller_User::getInstance()->getDefaultInternalAddressbook();
+                    break;
+                default:
+                    $contact->{$contactKey} = $user->{$userKey};
+            }
+        }
+        
+        return $contact;
+    }
+    
+    /**
      * import users from sync backend
      * 
      * @param array $options
index b86cef4..2406f47 100644 (file)
@@ -774,7 +774,9 @@ class Tinebase_User_Ldap extends Tinebase_User_Sql implements Tinebase_User_Inte
         $userData = $this->_ldap->getEntry($userData['dn']);
         
         $this->_ldap2Contact($userData, $_contact);
-        #if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . "  synced user object: " . print_r($_contact->toArray(), true));
+        
+        if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__
+            . " Synced user object: " . print_r($_contact->toArray(), true));
     }
     
     /**