Merge branch '2013.10' into 2014.11
[tine20] / tine20 / Setup / Frontend / Cli.php
index 51c06f9..f140fe7 100644 (file)
@@ -57,7 +57,7 @@ class Setup_Frontend_Cli
         } elseif(isset($_opts->uninstall)) {
             $this->_uninstall($_opts);
         } elseif(isset($_opts->list)) {
-            $this->_listInstalled();
+            $result = $this->_listInstalled();
         } elseif(isset($_opts->sync_accounts_from_ldap)) {
             $this->_importAccounts($_opts);
         } elseif(isset($_opts->sync_passwords_from_ldap)) {
@@ -72,6 +72,14 @@ class Setup_Frontend_Cli
             $this->_createAdminUser($_opts);
         } elseif(isset($_opts->getconfig)) {
             $this->_getConfig($_opts);
+        } elseif(isset($_opts->reset_demodata)) {
+            $this->_resetDemodata($_opts);
+        } elseif(isset($_opts->updateAllImportExportDefinitions)) {
+            $this->_updateAllImportExportDefinitions($_opts);
+        } elseif(isset($_opts->backup)) {
+            $this->_backup($_opts);
+        } elseif(isset($_opts->restore)) {
+            $this->_restore($_opts);
         }
         
         if ($exitAfterHandle) {
@@ -155,8 +163,8 @@ class Setup_Frontend_Cli
                 }
             }
             
-            // initial password
-            if (! isset($_options['adminPassword'])) {
+            // initial password / can be empty => will trigger password change dialogue
+            if (! array_key_exists('adminPassword', $_options)) {
                 $_options['adminPassword'] = $this->_promptPassword();
             }
         }
@@ -264,11 +272,122 @@ class Setup_Frontend_Cli
         }
         
         $controller->uninstallApplications($applications->name);
-
+        
         echo "Successfully uninstalled " . count($applications) . " applications.\n";
     }
+    
+    /**
+     * reinstall applications
+     * and reset Demodata
+     * php setup.php --reset_demodata USERNAME
+     * 
+     * @param Zend_Console_Getopt $_opts
+     */
+    protected function _resetDemodata(Zend_Console_Getopt $_opts)
+    {
+        $controller = Setup_Controller::getInstance();
+        $userController = Admin_Controller_User::getInstance();
+        $containerController = Tinebase_Container::getInstance();
+        $cli = new Tinebase_Frontend_Cli();
+        
+        //Don't reset this applications
+        $fixedApplications = array('Tinebase', 'Admin', 'Addressbook');
+        
+        //Log in
+        $opts = $_opts->getRemainingArgs();
+        $username = $opts[0];
+        if (empty($username)) {
+            echo "Username is missing!\n";
+            exit;
+        }
+        $user = Tinebase_User::getInstance()->getUserByLoginName($username);
+        Tinebase_Core::set(Tinebase_Core::USER, $user);
+        
+        //get all applications and remove some
+        $applications = Tinebase_Application::getInstance()->getApplications(NULL, 'id');
+        
+        foreach ($applications as $key => &$application) {
+            if (in_array($application, $fixedApplications)) {
+                unset($applications[$key]);
+            }
+        }
+        
+        //get set rights
+        $users = Tinebase_Acl_Roles::getInstance()->getRoleByName('user role');
+        $rights = Tinebase_Acl_Roles::getInstance()->getRoleRights($users->getId());
+        
+        //Uninstall Applications
+        try {
+            $controller->uninstallApplications($applications->name);
+            echo "Successfully uninstalled " . count($applications) . " applications.\n";
+        } catch (Tinebase_Exception_NotFound $e) {
+        }
+        //Install Applications
+        try {
+            $controller->installApplications($applications->name);
+            echo "Successfully installed " . count($applications) . " applications.\n";
+        } catch (Tinebase_Exception_NotFound $e) {
+        }
+        
+        //set rights
+        foreach ($applications as &$application) {
+            $newApplicationId = Tinebase_Application::getInstance()->getApplicationByName($application->name)->getId();
+            
+            foreach ($rights as &$right) {
+                if ($right['application_id'] == $application->id) {
+                    $right['application_id'] = $newApplicationId;
+                }
+            }
+            
+        }
+        
+        Tinebase_Acl_Roles::getInstance()->setRoleRights($users->getId(), $rights);
+        echo "Successfully restored user rights.\n";
+        
+        //Clean up addressbooks
+        $internalContacts = $userController->getDefaultInternalAddressbook();
+        $containers = $containerController->getAll();
+        foreach ($containers as $key => &$container) {
+            if ($container->id == $internalContacts) {
+                // Do nothing
+            } else {
+                try {
+                    $containerController->deleteContainer($container, true);
+                } catch (Tinebase_Exception_NotFound $e) {
+                }
+            }
+        }
+        echo "Successfully cleand up containers.\n";
+        
+        //remove state
+        $db = Tinebase_Core::getDb();
+        $statement = "TRUNCATE TABLE " . $db->quoteIdentifier(SQL_TABLE_PREFIX . 'state');
+        $db->query($statement);
+        echo "Successfully truncated state table.\n";
+        
+        //Get Demodata
+        $cli->createAllDemoData();
+        
+        //clear Cache
+        Tinebase_Core::getCache()->clean(Zend_Cache::CLEANING_MODE_ALL);
+        echo "Successfully cleared Cache.\n";
+        
+        echo "Every thing done!\n";
+    }
 
     /**
+     * Update Import Export Definitions for all applications
+     */
+    protected function _updateAllImportExportDefinitions(Zend_Console_Getopt $_opts){
+        //get all applications
+        $applications = Tinebase_Application::getInstance()->getApplications(NULL, 'id');
+        foreach ($applications as $application) {
+            Setup_Controller::getInstance()->createImportExportDefinitions($application);
+            echo "Update definitions for " . $application->name . "...\n";
+        }
+    }
+    
+    /**
      * list installed apps
      */
     protected function _listInstalled()
