0010710: Shorten Usernames
authorsstamer <s.stamer@metaways.de>
Wed, 7 Jan 2015 13:28:23 +0000 (14:28 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 8 Jan 2015 13:04:44 +0000 (14:04 +0100)
https://forge.tine20.org/view.php?id=10710

Change-Id: I7012154dea1b469bef78838c5aa6b442af48d1a2
Reviewed-on: http://gerrit.tine20.com/customers/1512
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Courses/Controller/Course.php
tine20/Tinebase/Model/FullUser.php
tine20/Tinebase/User/Abstract.php

index 3c7464b..99b34f5 100644 (file)
@@ -400,6 +400,9 @@ class Courses_Controller_Course extends Tinebase_Controller_Record_Abstract
             
             if ($this->_config->get(Courses_Config::STUDENT_LOGINNAME_PREFIX, FALSE) && ($position = strrpos($user->accountLoginName, '-')) !== false) {
                 $user->accountLoginName = $courseName . '-' . substr($user->accountLoginName, $position + 1);
+                
+                //short User name
+                $user->accountLoginName = $user->shortenUsername();
             }
             
             $user->accountPrimaryGroup  = $course->group_id;
index 584d45c..3655af7 100644 (file)
@@ -90,7 +90,7 @@ class Tinebase_Model_FullUser extends Tinebase_Model_User
             . ' ' . print_r($options, TRUE));
         
         if (! isset($this->accountLoginName)) {
-            $this->accountLoginName = Tinebase_User::getInstance()->generateUserName($this, (isset($options['userNameSchema'])) ? $options['userNameSchema'] : 1, (Tinebase_Config::getInstance()->get(Tinebase_Config::MAX_USERNAME_LENGTH)));
+            $this->accountLoginName = Tinebase_User::getInstance()->generateUserName($this, (isset($options['userNameSchema'])) ? $options['userNameSchema'] : 1);
             $this->accountFullName = Tinebase_User::getInstance()->generateAccountFullName($this);
         }
         
@@ -110,6 +110,9 @@ class Tinebase_Model_FullUser extends Tinebase_Model_User
             $this->accountLoginName = $options['accountLoginNamePrefix'] . $this->accountLoginName;
         }
         
+        // short username if needed
+        $this->accountLoginName = $this->shortenUsername();
+        
         // add home dir if empty and prefix is given (append login name)
         if (empty($this->accountHomeDirectory) && ! empty($options['accountHomeDirectoryPrefix'])) {
             $this->accountHomeDirectory = $options['accountHomeDirectoryPrefix'] . $this->accountLoginName;
@@ -293,4 +296,19 @@ class Tinebase_Model_FullUser extends Tinebase_Model_User
         
         return $result;
     }
+    
+    
+    /**
+     * Short username to a configured length
+     */
+    public function shortenUsername()
+    {
+        $username = $this->accountLoginName;
+        $maxLoginNameLength = Tinebase_Config::getInstance()->get(Tinebase_Config::MAX_USERNAME_LENGTH);
+        if (!empty($maxLoginNameLength) && strlen($username) > $maxLoginNameLength) {
+            $username = substr($username, 0, $maxLoginNameLength);
+        }
+        
+        return $username;
+    }
 }
index f66ef01..cab6456 100644 (file)
@@ -341,7 +341,7 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
      * @param integer $_schema 0 = lastname (10 chars) / 1 = lastname + 2 chars of firstname / 2 = 1-x chars of firstname + lastname 
      * @return string
      */
-    public function generateUserName($_account, $_schema = 1, $_length = NULL)
+    public function generateUserName($_account, $_schema = 1)
     {
         if (! empty($_account->accountFirstName) && $_schema > 0 && method_exists($this, '_generateUserWithSchema' . $_schema)) {
             $userName = call_user_func_array(array($this, '_generateUserWithSchema' . $_schema), array($_account));
@@ -349,12 +349,9 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
             $userName = strtolower(substr(replaceSpecialChars($_account->accountLastName), 0, 10));
         }
         
-        if (!empty($_length) && (strlen($userName) > $_length)) {
-            $userName = substr($userName, 0, $_length);
-        }
-        $userName = $this->_addSuffixToNameIfExists('accountLoginName', $userName, $_length);
+        $userName = $this->_addSuffixToNameIfExists('accountLoginName', $userName);
         
-        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . '  generated username: ' . $userName);
+        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . '  generated username: ' . $userName . ' with schema: '. $_schema);
         
         return $userName;
     }
@@ -432,7 +429,7 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
      * @param string $_name
      * @return string
      */
-    protected function _addSuffixToNameIfExists($_property, $_name, $_length)
+    protected function _addSuffixToNameIfExists($_property, $_name)
     {
         $result = $_name;
         if ($this->nameExists($_property, $_name)) {
@@ -441,9 +438,6 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
             while ($numSuffix < 100) {
                 $suffix = sprintf('%02d', $numSuffix);
                 
-                if (!empty($_length) && (strlen($_name . $suffix) > $_length)) {
-                    $_name = substr($_name, 0, strlen($_name) - (strlen($_name . $suffix) - $_length));
-                }
                 if (! $this->nameExists($_property, $_name . $suffix)) {
                     $result = $_name . $suffix;
                     break;