CRM-11203 authenticate user when running jobs via cli
authorBrian Shaughnessy <brian@lcdservices.biz>
Wed, 13 Mar 2013 17:23:47 +0000 (13:23 -0400)
committerBrian Shaughnessy <brian@lcdservices.biz>
Wed, 13 Mar 2013 17:23:47 +0000 (13:23 -0400)
CRM/Utils/System.php
CRM/Utils/System/Drupal.php
CRM/Utils/System/Joomla.php
bin/cli.class.php

index 05c5cbff1183a40e17780d2965da222c7580bb6d..ed43fc9c43c1b895cd073b6b961060611cb2c382 100644 (file)
@@ -524,7 +524,7 @@ class CRM_Utils_System {
     return TRUE;
   }
 
-  static function authenticateScript($abort = TRUE, $name = NULL, $pass = NULL, $storeInSession = TRUE, $loadCMSBootstrap = TRUE) {
+  static function authenticateScript($abort = TRUE, $name = NULL, $pass = NULL, $storeInSession = TRUE, $loadCMSBootstrap = TRUE, $requireKey = TRUE) {
     // auth to make sure the user has a login/password to do a shell
     // operation
     // later on we'll link this to acl's
@@ -540,7 +540,7 @@ class CRM_Utils_System {
       );
     }
 
-    if (!self::authenticateKey($abort)) {
+    if ($requireKey && !self::authenticateKey($abort)) {
       return FALSE;
     }
 
@@ -554,9 +554,9 @@ class CRM_Utils_System {
       // lets store contact id and user id in session
       list($userID, $ufID, $randomNumber) = $result;
       if ($userID && $ufID) {
-        $session = CRM_Core_Session::singleton();
-        $session->set('ufID', $ufID);
-        $session->set('userID', $userID);
+
+        $config = CRM_Core_Config::singleton();
+        $config->userSystem->setUserSession( array($userID, $ufID) );
       }
       else {
         return self::authenticateAbort("ERROR: Unexpected error, could not match userID and contactID",
index 44adea35fb2402d74896f0234c78036401b6a17e..2afae9083b112b5af17db72f3fdfe6118278ac2d 100644 (file)
@@ -1024,4 +1024,18 @@ AND    u.status = 1
       og_ungroup($ogID, 'user', user_load($drupalID));
     }
   }
+
+  /**
+   * Set a init session with user object
+   *
+   * @param array $data  array with user specific data
+   *
+   * @access public
+   */
+  function setUserSession($data) {
+    list($userID, $ufID) = $data;
+    $session = CRM_Core_Session::singleton();
+    $session->set('ufID', $ufID);
+    $session->set('userID', $userID);
+  }
 }
index 673c62112976cf5fdc87565c0083b5462a28f3b4..81790973034e546574cf9b05c5393a84a2bfec76 100644 (file)
@@ -475,7 +475,7 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
           'pass' => $password,
         );
       }
-      CRM_Utils_System::loadBootStrap($bootStrapParams);
+      CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
     }
 
     jimport('joomla.application.component.helper');
@@ -526,6 +526,24 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
     return FALSE;
   }
 
+  /**
+   * Set a init session with user object
+   *
+   * @param array $data  array with user specific data
+   *
+   * @access public
+   */
+  function setUserSession($data) {
+    list($userID, $ufID) = $data;
+    $user = new JUser( $ufID );
+    $session = &JFactory::getSession();
+    $session->set('user', $user);
+
+    $cSession = CRM_Core_Session::singleton();
+    $cSession->set('ufID', $ufID);
+    $cSession->set('userID', $userID);
+  }
+
   /**
    * Set a message in the UF to display to a user
    *
@@ -581,13 +599,13 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
    * @param $loadUser boolean load cms user?
    * @param $throwError throw error on failure?
    */
-  function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE) {
+  function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $loadDefines = TRUE) {
     // Setup the base path related constant.
     $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
 
     // load BootStrap here if needed
     // We are a valid Joomla entry point.
-    if ( ! defined( '_JEXEC' ) ) {
+    if ( ! defined( '_JEXEC' ) && $loadDefines ) {
       define('_JEXEC', 1);
       define('DS', DIRECTORY_SEPARATOR);
       define('JPATH_BASE', $joomlaBase . '/administrator');
index 0b4f0437f9039c329526c1f4c6ccde8ee360c6ac..692d7e7dde799423aac49f5166fa67c975c6f31e 100644 (file)
@@ -218,6 +218,10 @@ class civicrm_cli {
     }
 
     if (!empty($this->_user)) {
+      if(!CRM_Utils_System::authenticateScript(TRUE, $this->_user, $this->_password, TRUE, FALSE, FALSE)) {
+        $this->_log(ts("Failed to login as %1. Wrong username or password.", array('1' => $this->_user)));
+        return FALSE;
+      }
       if (!$cms->loadUser($this->_user)) {
         $this->_log(ts("Failed to login as %1", array('1' => $this->_user)));
         return FALSE;