10068: Add suffix to AccountFullName
authorsstamer <s.stamer@metaways.de>
Thu, 17 Jul 2014 14:37:37 +0000 (16:37 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 18 Jul 2014 07:59:18 +0000 (09:59 +0200)
https://forge.tine20.org/mantisbt/view.php?id=10068

- needed for active Directory

Change-Id: Iff25dc4208d6201b495282f7c92ac75c1d5778f9
Reviewed-on: http://gerrit.tine20.com/customers/881
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Tinebase/User/AbstractTest.php
tine20/Tinebase/Model/FullUser.php
tine20/Tinebase/User/Abstract.php

index 97ef969..f29bd67 100644 (file)
@@ -90,6 +90,40 @@ class Tinebase_User_AbstractTest extends PHPUnit_Framework_TestCase
         $this->_uit->deleteUsers($createdUserIds);
     }
     
+    /**
+     * test User with the same Name
+     * Suffix for accountLoginName and accountFullName
+     */
+    public function testDoubleUserNames()
+    {
+        $user1 = new Tinebase_Model_FullUser(array(
+            'accountFirstName' => 'Leonie',
+            'accountLastName'  => 'Weiss',
+            'accountPrimaryGroup' => Tinebase_Core::getUser()->accountPrimaryGroup
+        ), true);
+        $user1->accountLoginName = $this->_uit->generateUserName($user1);
+        $user1->accountFullName = $this->_uit->generateAccountFullName($user1);
+        $this->_uit->addUser($user1);
+        
+        $user2 = new Tinebase_Model_FullUser(array(
+            'accountFirstName' => 'Leonie',
+            'accountLastName'  => 'Weiss',
+            'accountPrimaryGroup' => Tinebase_Core::getUser()->accountPrimaryGroup
+        ), true);
+        $user2->accountLoginName = $this->_uit->generateUserName($user2);
+        $user2->accountFullName = $this->_uit->generateAccountFullName($user2);
+        $this->_uit->addUser($user2);
+        
+        $this->assertEquals('weissle', $user1->accountLoginName);
+        $this->assertEquals('weissle00', $user2->accountLoginName);
+        
+        $this->assertEquals('Leonie Weiss', $user1->accountFullName);
+        $this->assertEquals('Leonie Weiss00', $user2->accountFullName);
+        
+        $this->_uit->deleteUser($user1->getId());
+        $this->_uit->deleteUser($user2->getId());
+    }
+    
     public function testCachePassword()
     {
         //Tinebase_User::getInstance()->cachePassword('secret');
index d6c9d4f..5265de4 100644 (file)
@@ -92,6 +92,7 @@ class Tinebase_Model_FullUser extends Tinebase_Model_User
         // create valid login name
         if (! isset($this->accountLoginName)) {
             $this->accountLoginName = Tinebase_User::getInstance()->generateUserName($this, (isset($options['userNameSchema'])) ? $options['userNameSchema'] : 1);
+            $this->accountFullName = Tinebase_User::getInstance()->generateAccountFullName($this);
         }
         
         if (empty($this->accountPrimaryGroup)) {
index 50fcd2f..d0f07ae 100644 (file)
@@ -350,7 +350,7 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
                 $userName = strtolower(replaceSpecialChars(substr($_account->accountLastName, 0, 10)));
             }
         }
-        $userName = $this->_addSuffixToUsernameIfExists($userName);
+        $userName = $this->_addSuffixToNameIfExists('accountLoginName', $userName);
         
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . '  generated username: ' . $userName);
         
@@ -358,6 +358,18 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
     }
     
     /**
+     * Full name generation for user with the same name
+     * this is needed for active directory because accountFullName is used for the dn
+     *
+     * @param Tinebase_Model_FullUser $_account
+     * @return string
+     */
+    public function generateAccountFullName($_account)
+    {
+        return $this->_addSuffixToNameIfExists('accountFullName', $_account->accountFullName);
+    }
+    
+    /**
      * schema 1 = lastname + 2 chars of firstname
      * 
      * @param Tinebase_Model_FullUser $_account
@@ -381,7 +393,7 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
         for ($i=0; $i < strlen($_account->accountFirstName); $i++) {
         
             $userName = strtolower(replaceSpecialChars(substr($_account->accountFirstName, 0, $i+1) . $_account->accountLastName));
-            if (! $this->userNameExists($userName)) {
+            if (! $this->nameExists('accountLoginName', $userName)) {
                 $result = $userName;
                 break;
             }
@@ -402,7 +414,7 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
         for ($i=0; $i < strlen($_account->accountFirstName); $i++) {
         
             $userName = strtolower(replaceSpecialChars(substr($_account->accountFirstName, 0, $i+1) . '.' . $_account->accountLastName));
-            if (! $this->userNameExists($userName)) {
+            if (! $this->nameExists('accountLoginName', $userName)) {
                 $result = $userName;
                 break;
             }
@@ -412,22 +424,23 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
     }
     
     /**
-     * add a suffix to username if it already exists
+     * add a suffix to username and AccountFullName
      * 
-     * @param string $_userName
+     * @param string $_property
+     * @param string $_name
      * @return string
      */
-    protected function _addSuffixToUsernameIfExists($_userName)
+    protected function _addSuffixToNameIfExists($_property, $_name)
     {
-        $result = $_userName;
-        if ($this->userNameExists($_userName)) {
+        $result = $_name;
+        if ($this->nameExists($_property, $_name)) {
             $numSuffix = 0;
         
             while ($numSuffix < 100) {
                 $suffix = sprintf('%02d', $numSuffix);
         
-                if (! $this->userNameExists($_userName . $suffix)) {
-                    $result = $_userName . $suffix;
+                if (! $this->nameExists($_property, $_name . $suffix)) {
+                    $result = $_name . $suffix;
                     break;
                 }
         
@@ -506,18 +519,19 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
     }
     
     /**
-     * checks if username already exists
+     * checks if accountLoginName/accountFullName already exists
      *
-     * @param   string  $_username
+     * @param   string  $_property
+     * @param   string  $_value
      * @return  bool    
      * 
      */
-    public function userNameExists($_username)
+    public function nameExists($_property, $_value)
     {
         try {
-            $this->getUserByLoginName($_username)->getId();
+            $this->getUserByProperty($_property, $_value)->getId();
         } catch (Tinebase_Exception_NotFound $e) {
-            // username not found
+            // username or full name not found
             return false;
         }