Advanced User Cli Import
authorsstamer <s.stamer@metaways.de>
Wed, 16 Jul 2014 10:40:30 +0000 (12:40 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 16 Jul 2014 13:25:45 +0000 (15:25 +0200)
- Allow additional groups
- samba settings

Change-Id: I4cfeaf4f7b46157a65aa3fc22de55f37a497e176
Reviewed-on: http://gerrit.tine20.com/customers/838
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/test_teacher.csv
tine20/Admin/Import/Csv.php
tine20/Admin/Import/definitions/admin_user_import_csv_advanced.xml [new file with mode: 0644]
tine20/Tinebase/Model/FullUser.php

index 3258625..4e10540 100644 (file)
@@ -58,9 +58,15 @@ class Admin_CliTest extends TestCase
         $this->_usernamesToDelete = array('hmaster', 'hmeister', 'hmoster', 'irmeli', 'testuser', 'm.muster');
         
         $this->_groupsToDelete = new Tinebase_Record_RecordSet('Tinebase_Model_Group');
+        $this->_testGroup['domainuser'] = Tinebase_Group::getInstance()->create(new Tinebase_Model_Group(array(
+            'name'   => 'domainuser'
+        )));
         $this->_testGroup['teacher'] = Tinebase_Group::getInstance()->create(new Tinebase_Model_Group(array(
             'name'   => 'teacher'
         )));
+        $this->_testGroup['student'] = Tinebase_Group::getInstance()->create(new Tinebase_Model_Group(array(
+            'name'   => 'student'
+        )));
         $this->_groupsToDelete->addRecord($this->_testGroup['teacher']);
         
         $this->objects['config'] = '<?xml version="1.0" encoding="UTF-8"?>
@@ -191,14 +197,14 @@ class Admin_CliTest extends TestCase
                 </field>
             </mapping>
         </config>';
-          $this->objects['configGroup'] = '<?xml version="1.0" encoding="UTF-8"?>
+          $this->objects['configAdvanced'] = '<?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>
-           <delimiter>;</delimiter>
+           <delimiter>,</delimiter>
            <mapping>
                <field>
                    <source>firstname</source>
@@ -217,17 +223,41 @@ class Admin_CliTest extends TestCase
                    <destination>password</destination>
                </field>
                <field>
-                   <source>group_id</source>
+                   <source>primary_group_id</source>
                    <destination>accountPrimaryGroup</destination>
                </field>
                <field>
+                   <source>additional_groups</source>
+                   <destination>groups</destination>
+               </field>
+              <field>
                    <source>accountHomeDirectory</source>
                    <destination>accountHomeDirectory</destination>
                </field>
                <field>
+                   <source>accountHomeDirectoryPrefix</source>
+                   <destination>accountHomeDirectoryPrefix</destination>
+               </field>
+               <field>
                    <source>accountLoginShell</source>
                    <destination>accountLoginShell</destination>
                </field>
+               <field>
+                   <source>homePath</source>
+                   <destination>homePath</destination>
+               </field>
+               <field>
+                   <source>homeDrive</source>
+                   <destination>homeDrive</destination>
+               </field>
+               <field>
+                   <source>logonScript</source>
+                   <destination>logonScript</destination>
+               </field>
+               <field>
+                   <source>profilePath</source>
+                   <destination>profilePath</destination>
+               </field>
            </mapping>
         </config>';
     }
@@ -372,10 +402,12 @@ class Admin_CliTest extends TestCase
     }
     
     /**
-     * testImportUsersWithGroup
+     * testImportUsersAdvanced
      */
