0013310: user sync creates contact even if user creation fails
authorPhilipp Schüle <p.schuele@metaways.de>
Wed, 5 Jul 2017 11:59:07 +0000 (13:59 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 7 Jul 2017 06:45:04 +0000 (08:45 +0200)
* remove contact if user creation failed

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

Change-Id: Ib0a6163732698a52e83b622ad46c98faf4cd4946
Reviewed-on: http://gerrit.tine20.com/customers/5021
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Tinebase/User.php

index ddf6325..09b0d1c 100644 (file)
@@ -483,10 +483,21 @@ class Tinebase_User
             }
             
             if ($user->visibility !== Tinebase_Model_FullUser::VISIBILITY_HIDDEN) {
-                self::createContactForSyncedUser($user);
+                $contact = self::createContactForSyncedUser($user);
+            } else {
+                $contact = null;
             }
             Tinebase_Timemachine_ModificationLog::setRecordMetaData($user, 'create');
-            $syncedUser = $userBackend->addUserInSqlBackend($user);
+            try {
+                $syncedUser = $userBackend->addUserInSqlBackend($user);
+            } catch (Zend_Db_Statement_Exception $zdse) {
+                Tinebase_Exception::log($zdse);
+                // something happened during user creation: rolling back contact
+                if ($contact) {
+                    $addressbook = Addressbook_Backend_Factory::factory(Addressbook_Backend_Factory::SQL);
+                    $addressbook->delete($contact->getId());
+                }
+            }
             $userBackend->addPluginUser($syncedUser, $user);
         }
         
@@ -702,11 +713,12 @@ class Tinebase_User
      * create contact in addressbook
      * 
      * @param Tinebase_Model_FullUser $user
+     * @return Addressbook_Model_Contact|null
      */
     public static function createContactForSyncedUser($user)
     {
         if (! Tinebase_Application::getInstance()->isInstalled('Addressbook')) {
-            return;
+            return null;
         }
         
         $contact = self::_user2Contact($user);
@@ -721,6 +733,8 @@ class Tinebase_User
             . " Added contact " . $contact->n_given);
         
         $user->contact_id = $contact->getId();
+
+        return $contact;
     }
     
     /**