@@ -277,13 +396,15 @@ class Setup_Frontend_Cli
             $applications = Tinebase_Application::getInstance()->getApplications(NULL, 'id');
         } catch (Zend_Db_Statement_Exception $e) {
             echo "No applications installed\n";
-            return;
+            return 1;
         }
         
         echo "Currently installed applications:\n";
         foreach($applications as $application) {
             echo "* $application\n";
         }
+        
+        return 0;
     }
     
     /**
@@ -313,7 +434,9 @@ class Setup_Frontend_Cli
         if ($_opts->syncdeletedusers) {
             $options['deleteUsers'] = true;
         }
-
+        if ($_opts->syncaccountstatus) {
+            $options['syncAccountStatus'] = true;
+        }
         if ($_opts->syncontactphoto) {
             $options['syncContactPhoto'] = true;
         }
@@ -402,7 +525,7 @@ class Setup_Frontend_Cli
             $errors[] = 'Missing argument: configvalue';
         }
         $configKey = (string)$options['configkey'];
-        $configValue = (is_json($options['configvalue'])) ? Zend_Json::decode($options['configvalue']) : self::parseConfigValue($options['configvalue']);
+        $configValue = self::parseConfigValue($options['configvalue']);
         $applicationName = (isset($options['app'])) ? $options['app'] : 'Tinebase';
         
         if (empty($errors)) {
@@ -461,7 +584,7 @@ class Setup_Frontend_Cli
         }
         
         echo "Please enter a username. If the user already exists, he is reactivated and you can reset the password.\n";
-        $username = Tinebase_Server_Cli::promptInput('Username');
+        $username = strtolower(Tinebase_Server_Cli::promptInput('Username'));
         $tomorrow = Tinebase_DateTime::now()->addDay(1);
         
         try {
@@ -528,22 +651,26 @@ class Setup_Frontend_Cli
     {
         $roleMemberships = Tinebase_Acl_Roles::getInstance()->getRoleMemberships($user->getId());
         $adminRoleFound = FALSE;
+        // TODO allow to configure this / pass it as param
+        $adminRoleName = 'admin role';
+
         foreach ($roleMemberships as $roleId) {
             $role = Tinebase_Acl_Roles::getInstance()->getRoleById($roleId);
-            if ($role->name === 'admin role') {
+            if ($role->name === $adminRoleName) {
                 $adminRoleFound = TRUE;
-                //print_r(Tinebase_Acl_Roles::getInstance()->getRoleRights($role->getId()));
                 break;
             }
         }
-        
+
         if (! $adminRoleFound || ! Tinebase_Acl_Roles::getInstance()->hasRight('Admin', $user->getId(), Tinebase_Acl_Rights::ADMIN)) {
             echo "Admin role not found for user " . $user->accountLoginName . ".\n";
-            $adminRole = new Tinebase_Model_Role(array(
-                'name'                  => 'admin role',
-                'description'           => 'admin role for tine. this role has all rights per default.',
-            ));
-            $adminRole = Tinebase_Acl_Roles::getInstance()->createRole($adminRole);
+
+            try {
+                $adminRole = Tinebase_Acl_Roles::getInstance()->getRoleByName($adminRoleName);
+            } catch (Tinebase_Exception_NotFound $tenf) {
+                $adminRole = $this->_createNewAdminRoleForAdmin($adminRoleName);
+            }
+
             Tinebase_Acl_Roles::getInstance()->setRoleMembers($adminRole->getId(), array(
                 array(
                     'id'    => $user->getId(),
@@ -551,25 +678,48 @@ class Setup_Frontend_Cli
                 )
             ));
             
-            // add all rights for all apps
-            $enabledApps = Tinebase_Application::getInstance()->getApplicationsByState(Tinebase_Application::ENABLED);
-            $roleRights = array();
-            foreach ($enabledApps as $application) {
-                $allRights = Tinebase_Application::getInstance()->getAllRights($application->getId());
-                foreach ($allRights as $right) {
-                    $roleRights[] = array(
-                        'application_id' => $application->getId(),
-                        'right'          => $right,
-                    );
-                }
-            }
-            Tinebase_Acl_Roles::getInstance()->setRoleRights($adminRole->getId(), $roleRights);
-            
-            echo "Created admin role for user " . $user->accountLoginName . ".\n";
+            echo "Added user " . $user->accountLoginName . " to role '$adminRoleName''.\n";
             // @todo clear roles/groups cache
         }
     }
-    
+
+    protected function _createNewAdminRoleForAdmin($adminRoleName)
+    {
+        $adminRole = new Tinebase_Model_Role(array(
+            'name'                  => $adminRoleName,
+            'description'           => 'admin role for tine. this role has all rights per default.',
+        ));
+
+        $adminRole = Tinebase_Acl_Roles::getInstance()->createRole($adminRole);
+        // add all rights for all apps
+        $enabledApps = Tinebase_Application::getInstance()->getApplicationsByState(Tinebase_Application::ENABLED);
+        $roleRights = array();
+        foreach ($enabledApps as $application) {
+            $allRights = Tinebase_Application::getInstance()->getAllRights($application->getId());
+            foreach ($allRights as $right) {
+                $roleRights[] = array(
+                    'application_id' => $application->getId(),
+                    'right'          => $right,
+                );
+            }
+        }
+        Tinebase_Acl_Roles::getInstance()->setRoleRights($adminRole->getId(), $roleRights);
+
+        return $adminRole;
+    }
+
+    protected function _backup(Zend_Console_Getopt $_opts)
+    {
+        $options = $this->_parseRemainingArgs($_opts->getRemainingArgs());
+        Setup_Controller::getInstance()->backup($options);
+    }
+
+    protected function _restore(Zend_Console_Getopt $_opts)
+    {
+        $options = $this->_parseRemainingArgs($_opts->getRemainingArgs());
+        Setup_Controller::getInstance()->restore($options);
+    }
+
     /**
      * parse options
      * 
@@ -580,6 +730,11 @@ class Setup_Frontend_Cli
     {
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($_value, TRUE));
         
+        // check value is json encoded
+        if (Tinebase_Helper::is_json($_value)) {
+            return Zend_Json::decode($_value); 
+        }
+        
         $result = array(
             'active' => 1
         );