-    public function testImportUsersWithGroup()
+    public function testImportUsersAdvanced()
     {
+        $userBackend = Tinebase_User::getInstance();
+        
         $readFile = fopen(dirname(__FILE__) . '/files/test_teacher.csv', 'r');
         $writeFile = fopen('test2.csv', 'w');
         $delimiter = ',';
@@ -383,7 +415,9 @@ class Admin_CliTest extends TestCase
         
         while (($row = fgetcsv($readFile)) !== false) {
             foreach ($row as $colIndex => &$field) {
-                $field = str_replace('GROUP', $this->_testGroup['teacher']->getId(), $field);
+                $field = str_replace('PRIMARYGROUP', $this->_testGroup['domainuser']->getId(), $field);
+                $field = str_replace('GROUP1', $this->_testGroup['teacher']->getId(), $field);
+                $field = str_replace('GROUP2', $this->_testGroup['student']->getId(), $field);
             }
             fputcsv($writeFile, $row, $delimiter, $enclosure);
         }
@@ -391,13 +425,23 @@ class Admin_CliTest extends TestCase
         fclose($readFile);
         fclose($writeFile);
         
-        $this->_importUsers($this->objects['configGroup'], 'test2.csv', 'admin_user_import_csv_test_teacher');
+        $this->_importUsers($this->objects['configAdvanced'], 'test2.csv', 'admin_user_import_csv_test_advanced');
         $newUser = Tinebase_User::getInstance()->getFullUserByLoginName('m.muster');
+        
         $newUserMemberships = Tinebase_Group::getInstance()->getGroupMemberships($newUser);
+        $this->assertTrue(in_array($this->_testGroup['domainuser']->getId(), $newUserMemberships),
+        ' not member of the domainuser group (' . $this->_testGroup['domainuser']->getId() . ') ' . print_r($newUserMemberships, TRUE));
+        $this->assertTrue(in_array($this->_testGroup['student']->getId(), $newUserMemberships),
+        ' not member of the student group (' . $this->_testGroup['student']->getId() . ') ' . print_r($newUserMemberships, TRUE));
         $this->assertTrue(in_array($this->_testGroup['teacher']->getId(), $newUserMemberships),
-        ' not member of the test group (' . $this->_testGroup['teacher']->getId() . ') ' . print_r($newUserMemberships, TRUE));
+        ' not member of the teacher group (' . $this->_testGroup['teacher']->getId() . ') ' . print_r($newUserMemberships, TRUE));
+        
         $this->assertEquals('/bin/false', $newUser->accountLoginShell);
+        $this->assertEquals('/storage/lehrer/m.muster', $newUser->accountHomeDirectory);
         
+        if (array_key_exists('Tinebase_User_Plugin_Samba', $userBackend->getPlugins())) {
+            $this->assertEquals('\\fileserver\profiles\m.muster', $newUser->sambaSAM->profilePath);
+        }
         unlink("test2.csv");
     }
 }
index b8db165..8634de8 100644 (file)
@@ -1,2 +1,2 @@
-objectname;firstname;lastname;password;accountLoginShell;accountHomeDirectory;group_id
-m.muster;Max;Muster;foo;/bin/false;/storage/lehrer/m.muster;GROUP
+"objectname","firstname","lastname","password","accountLoginShell","accountHomeDirectory","accountHomeDirectoryPrefix","primary_group_id","additional_groups","homePath","homeDrive","logonScript","profilePath"
+"m.muster","Max","Muster","foo","/bin/false","/storage/lehrer/m.muster","/storage/lehrer/","PRIMARYGROUP","GROUP1, GROUP2","\\\\fileserver\\","U:","lehrer.cmd","\\\\fileserver\\profiles\\"
index d5d7d21..b85dca6 100644 (file)
@@ -80,14 +80,19 @@ class Admin_Import_Csv extends Tinebase_Import_Csv_Abstract
             
             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']);
             }
+            if (isset($_recordData['samba'])) {
+                $this->_options['samba'] = $_recordData['samba'];
+            }
+            if (isset($_recordData['accountHomeDirectoryPrefix'])) {
+                $this->_options['accountHomeDirectoryPrefix'] = $_recordData['accountHomeDirectoryPrefix'];
+            }
+            
             $password = $record->applyOptionsAndGeneratePassword($this->_options, (isset($_recordData['password'])) ? $_recordData['password'] : NULL);
             Tinebase_Event::fireEvent(new Admin_Event_BeforeImportUser($record, $this->_options));
             
