From bec3fc7c6f2f33ef33854c8198dc196c35f1f175 Mon Sep 17 00:00:00 2001
From: Brian Shaughnessy <brian@lcdservices.biz>
Date: Wed, 13 Mar 2013 13:23:47 -0400
Subject: [PATCH] CRM-11203 authenticate user when running jobs via cli

---
 CRM/Utils/System.php        | 10 +++++-----
 CRM/Utils/System/Drupal.php | 14 ++++++++++++++
 CRM/Utils/System/Joomla.php | 24 +++++++++++++++++++++---
 bin/cli.class.php           |  4 ++++
 4 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/CRM/Utils/System.php b/CRM/Utils/System.php
index 05c5cbff11..ed43fc9c43 100644
--- a/CRM/Utils/System.php
+++ b/CRM/Utils/System.php
@@ -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",
diff --git a/CRM/Utils/System/Drupal.php b/CRM/Utils/System/Drupal.php
index 44adea35fb..2afae9083b 100644
--- a/CRM/Utils/System/Drupal.php
+++ b/CRM/Utils/System/Drupal.php
@@ -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);
+  }
 }
diff --git a/CRM/Utils/System/Joomla.php b/CRM/Utils/System/Joomla.php
index 673c621129..8179097303 100644
--- a/CRM/Utils/System/Joomla.php
+++ b/CRM/Utils/System/Joomla.php
@@ -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');
diff --git a/bin/cli.class.php b/bin/cli.class.php
index 0b4f0437f9..692d7e7dde 100644
--- a/bin/cli.class.php
+++ b/bin/cli.class.php
@@ -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;
-- 
GitLab