Merge branch '2013.10' into 2014.11
[tine20] / tine20 / Tinebase / User.php
index cc37581..f6ed098 100644 (file)
@@ -239,7 +239,7 @@ class Tinebase_User
         }
         
         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ 
-            . ' Created user backend of type ' . $backendType);
+            . ' Created user backend of type ' . get_class($result));
         
         return $result;
     }
@@ -419,7 +419,7 @@ class Tinebase_User
     }
     
     /**
-     * syncronize user from syncbackend to local sql backend
+     * synchronize user from syncbackend to local sql backend
      * 
      * @param  mixed  $username  the login id of the user to synchronize
      * @param  array $options
@@ -428,7 +428,6 @@ class Tinebase_User
      * 
      * @todo make use of dbmail plugin configurable (should be false by default)
      * @todo switch to new primary group if it could not be found
-     * @todo write a test and refactor this ... :(
      */
     public static function syncUser($username, $options = array())
     {
@@ -461,37 +460,8 @@ class Tinebase_User
         self::getPrimaryGroupForUser($user);
 
         try {
-            $currentUser = $userBackend->getUserByProperty('accountId', $user, 'Tinebase_Model_FullUser');
-        
-            $currentUser->accountLoginName          = $user->accountLoginName;
-            $currentUser->accountLastPasswordChange = $user->accountLastPasswordChange;
-            $currentUser->accountExpires            = $user->accountExpires;
-            $currentUser->accountPrimaryGroup       = $user->accountPrimaryGroup;
-            $currentUser->accountDisplayName        = $user->accountDisplayName;
-            $currentUser->accountLastName           = $user->accountLastName;
-            $currentUser->accountFirstName          = $user->accountFirstName;
-            $currentUser->accountFullName           = $user->accountFullName;
-            $currentUser->accountEmailAddress       = $user->accountEmailAddress;
-            $currentUser->accountHomeDirectory      = $user->accountHomeDirectory;
-            $currentUser->accountLoginShell         = $user->accountLoginShell;
-
-            $currentUser->accountStatus             = isset($user->accountStatus)
-                ? $user->accountStatus
-                : Tinebase_Model_User::ACCOUNT_STATUS_ENABLED;
-
-            if (! empty($user->visibility) && $currentUser->visibility !== $user->visibility) {
-                $currentUser->visibility            = $user->visibility;
-                if (empty($currentUser->contact_id) && $currentUser->visibility == Tinebase_Model_FullUser::VISIBILITY_DISPLAYED) {
-                    self::createContactForSyncedUser($currentUser);
-                }
-            }
-        
-            $syncedUser = $userBackend->updateUserInSqlBackend($currentUser);
-            if (! empty($user->container_id)) {
-                $syncedUser->container_id = $user->container_id;
-            }
-            $userBackend->updatePluginUser($syncedUser, $user);
-            
+            $syncedUser = self::_syncDataAndUpdateUser($user, $options);
+
         } catch (Tinebase_Exception_NotFound $ten) {
             try {
                 $invalidUser = $userBackend->getUserByPropertyFromSqlBackend('accountLoginName', $username, 'Tinebase_Model_FullUser');
@@ -505,20 +475,67 @@ class Tinebase_User
             if ($user->visibility !== Tinebase_Model_FullUser::VISIBILITY_HIDDEN) {
                 self::createContactForSyncedUser($user);
             }
+            Tinebase_Timemachine_ModificationLog::setRecordMetaData($user, 'create');
             $syncedUser = $userBackend->addUserInSqlBackend($user);
             $userBackend->addPluginUser($syncedUser, $user);
         }
         
         self::syncContactData($syncedUser, $options);
         
-        // sync group memberships
         Tinebase_Group::syncMemberships($syncedUser);
-        
+
         return $syncedUser;
     }
-    
+
     /**
-     * import contactdata(phone, address, fax, birthday. photo)
+     * sync account data and update
+     *
+     * @param Tinebase_Model_FullUser $user
+     * @param array $options
+     * @return mixed
+     * @throws Tinebase_Exception_InvalidArgument
+     */
+    protected static function _syncDataAndUpdateUser($user, $options)
+    {
+        $currentUser = Tinebase_User::getInstance()->getUserByProperty('accountId', $user, 'Tinebase_Model_FullUser');
+
+        $fieldsToSync = array('accountLoginName', 'accountLastPasswordChange', 'accountExpires', 'accountPrimaryGroup',
+            'accountDisplayName', 'accountLastName', 'accountFirstName', 'accountFullName', 'accountEmailAddress',
+            'accountHomeDirectory', 'accountLoginShell');
+        if (isset($options['syncAccountStatus']) && $options['syncAccountStatus']) {
+            $fieldsToSync[] = 'accountStatus';
+        }
+        $recordNeedsUpdate = false;
+        foreach ($fieldsToSync as $field) {
+            if ($currentUser->{$field} !== $user->{$field}) {
+                $currentUser->{$field} = $user->{$field};
+                $recordNeedsUpdate = true;
+            }
+        }
+
+        if (! empty($user->visibility) && $currentUser->visibility !== $user->visibility) {
+            $currentUser->visibility            = $user->visibility;
+            if (empty($currentUser->contact_id) && $currentUser->visibility == Tinebase_Model_FullUser::VISIBILITY_DISPLAYED) {
+                self::createContactForSyncedUser($currentUser);
+            }
+        }
+
+        if ($recordNeedsUpdate) {
+            Tinebase_Timemachine_ModificationLog::setRecordMetaData($currentUser, 'update');
+            $syncedUser = Tinebase_User::getInstance()->updateUserInSqlBackend($currentUser);
+        } else {
+            $syncedUser = $currentUser;
+        }
+        if (! empty($user->container_id)) {
+            $syncedUser->container_id = $user->container_id;
+        }
+        Tinebase_User::getInstance()->updatePluginUser($syncedUser, $user);
+
+        return $syncedUser;
+    }
+
+    /**
+     * import contact data(phone, address, fax, birthday. photo)
      * 
      * @param Tinebase_Model_FullUser $syncedUser
      * @param array $options
@@ -557,8 +574,10 @@ class Tinebase_User
             if (! $diff->isEmpty() || ($originalContact->jpegphoto == 0 && ! empty($contact->jpegphoto))) {
                 // add modlog info
                 Tinebase_Timemachine_ModificationLog::setRecordMetaData($contact, 'update');
-                Tinebase_Container::getInstance()->increaseContentSequence($contact->container_id);
-
+                if ($contact->container_id !== null) {
+                    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__
@@ -572,6 +591,7 @@ class Tinebase_User
         } catch (Addressbook_Exception_NotFound $aenf) {
             self::createContactForSyncedUser($syncedUser);
             $syncedUser = Tinebase_User::getInstance()->updateUserInSqlBackend($syncedUser);
+
         } catch (Tinebase_Exception_NotFound $tenf) {
             if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__
                 . ' Contact information seems to be missing in sync backend');
@@ -732,10 +752,8 @@ class Tinebase_User
             self::_syncDeletedUsers($users);
         }
         
-        // @todo this should be improved: only the cache of synced users + group memberships should be cleaned
-        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ 
-            . ' Finished synchronizing users. Clearing cache after user sync ...');
-        Tinebase_Core::getCache()->clean();
+        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+            . ' Finished synchronizing users.');
     }
 
     /**
@@ -894,14 +912,19 @@ class Tinebase_User
         // update or create user in local sql backend
         try {
             $userBackend->getUserByProperty('accountLoginName', $adminLoginName);
+            Tinebase_Timemachine_ModificationLog::setRecordMetaData($user, 'update');
             $user = $userBackend->updateUserInSqlBackend($user);
         } catch (Tinebase_Exception_NotFound $ten) {
             // call addUser here to make sure, sql user plugins (email, ...) are triggered
+            Tinebase_Timemachine_ModificationLog::setRecordMetaData($user, 'create');
             $user = $userBackend->addUser($user);
         }
         
         // set the password for the account
-        Tinebase_User::getInstance()->setPassword($user, $adminPassword);
+        // empty password triggers password change dialogue during first login
+        if (!empty($adminPassword)) {
+            Tinebase_User::getInstance()->setPassword($user, $adminPassword);
+        }
 
         // add the admin account to all groups
         Tinebase_Group::getInstance()->addGroupMember($adminGroup, $user);