0011192: LDAP sync should delete contacts
authorPhilipp Schüle <p.schuele@metaways.de>
Mon, 31 Aug 2015 13:59:49 +0000 (15:59 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 18 Nov 2015 12:28:38 +0000 (13:28 +0100)
* expires the users before deleting them
* users need to be expired for some time (1 year) before they are deleted
* removes user contact, too

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

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

index 916de42..2d8d2e6 100644 (file)
@@ -238,7 +238,7 @@ class Tinebase_User_LdapTest extends TestCase
             'accountPrimaryGroup'   => Tinebase_Group::getInstance()->getDefaultGroup()->id,
             'accountLastName'       => 'Tine 2.0',
             'accountFirstName'      => 'PHPUnit User',
             'accountPrimaryGroup'   => Tinebase_Group::getInstance()->getDefaultGroup()->id,
             'accountLastName'       => 'Tine 2.0',
             'accountFirstName'      => 'PHPUnit User',
-            'accountEmailAddress'   => 'phpunit@' . $emailDomain
+            'accountEmailAddress'   => 'phpunit@' . $emailDomain,
         ));
         
         return $user;
         ));
         
         return $user;
@@ -251,6 +251,11 @@ class Tinebase_User_LdapTest extends TestCase
     {
         $user = $this->testAddUser();
 
     {
         $user = $this->testAddUser();
 
+        // add user contact
+        $contact = Admin_Controller_User::getInstance()->createOrUpdateContact($user);
+        $user->contact_id = $contact->getId();
+        Tinebase_User::getInstance()->updateUser($user);
+
         // delete user in ldap
         Tinebase_User::getInstance()->deleteUserInSyncBackend($user->getId());
 
         // delete user in ldap
         Tinebase_User::getInstance()->deleteUserInSyncBackend($user->getId());
 
@@ -262,6 +267,23 @@ class Tinebase_User_LdapTest extends TestCase
         $syncOptions = array('deleteUsers' => true);
         Tinebase_User::syncUsers($syncOptions);
 
         $syncOptions = array('deleteUsers' => true);
         Tinebase_User::syncUsers($syncOptions);
 
+        $now = Tinebase_DateTime::now();
+        $user = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $user->getId(), 'Tinebase_Model_FullUser');
+        $this->assertEquals($now->toString(), $user->accountExpires->toString(), 'user should be expired');
+
+        sleep(1);
+        Tinebase_User::syncUsers($syncOptions);
+        $user = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $user->getId(), 'Tinebase_Model_FullUser');
+        $this->assertEquals($now->toString(), $user->accountExpires->toString(), 'expiry date should still be the same');
+
+        // set expired to -1 year -> user should be deleted
+        $user->accountExpires = $now->subYear(1);
+        Tinebase_User::getInstance()->updateUserInSqlBackend($user);
+
+        // sync again
+        sleep(1);
+        Tinebase_User::syncUsers($syncOptions);
+
         // check if user is deleted in tine, too
         try {
             Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $user->getId());
         // check if user is deleted in tine, too
         try {
             Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $user->getId());
@@ -269,5 +291,13 @@ class Tinebase_User_LdapTest extends TestCase
         } catch (Tinebase_Exception_NotFound $tenf) {
             $this->assertContains('User with accountId = ' . $sqlUser->getId(), $tenf->getMessage());
         }
         } catch (Tinebase_Exception_NotFound $tenf) {
             $this->assertContains('User with accountId = ' . $sqlUser->getId(), $tenf->getMessage());
         }
+
+        // check if user contact is deleted, too
+        try {
+            Addressbook_Controller_Contact::getInstance()->get($sqlUser->contact_id);
+            $this->fail('user contact should be deleted from tine20 db');
+        } catch (Tinebase_Exception_NotFound $tenf) {
+            $this->assertContains('Addressbook_Model_Contact record with id = ' . $sqlUser->contact_id, $tenf->getMessage());
+        }
     }
 }
     }
 }
index cc597b2..5698c6a 100644 (file)
@@ -747,7 +747,35 @@ class Tinebase_User
             . ' About to delete ' . count($deletedInSyncBackend) . ' users in SQL backend...');
 
         foreach ($deletedInSyncBackend as $userToDelete) {
             . ' About to delete ' . count($deletedInSyncBackend) . ' users in SQL backend...');
 
         foreach ($deletedInSyncBackend as $userToDelete) {
-            Tinebase_User::getInstance()->deleteUserInSqlBackend($userToDelete);
+            $user = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $userToDelete, 'Tinebase_Model_FullUser');
+
+            // at first, we expire the user
+            $now = Tinebase_DateTime::now();
+            if (! $user->accountExpires) {
+                if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                    . ' Set expiry date to ' . $now);
+                $user->accountExpires = $now;
+                Tinebase_User::getInstance()->updateUserInSqlBackend($user);
+            } else {
+                if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                    . ' User already expired ' . print_r($user->toArray(), true));
+
+                // TODO make time span configurable?
+                if ($user->accountExpires->isEarlier($now->subYear(1))) {
+                    // if he or she is already expired longer than configured expiry, we remove them!
+                    Tinebase_User::getInstance()->deleteUserInSqlBackend($userToDelete);
+
+                    if (Tinebase_Application::getInstance()->isInstalled('Addressbook') === true && ! empty($user->contact_id)) {
+                        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                            . ' Deleting user contact of ' . $user->accountLoginName);
+
+                        $contactsBackend = Addressbook_Backend_Factory::factory(Addressbook_Backend_Factory::SQL);
+                        $contactsBackend->delete($user->contact_id);
+                    }
+                } else {
+                    // keep user in expiry state
+                }
+            }
         }
     }
 
         }
     }