Tinebase CLI - updateAllAccountsWithAccountEmail - set imap/smtpUser if not set
authorPhilipp Schüle <p.schuele@metaways.de>
Thu, 3 Aug 2017 16:42:21 +0000 (18:42 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 4 Aug 2017 09:35:50 +0000 (11:35 +0200)
* checks and corrects domains for email users
* also fixes a problem with removeDestinations for postfix multiinstance

Change-Id: Ic452f3e11b0f649ad80c2d3edd025b7a1a1a5000
Reviewed-on: http://gerrit.tine20.com/customers/5393
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Tinebase/EmailUser.php
tine20/Tinebase/EmailUser/Smtp/Postfix.php
tine20/Tinebase/EmailUser/Smtp/PostfixCombined.php
tine20/Tinebase/EmailUser/Smtp/PostfixMultiInstance.php
tine20/Tinebase/EmailUser/Sql.php
tine20/Tinebase/Frontend/Cli.php

index b8b9acf..0cb47fa 100644 (file)
@@ -360,4 +360,25 @@ class Tinebase_EmailUser
         
         return self::$_configs[$_configType];
     }
+
+    /**
+     * @param array|null $config
+     * @return array
+     */
+    public static function getAllowedDomains($config = null)
+    {
+        if ($config === null) {
+            $config = Tinebase_Config::getInstance()->get(Tinebase_Config::SMTP)->toArray();
+        }
+
+        $allowedDomains = array();
+        if (! empty($config['primarydomain'])) {
+            $allowedDomains = array($config['primarydomain']);
+            if (! empty($config['secondarydomains'])) {
+                // merge primary and secondary domains and split secondary domains + trim whitespaces
+                $allowedDomains = array_merge($allowedDomains, preg_split('/\s*,\s*/', $config['secondarydomains']));
+            }
+        }
+        return $allowedDomains;
+    }
 }
index 0f876cb..3063e61 100644 (file)
@@ -147,15 +147,7 @@ class Tinebase_EmailUser_Smtp_Postfix extends Tinebase_EmailUser_Sql implements
         
         // set domain from smtp config
         $this->_config['domain'] = !empty($this->_config['primarydomain']) ? $this->_config['primarydomain'] : null;
-        
-        // add allowed domains
-        if (! empty($this->_config['primarydomain'])) {
-            $this->_config['alloweddomains'] = array($this->_config['primarydomain']);
-            if (! empty($this->_config['secondarydomains'])) {
-                // merge primary and secondary domains and split secondary domains + trim whitespaces
-                $this->_config['alloweddomains'] = array_merge($this->_config['alloweddomains'], preg_split('/\s*,\s*/', $this->_config['secondarydomains']));
-            } 
-        }
+        $this->_config['alloweddomains'] = Tinebase_EmailUser::getAllowedDomains($this->_config);
         
         $this->_clientId = Tinebase_Core::getTinebaseId();
         
index 8ae894b..920140d 100644 (file)
@@ -68,15 +68,7 @@ class Tinebase_EmailUser_Smtp_PostfixCombined extends Tinebase_EmailUser_Sql imp
         
         // set domain from smtp config
         $this->_config['domain'] = !empty($this->_config['primarydomain']) ? $this->_config['primarydomain'] : null;
-        
-        // add allowed domains
-        if (! empty($this->_config['primarydomain'])) {
-            $this->_config['alloweddomains'] = array($this->_config['primarydomain']);
-            if (! empty($this->_config['secondarydomains'])) {
-                // merge primary and secondary domains and split secondary domains + trim whitespaces
-                $this->_config['alloweddomains'] = array_merge($this->_config['alloweddomains'], preg_split('/\s*,\s*/', $this->_config['secondarydomains']));
-            } 
-        }
+        $this->_config['alloweddomains'] = Tinebase_EmailUser::getAllowedDomains($this->_config);
         
         $this->_destinationTable = $this->_config['prefix'] . $this->_config['destinationTable'];
         $this->_forwardsTable    = $this->_config['prefix'] . $this->_config['forwardsTable'];
index a1ce031..94924c3 100644 (file)
@@ -159,18 +159,10 @@ class Tinebase_EmailUser_Smtp_PostfixMultiInstance extends Tinebase_EmailUser_Sq
     {
         parent::__construct($_options);
         
-        // set domain from smtp config
+        // set domain and allowed domains from smtp config
         $this->_config['domain'] = !empty($this->_config['primarydomain']) ? $this->_config['primarydomain'] : null;
-        
-        // add allowed domains
-        if (! empty($this->_config['primarydomain'])) {
-            $this->_config['alloweddomains'] = array($this->_config['primarydomain']);
-            if (! empty($this->_config['secondarydomains'])) {
-                // merge primary and secondary domains and split secondary domains + trim whitespaces
-                $this->_config['alloweddomains'] = array_merge($this->_config['alloweddomains'], preg_split('/\s*,\s*/', $this->_config['secondarydomains']));
-            } 
-        }
-        
+        $this->_config['alloweddomains'] = Tinebase_EmailUser::getAllowedDomains($this->_config);
+
         $this->_clientId = Tinebase_Core::getTinebaseId();
         
         $this->_destinationTable = $this->_config['prefix'] . $this->_config['destinationTable'];
@@ -263,7 +255,7 @@ class Tinebase_EmailUser_Smtp_PostfixMultiInstance extends Tinebase_EmailUser_Sq
             $_smtpSettings['id'] = $this->_db->lastInsertId();
         }
 
-        $this->_removeDestinations($_smtpSettings[$this->_propertyMapping['emailUserId']]);
+        $this->_removeDestinations($_smtpSettings['id']);
         
         // check if it should be forward only
         if (! $_smtpSettings[$this->_propertyMapping['emailForwardOnly']]) {
index 189d6a9..41ead77 100644 (file)
@@ -456,7 +456,8 @@ abstract class Tinebase_EmailUser_Sql extends Tinebase_User_Plugin_Abstract
             
         } catch (Zend_Db_Statement_Exception $zdse) {
             Tinebase_TransactionManager::getInstance()->rollBack();
-            Tinebase_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . ' Error while updating email user: ' . $zdse->getMessage());
+            Tinebase_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . ' Error while updating email user');
+            Tinebase_Exception::log($zdse);
         }
     }
     
@@ -567,5 +568,4 @@ abstract class Tinebase_EmailUser_Sql extends Tinebase_User_Plugin_Abstract
             }
         }
     }
-
 }
index 46fc322..9fc12ed 100644 (file)
@@ -1788,9 +1788,19 @@ class Tinebase_Frontend_Cli extends Tinebase_Frontend_Cli_Abstract
         }
 
         $userController = Tinebase_User::getInstance();
+        $emailUser = Tinebase_EmailUser::getInstance();
+        $allowedDomains = Tinebase_EmailUser::getAllowedDomains();
         /** @var Tinebase_Model_FullUser $user */
         foreach ($userController->getFullUsers() as $user) {
-            if (!empty($user->accountEmailAddress)) {
+            $emailUser->inspectGetUserByProperty($user);
+            if (! empty($user->accountEmailAddress)) {
+                list($userPart, $domainPart) = explode('@', $user->accountEmailAddress);
+                if (count($allowedDomains) > 0 && ! in_array($domainPart, $allowedDomains)) {
+                    $newEmailAddress = $userPart . '@' . $allowedDomains[0];
+                    if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__
+                        . ' Setting new email address for user to comply with allowed domains: ' . $newEmailAddress);
+                    $user->accountEmailAddress = $newEmailAddress;
+                }
                 $userController->updateUser($user);
             }
         }