Merge pull request #2762 from amitajgaonkar/WebtestIssues
[civicrm-core.git] / CRM / Core / BAO / UFMatch.php
index 2b3433de9a755739010d8a505c929a88cad0c035..7a8e8fa096ef6146bbfee5db0b079f8cc0d406e9 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -72,34 +72,15 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
    * @static
    */
   static function synchronize(&$user, $update, $uf, $ctype, $isLogin = FALSE) {
-    $config = CRM_Core_Config::singleton();
+    $userSystem = CRM_Core_Config::singleton()->userSystem;
     $session = CRM_Core_Session::singleton();
     if (!is_object($session)) {
       CRM_Core_Error::fatal('wow, session is not an object?');
       return;
     }
 
-    if ($config->userSystem->is_drupal) {
-      $key   = 'uid';
-      $login = 'name';
-      $mail  = 'mail';
-    }
-    elseif ($uf == 'Joomla') {
-      $key   = 'id';
-      $login = 'username';
-      $mail  = 'email';
-      if (!isset($user->id) || !isset($user->email)) {
-        $user = JFactory::getUser();
-      }
-    }
-    elseif ($uf == 'WordPress') {
-      $key   = 'ID';
-      $login = 'user_login';
-      $mail  = 'user_email';
-    }
-    else {
-      CRM_Core_Error::statusBounce(ts('Please set the user framework variable'));
-    }
+    $userSystemID = $userSystem->getBestUFID($user);
+    $uniqId = $userSystem->getBestUFUniqueIdentifier($user);
 
     // if the id of the object is zero (true for anon users in drupal)
     // have we already processed this user, if so early
@@ -107,7 +88,7 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
     $userID = $session->get('userID');
     $ufID = $session->get('ufID');
 
-    if (!$update && $ufID == $user->$key) {
+    if (!$update && $ufID == $userSystemID) {
       return;
     }
 
@@ -115,7 +96,7 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
     $isUserLoggedIn = CRM_Utils_System::isUserLoggedIn();
 
     // reset the session if we are a different user
-    if ($ufID && $ufID != $user->$key) {
+    if ($ufID && $ufID != $userSystemID) {
       $session->reset();
 
       //get logged in user ids, and set to session.
@@ -124,22 +105,15 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
         $session->set('ufID', CRM_Utils_Array::value('uf_id', $userIds, ''));
         $session->set('userID', CRM_Utils_Array::value('contact_id', $userIds, ''));
         $session->set('ufUniqID', CRM_Utils_Array::value('uf_name', $userIds, ''));
-        if (CRM_Utils_Array::value('contact_id', $userIds, FALSE)) {
-          $session->set('authSrc', CRM_Core_Permission::AUTH_SRC_LOGIN);
-        }
       }
     }
 
     // return early
-    if ($user->$key == 0) {
+    if ($userSystemID == 0) {
       return;
     }
 
-    if (!isset($uniqId) || !$uniqId) {
-      $uniqId = $user->$mail;
-    }
-
-    $ufmatch = self::synchronizeUFMatch($user, $user->$key, $uniqId, $uf, NULL, $ctype, $isLogin);
+    $ufmatch = self::synchronizeUFMatch($user, $userSystemID, $uniqId, $uf, NULL, $ctype, $isLogin);
     if (!$ufmatch) {
       return;
     }
@@ -163,9 +137,6 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
     $session->set('ufID', $ufID);
     $session->set('userID', $userID);
     $session->set('ufUniqID', $ufUniqID);
-    if ($userID) {
-      $session->set('authSrc', CRM_Core_Permission::AUTH_SRC_LOGIN);
-    }
 
     // add current contact to recently viewed
     if ($ufmatch->contact_id) {