From: Eileen McNaughton Date: Thu, 20 Feb 2014 02:52:35 +0000 (+1300) Subject: Drupal 8 preparation - move userFramework ID retrieval to UF classes X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=32998c82101955eaace07f41b395dbd933ad9e72;p=civicrm-core.git Drupal 8 preparation - move userFramework ID retrieval to UF classes --- diff --git a/CRM/Core/BAO/UFMatch.php b/CRM/Core/BAO/UFMatch.php index 18fe4dfdab..88a2a9d884 100644 --- a/CRM/Core/BAO/UFMatch.php +++ b/CRM/Core/BAO/UFMatch.php @@ -78,23 +78,15 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch { CRM_Core_Error::fatal('wow, session is not an object?'); return; } + $userSystemID = $config->userSystem->getBestUFID($user); 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 { @@ -107,7 +99,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 +107,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. @@ -128,15 +120,13 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch { } // return early - if ($user->$key == 0) { + if ($userSystemID == 0) { return; } - if (!isset($uniqId) || !$uniqId) { - $uniqId = $user->$mail; - } + $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; } diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index 2e6e47dca9..160073122a 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -217,5 +217,37 @@ abstract class CRM_Utils_System_Base { function getTimeZoneString() { return NULL; } + /** + * Get User ID from UserFramework system (CMS) + * @param object $user object as described by the User Framework + * @return mixed + * + */ + function getUserIDFromUserObject($user) {} + + /** + * Get currently logged in user uf id. + * + * @return int $userID logged in user uf id. + */ + function getLoggedInUfID() {} + + /** + * return a UFID (user account ID from the UserFramework / CMS system being based on the user object + * passed, defaulting to the logged in user if not passed. Note that ambiguous situation occurs + * in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be resolving the user id before calling + * the function + * + * Note there is already a function getUFId which takes $username as a param - we could add $user + * as a second param to it but it seems messy - just overloading it because the name is taken + * @param string $user + * @return int $ufid - user ID of UF System + */ + function getBestUFID($user = NULL) { + if($user) { + return $this->getUserIDFromUserObject($user); + } + return $this->getLoggedInUfID(); + } } diff --git a/CRM/Utils/System/DrupalBase.php b/CRM/Utils/System/DrupalBase.php index e8b5a82375..3ac0fb9c84 100644 --- a/CRM/Utils/System/DrupalBase.php +++ b/CRM/Utils/System/DrupalBase.php @@ -198,4 +198,13 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base { } } } + + /** + * Get User ID from UserFramework system (Drupal) + * @param object $user object as described by the CMS + * @return mixed + */ + function getUserIDFromUserObject($user) { + return !empty($user->uid) ? $user->uid : NULL; + } } diff --git a/CRM/Utils/System/Joomla.php b/CRM/Utils/System/Joomla.php index 0f635aacb8..18f7420087 100644 --- a/CRM/Utils/System/Joomla.php +++ b/CRM/Utils/System/Joomla.php @@ -648,6 +648,15 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base { return ($user->guest) ? NULL : $user->id; } + /** + * Get User ID from UserFramework system (Joomla) + * @param object $user object as described by the CMS + * @return mixed + */ + function getUserIDFromUserObject($user) { + return !empty($user->id) ? $user->id : NULL; + } + /** * Get a list of all installed modules, including enabled and disabled ones * diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index ac4de75cf8..3748a225ca 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -594,6 +594,15 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { return $ufID; } + /** + * Get User ID from UserFramework system (Joomla) + * @param object $user object as described by the CMS + * @return mixed + */ + function getUserIDFromUserObject($user) { + return !empty($user->ID) ? $user->ID : NULL; + } + /** * Get user login URL for hosting CMS (method declared in each CMS system class) *