Admin Import for emailUsers
authorsstamer <s.stamer@metaways.de>
Wed, 18 Jun 2014 15:03:09 +0000 (17:03 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 18 Jun 2014 15:17:30 +0000 (17:17 +0200)
Change-Id: I39fd6a2d23aad2a6dcfde71e3c005cfa2d454d2c
Reviewed-on: http://gerrit.tine20.com/customers/739
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Admin/CliTest.php
tests/tine20/Admin/files/tine_user5.csv [new file with mode: 0644]
tine20/Admin/Import/Csv.php
tine20/Admin/Import/definitions/admin_user_import_csv_for_emailuser.xml [new file with mode: 0644]
tine20/Admin/Setup/Update/Release8.php [new file with mode: 0644]
tine20/Admin/Setup/setup.xml
tine20/Tinebase/Import/Abstract.php
tine20/Tinebase/Import/Csv/Abstract.php
tine20/Tinebase/Model/FullUser.php

index 8a9045d..e12517d 100644 (file)
@@ -40,9 +40,10 @@ class Admin_CliTest extends TestCase
     {
         parent::setUp();
         
+        
         $this->_cli = new Admin_Frontend_Cli();
         
-        $this->_usernamesToDelete = array('hmaster', 'hmeister', 'hmoster', 'irmeli');
+        $this->_usernamesToDelete = array('hmaster', 'hmeister', 'hmoster', 'irmeli', 'testuser');
         
         $this->objects['config'] = '<?xml version="1.0" encoding="UTF-8"?>
         <config>
@@ -133,8 +134,47 @@ class Admin_CliTest extends TestCase
                 </field>
             </mapping>
         </config>';
+        $this->objects['configEmailuser'] = '<?xml version="1.0" encoding="UTF-8"?>
+        <config>
+            <model>Tinebase_Model_FullUser</model>
+            <plugin>Admin_Import_Csv</plugin>
+            <type>import</type>
+            <headline>1</headline>
+            <dryrun>0</dryrun>
+            <extension>csv</extension>
+            <mapping>
+                <field>
+                    <source>firstname</source>
+                    <destination>accountFirstName</destination>
+                </field>
+                <field>
+                    <source>lastname</source>
+                    <destination>accountLastName</destination>
+                </field>
+                <field>
+                    <source>loginname</source>
+                    <destination>accountLoginName</destination>
+                </field>
+                <field>
+                    <source>password</source>
+                    <destination>password</destination>
+                </field>
+                <field>
+                    <source>email</source>
+                    <destination>accountEmailAddress</destination>
+                </field>
+                <field>
+                    <source>emailAliases</source> <!-- leerzeichen separator -->
+                    <destination>emailAliases</destination>
+                </field>
+                <field>
+                    <source>emailForwards</source>
+                    <destination>emailForwards</destination>
+                </field>
+            </mapping>
+        </config>';
     }
-
+    
     /**
      * test to import admin users
      *
@@ -225,4 +265,40 @@ class Admin_CliTest extends TestCase
         $out = $this->_importUsers($this->objects['configSemicolon'], dirname(__FILE__) . '/files/tine_user3.csv', 'admin_user_import_csv_test_semicolon');
         $this->_checkResult($out, 'irmeli');
     }
+    
+    /**
+     * testImportUsersWithEmailUser
+     */
+    public function testImportUsersWithEmailUser()
+    {
+        $userBackend = Tinebase_User::getInstance();
+        $config = TestServer::getInstance()->getConfig();
+        $maildomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
+
+        $readFile = fopen(dirname(__FILE__) . '/files/tine_user5.csv', 'r');
+        $writeFile = fopen('test.csv', 'w');
+        $delimiter = ',';
+        $enclosure = '"';
+        
+        while (($row = fgetcsv($readFile)) !== false) {
+            foreach ($row as $colIndex => &$field) {
+                $field = str_replace('DOMAIN', $maildomain, $field);
+            }
+            fputcsv($writeFile, $row, $delimiter, $enclosure);
+        }
+        
+        fclose($readFile);
+        fclose($writeFile);
+        
+        if (! array_key_exists('Tinebase_EmailUser_Smtp_Postfix', $userBackend->getPlugins())) {
+            $this->markTestSkipped('Postfix SQL plugin not enabled');
+        }
+        
+        $this->_importUsers($this->objects['configEmailuser'], 'test.csv', 'admin_user_import_csv_test_emailuser');
+        $newUser = $userBackend->getFullUserByLoginName('testuser');
+        $this->assertEquals(array('contact@' . $maildomain, 'kontakt@' . $maildomain), $newUser->smtpUser->emailAliases);
+        $this->assertEquals(array('test@' . $maildomain), $newUser->smtpUser->emailForwards);
+        $this->assertTrue($newUser->smtpUser->emailForwardOnly);
+        unlink("test.csv");
+    }
 }
diff --git a/tests/tine20/Admin/files/tine_user5.csv b/tests/tine20/Admin/files/tine_user5.csv
new file mode 100644 (file)
index 0000000..f0c1498
--- /dev/null
@@ -0,0 +1,2 @@
+firstname,lastname,loginname,password,email,emailAliases,emailForwards
+,a,Testuser,tollespw,info@DOMAIN,contact@DOMAIN kontakt@DOMAIN,test@DOMAIN
index 176d374..d5d7d21 100644 (file)
@@ -77,11 +77,19 @@ class Admin_Import_Csv extends Tinebase_Import_Csv_Abstract
         if ($_record instanceof Tinebase_Model_FullUser && $this->_controller instanceof Admin_Controller_User) {
             
             $record = $_record;
+            
+            if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ 
+                . ' record Data' . print_r($_recordData, true));
+            if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ 
+                . ' ' . print_r($record->toArray(), true));
+            if (isset($_recordData['smtpUser'])) {
+                $record->smtpUser = new Tinebase_Model_EmailUser($_recordData['smtpUser']);
+            }
+            if (isset($_recordData['imapUser'])) {
+                $record->imapUser = new Tinebase_Model_EmailUser($_recordData['imapUser']);
+            }
             $password = $record->applyOptionsAndGeneratePassword($this->_options, (isset($_recordData['password'])) ? $_recordData['password'] : NULL);
