0013278: add --setpassword to setup cli
authorPhilipp Schüle <p.schuele@metaways.de>
Wed, 28 Jun 2017 19:21:24 +0000 (21:21 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 30 Jun 2017 06:47:54 +0000 (08:47 +0200)
 * usage:
  setup.php --setpassword -- \
    username=myusername password=myrandompw
 * only works for system users

https://forge.tine20.org/view.php?id=13278

Change-Id: I33d64f5fb9057c487c84b36574d84df368d69756
Reviewed-on: http://gerrit.tine20.com/customers/4979
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/setup/Setup/CliTest.php
tine20/Setup/Frontend/Cli.php
tine20/Setup/Server/Cli.php
tine20/Tinebase/Auth.php

index 1524bfd..3250e7c 100644 (file)
@@ -95,4 +95,16 @@ class Setup_CliTest extends TestCase
 (
 )", $result);
     }
+
+    /**
+     * Test setpassword of replicationuser
+     */
+    public function testSetPassword()
+    {
+        $this->testSetConfig();
+        $result = $this->_cliHelper('setpassword', array('--setpassword','--','username=replicationuser', 'password=xxxx1234'));
+        self::assertEmpty($result);
+        $auth = Tinebase_Auth::getInstance()->authenticate('replicationuser', 'xxxx1234');
+        self::assertTrue($auth->isValid(), print_r($auth, true));
+    }
 }
index c7eae5d..75ce2ac 100644 (file)
@@ -15,6 +15,8 @@
  * This class handles all requests from cli scripts
  *
  * @package     Tinebase
+ *
+ * TODO extend TFCliAbstract
  */
 class Setup_Frontend_Cli
 {
@@ -47,7 +49,15 @@ class Setup_Frontend_Cli
      */
     public function handle(Zend_Console_Getopt $_opts, $exitAfterHandle = true)
     {
-        Setup_Core::set(Setup_Core::USER, 'setupuser');
+        // always set real setup user if Tinebase is installed
+        if (Setup_Controller::getInstance()->isInstalled('Tinebase')) {
+            $setupUser = Setup_Update_Abstract::getSetupFromConfigOrCreateOnTheFly();
+            if (!Setup_Core::getUser() instanceof Tinebase_Model_User) {
+                Setup_Core::set(Tinebase_Core::USER, $setupUser);
+            }
+        } else {
+            Setup_Core::set(Setup_Core::USER, 'setupuser');
+        }
 
         $result = 0;
         if (isset($_opts->install)) {
@@ -84,6 +94,8 @@ class Setup_Frontend_Cli
             $this->_restore($_opts);
         } elseif(isset($_opts->compare)) {
             $this->_compare($_opts);
+        } elseif(isset($_opts->setpassword)) {
+            $this->_setPassword($_opts);
         }
         
         if ($exitAfterHandle) {
@@ -206,7 +218,33 @@ class Setup_Frontend_Cli
         
         return $password1;
     }
-    
+
+    /**
+     * set system user password
+     *
+     * @param Zend_Console_Getopt $_opts
+     * @return integer
+     */
+    protected function _setPassword(Zend_Console_Getopt $_opts)
+    {
+        $options = $this->_parseRemainingArgs($_opts->getRemainingArgs());
+        if (empty($options['username']) || empty($options['password'])) {
+            echo "username and password parameters required\n";
+            return 2;
+        }
+
+        $username = $options['username'];
+        $password = $options['password'];
+        if (! in_array($username, Tinebase_User::getSystemUsernames(), /* strict */ true)) {
+            echo "it's only allowed to set system user passwords here\n";
+            return 2;
+        }
+
+        $user = Tinebase_User::getInstance()->getUserByLoginName($username);
+        Tinebase_User::getInstance()->setPassword($user, $password);
+        return 0;
+    }
+
     /**
      * update existing applications
      *
@@ -396,11 +434,6 @@ class Setup_Frontend_Cli
      */
     protected function _updateAllImportExportDefinitions(Zend_Console_Getopt $_opts){
 
-        $setupUser = Setup_Update_Abstract::getSetupFromConfigOrCreateOnTheFly();
-        if (! Tinebase_Core::getUser() instanceof Tinebase_Model_User) {
-            Tinebase_Core::set(Tinebase_Core::USER, $setupUser);
-        }
-
         //get all applications
         $applications = Tinebase_Application::getInstance()->getApplications(NULL, 'id');
         foreach ($applications as $application) {
@@ -617,11 +650,6 @@ class Setup_Frontend_Cli
             die('Install Tinebase first.');
         }
 
-        $setupUser = Setup_Update_Abstract::getSetupFromConfigOrCreateOnTheFly();
-        if (! Tinebase_Core::getUser() instanceof Tinebase_Model_User) {
-            Tinebase_Core::set(Tinebase_Core::USER, $setupUser);
-        }
-
         echo "Please enter a username. An existing user is reactivated and you can reset the password.\n";
         $username = strtolower(Tinebase_Server_Cli::promptInput('Username'));
         $tomorrow = Tinebase_DateTime::now()->addDay(1);
@@ -841,6 +869,12 @@ class Setup_Frontend_Cli
         return $options;
     }
 
+    /**
+     * compare shema of two tine databases
+     *
+     * @param Zend_Console_Getopt $_opts
+     * @throws Exception
+     */
     protected function _compare(Zend_Console_Getopt $_opts)
     {
         $options = $this->_parseRemainingArgs($_opts->getRemainingArgs());
index c7b5d25..d275679 100644 (file)
@@ -68,6 +68,9 @@ class Setup_Server_Cli implements Tinebase_Server_Interface
                 'compare'                   => 'compare schemas with another database
                         Examples:
                            setup.php --compare -- otherdb=tine20other',
+                'setpassword'               => 'set system user password
+                        Examples:
+                           setup.php --setpassword -- username=myusername password=myrandompw',
             ));
             $opts->parse();
         } catch (Zend_Console_Getopt_Exception $e) {
@@ -93,6 +96,7 @@ class Setup_Server_Cli implements Tinebase_Server_Interface
             empty($opts->backup) &&
             empty($opts->restore) &&
             empty($opts->compare) &&
+            empty($opts->setpassword) &&
             empty($opts->getconfig)))
         {
             echo $opts->getUsageMessage();
@@ -107,10 +111,10 @@ class Setup_Server_Cli implements Tinebase_Server_Interface
         
         Setup_Core::initFramework();
 
-        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ .' Is cli request. method: ' . $this->getRequestMethod());
-        
+        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+            . ' Is cli request. method: ' . $this->getRequestMethod());
+
         $setupServer = new Setup_Frontend_Cli();
-        #$setupServer->authenticate($opts->username, $opts->password);
         return $setupServer->handle($opts);
     }
     
index cf98f40..ebdeff0 100755 (executable)
@@ -220,10 +220,14 @@ class Tinebase_Auth
         }
         
         $this->_backend->setCredential($_password);
-        
-        if (Tinebase_Session::isStarted()) {
-            Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session());
-        } else {
+
+        try {
+            if (Tinebase_Session::isStarted()) {
+                Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session());
+            } else {
+                Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_NonPersistent());
+            }
+        } catch (Zend_Session_Exception $e) {
             Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_NonPersistent());
         }
         $result = Zend_Auth::getInstance()->authenticate($this->_backend);