@@ -106,7 +111,6 @@ class Admin_Import_Csv extends Tinebase_Import_Csv_Abstract
         } else {
             $record = parent::_importRecord($_record, $_resolveStrategy, $_recordData);
         }
-        
         return $record;
     }
     
@@ -118,6 +122,16 @@ class Admin_Import_Csv extends Tinebase_Import_Csv_Abstract
                 'emailForwards'    => isset($result['emailForwards']) && !empty($result['emailForwards']) ? explode(' ', trim($result['emailForwards'])) : array(),
                 'emailAliases'     => isset($result['emailAliases']) ? explode(' ', trim($result['emailAliases'])) : array()
                             );
+            $result['groups'] = !empty($result['groups']) ? array_map('trim',explode(",",$result['groups'])) : array();
+            
+            $result['samba'] = array(
+                'homePath'      => (isset($result['homePath'])) ? stripslashes($result['homePath']) : '',
+                'homeDrive'     => (isset($result['homeDrive'])) ? stripslashes($result['homeDrive']) : '',
+                'logonScript'   => (isset($result['logonScript'])) ? $result['logonScript'] : '',
+                'profilePath'   => (isset($result['profilePath'])) ? stripslashes($result['profilePath']) : '',
+                'pwdCanChange'  => isset($result['pwdCanChange'])  ? new Tinebase_DateTime($result['pwdCanChange']) : '',
+                'pwdMustChange' => isset($result['pwdMustChange']) ? new Tinebase_DateTime($result['pwdMustChange']) : ''
+                            );
         return $result;
     }
 }
diff --git a/tine20/Admin/Import/definitions/admin_user_import_csv_advanced.xml b/tine20/Admin/Import/definitions/admin_user_import_csv_advanced.xml
new file mode 100644 (file)
index 0000000..83a0ee3
--- /dev/null
@@ -0,0 +1,63 @@
+<?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>
+       <delimiter>,</delimiter>
+       <mapping>
+           <field>
+               <source>firstname</source>
+               <destination>accountFirstName</destination>
+           </field>
+           <field>
+               <source>lastname</source>
+               <destination>accountLastName</destination>
+           </field>
+           <field>
+               <source>objectname</source>
+               <destination>accountLoginName</destination>
+           </field>
+           <field>
+               <source>password</source>
+               <destination>password</destination>
+           </field>
+           <field>
+               <source>primary_group_id</source>
+               <destination>accountPrimaryGroup</destination>
+           </field>
+           <field>
+               <source>additional_groups</source>
+               <destination>groups</destination>
+           </field>
+           <field>
+               <source>accountHomeDirectory</source>
+               <destination>accountHomeDirectory</destination>
+           </field>
+           <field>
+               <source>accountHomeDirectoryPrefix</source>
+               <destination>accountHomeDirectoryPrefix</destination>
+           </field>
+           <field>
+               <source>accountLoginShell</source>
+               <destination>accountLoginShell</destination>
+           </field>
+           <field>
+               <source>homePath</source>
+               <destination>homePath</destination>
+           </field>
+           <field>
+               <source>homeDrive</source>
+               <destination>homeDrive</destination>
+           </field>
+           <field>
+               <source>logonScript</source>
+               <destination>logonScript</destination>
+           </field>
+           <field>
+               <source>profilePath</source>
+               <destination>profilePath</destination>
+           </field>
+       </mapping>
+    </config>
index febcff6..d6c9d4f 100644 (file)
@@ -94,7 +94,7 @@ class Tinebase_Model_FullUser extends Tinebase_Model_User
             $this->accountLoginName = Tinebase_User::getInstance()->generateUserName($this, (isset($options['userNameSchema'])) ? $options['userNameSchema'] : 1);
         }
         
-        if (! isset($this->accountPrimaryGroup)) {
+        if (empty($this->accountPrimaryGroup)) {
             if (! empty($options['group_id'])) {
                 $groupId = $options['group_id'];
             } else {