-
             Tinebase_Event::fireEvent(new Admin_Event_BeforeImportUser($record, $this->_options));
-
-            if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($record->toArray(), true));
             
             // try to create record with password
             if ($record->isValid()) {
@@ -101,4 +109,15 @@ class Admin_Import_Csv extends Tinebase_Import_Csv_Abstract
         
         return $record;
     }
+    
+    protected function _doMapping($_data)
+    {
+        $result = parent::_doMapping($_data);
+            $result['smtpUser'] = array(
+                'emailForwardOnly' => isset($_recordData['emailForwardOnly']) ? $_recordData['emailForwardOnly'] : true,
+                'emailForwards'    => isset($result['emailForwards']) && !empty($result['emailForwards']) ? explode(' ', trim($result['emailForwards'])) : array(),
+                'emailAliases'     => isset($result['emailAliases']) ? explode(' ', trim($result['emailAliases'])) : array()
+                            );
+        return $result;
+    }
 }
diff --git a/tine20/Admin/Import/definitions/admin_user_import_csv_for_emailuser.xml b/tine20/Admin/Import/definitions/admin_user_import_csv_for_emailuser.xml
new file mode 100644 (file)
index 0000000..c70bcf6
--- /dev/null
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<config>
+    <model>Tinebase_Model_FullUser</model>
+    <plugin>Admin_Import_Csv</plugin>
+    <type>import</type>
+    <headline>1</headline>
+    <dryrun>0</dryrun>
+    <extension>csv</extension>
+    <mapping>
+        <field>
+            <source>firstname</source>
+            <destination>accountFirstName</destination>
+        </field>
+        <field>
+            <source>lastname</source>
+            <destination>accountLastName</destination>
+        </field>
+        <field>
+            <source>loginname</source>
+            <destination>accountLoginName</destination>
+        </field>
+        <field>
+            <source>password</source>
+            <destination>password</destination>
+        </field>
+        <field>
+            <source>email</source>
+            <destination>accountEmailAddress</destination>
+        </field>
+        <field>
+            <source>emailAliases</source> <!-- leerzeichen separator -->
+            <destination>emailAliases</destination>
+        </field>
+        <field>
+            <source>emailForwards</source>
+            <destination>emailForwards</destination>
+        </field>
+    </mapping>
+</config>
\ No newline at end of file
diff --git a/tine20/Admin/Setup/Update/Release8.php b/tine20/Admin/Setup/Update/Release8.php
new file mode 100644 (file)
index 0000000..c26de23
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Tine 2.0
+ *
+ * @package     Admin
+ * @license     http://www.gnu.org/licenses/agpl.html AGPL3
+ * @copyright   Copyright (c) 2013 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @author      Stefanie Stamer <s.stamer@metaways.de>
+ */
+
+class Admin_Setup_Update_Release8 extends Setup_Update_Abstract
+{
+    /**
+     * update to 8.1
+     * 
+     * @return void
+     */
+    public function update_0()
+    {
+        Setup_Controller::getInstance()->createImportExportDefinitions(Tinebase_Application::getInstance()->getApplicationByName('Admin'));
+        $this->setApplicationVersion('Admin', '8.1');
+    }
+}
index d690756..63fe47c 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <application>
     <name>Admin</name>
-    <version>8.0</version>
+    <version>8.1</version>
     <order>1</order>
     <depends>
         <application>Tinebase</application>
index 8cd769f..3546a99 100644 (file)
@@ -555,6 +555,10 @@ abstract class Tinebase_Import_Abstract implements Tinebase_Import_Interface
         }
         
         $this->_handleTags($_record, $_resolveStrategy);
+        
+        if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ 
+            . ' Record to import: ' . print_r($_record->toArray(), true));
+        
         $importedRecord = $this->_importAndResolveConflict($_record, $_resolveStrategy);
         
         $this->_importResult['results']->addRecord($importedRecord);
index ce6f42f..cc026c6 100644 (file)
@@ -174,6 +174,7 @@ abstract class Tinebase_Import_Csv_Abstract extends Tinebase_Import_Abstract
         
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__
             . ' Mapped data: ' . print_r($data, true));
+        
         return $data;
     }
     
index b32b70b..514f3be 100644 (file)
@@ -169,12 +169,22 @@ class Tinebase_Model_FullUser extends Tinebase_Model_User
     protected function _addEmailUser($password)
     {
         if (! empty($this->accountEmailAddress)) {
-            $this->imapUser = new Tinebase_Model_EmailUser(array(
-                'emailPassword' => $password
-            ));
-            $this->smtpUser = new Tinebase_Model_EmailUser(array(
-                'emailPassword' => $password
-            ));
+            
+            if (isset($this->imapUser)) {
+                $this->imapUser->emailPassword = $password;
+            } else {
+                $this->imapUser = new Tinebase_Model_EmailUser(array(
+                    'emailPassword' => $password
+                ));
+            }
+            
+            if (isset($this->smtpUser)) {
+                $this->smtpUser->emailPassword = $password;
+            } else {
+                $this->smtpUser = new Tinebase_Model_EmailUser(array(
+                    'emailPassword' => $password
+                ));
+            }
         }
     }