From 2b617cb0bfa6119d8041b9c80d6db8f3b1d841b2 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 20 Feb 2014 16:43:26 +1300 Subject: [PATCH] Drupal 8 prep - refactor retrieval of uniqID in synch class --- CRM/Core/BAO/UFMatch.php | 19 +++-------------- CRM/Utils/System/Base.php | 34 ++++++++++++++++++++++++++++++- CRM/Utils/System/DrupalBase.php | 20 ++++++++++++++++++ CRM/Utils/System/Joomla.php | 19 +++++++++++++++++ CRM/Utils/System/WordPress.php | 36 ++++++++++++++++++++++++++------- 5 files changed, 104 insertions(+), 24 deletions(-) diff --git a/CRM/Core/BAO/UFMatch.php b/CRM/Core/BAO/UFMatch.php index 88a2a9d884..dadd0ce70f 100644 --- a/CRM/Core/BAO/UFMatch.php +++ b/CRM/Core/BAO/UFMatch.php @@ -72,26 +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; } - $userSystemID = $config->userSystem->getBestUFID($user); - if ($config->userSystem->is_drupal) { - $mail = 'mail'; - } - elseif ($uf == 'Joomla') { - $mail = 'email'; - } - elseif ($uf == 'WordPress') { - $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 @@ -124,8 +113,6 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch { return; } - $uniqId = $user->$mail; - $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 160073122a..9cccc92be5 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -217,6 +217,15 @@ abstract class CRM_Utils_System_Base { function getTimeZoneString() { return NULL; } + + /** + * Get Unique Identifier from UserFramework system (CMS) + * @param object $user object as described by the User Framework + * @return mixed $uniqueIdentifer Unique identifier from the user Framework system + * + */ + function getUniqueIdentifierFromUserObject($user) {} + /** * Get User ID from UserFramework system (CMS) * @param object $user object as described by the User Framework @@ -232,6 +241,13 @@ abstract class CRM_Utils_System_Base { */ function getLoggedInUfID() {} + /** + * Get currently logged in user unique identifier - this tends to be the email address or user name. + * + * @return string $userID logged in user unique identifier + */ + function getLoggedInUniqueIdentifier() {} + /** * 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 @@ -240,7 +256,7 @@ abstract class CRM_Utils_System_Base { * * 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 + * @param object $user * @return int $ufid - user ID of UF System */ function getBestUFID($user = NULL) { @@ -249,5 +265,21 @@ abstract class CRM_Utils_System_Base { } return $this->getLoggedInUfID(); } + + /** + * return a unique identifier (usually an email address or username) 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 unique identifier before calling + * the function + * + * @param object $user + * @return string $uniqueIdentifier - unique identifier from the UF System + */ + function getBestUFUniqueIdentifier($user = NULL) { + if($user) { + return $this->getUniqueIdentifierFromUserObject($user); + } + return $this->getLoggedInUniqueIdentifier(); + } } diff --git a/CRM/Utils/System/DrupalBase.php b/CRM/Utils/System/DrupalBase.php index 3ac0fb9c84..169d6f4cb4 100644 --- a/CRM/Utils/System/DrupalBase.php +++ b/CRM/Utils/System/DrupalBase.php @@ -207,4 +207,24 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base { function getUserIDFromUserObject($user) { return !empty($user->uid) ? $user->uid : NULL; } + + /** + * Get Unique Identifier from UserFramework system (CMS) + * @param object $user object as described by the User Framework + * @return mixed $uniqueIdentifer Unique identifier from the user Framework system + * + */ + function getUniqueIdentifierFromUserObject($user) { + return empty($user->mail) ? NULL : $user->mail; + } + + /** + * Get currently logged in user unique identifier - this tends to be the email address or user name. + * + * @return string $userID logged in user unique identifier + */ + function getLoggedInUniqueIdentifier() { + global $user; + return $this->getUniqueIdentifierFromUserObject($user); + } } diff --git a/CRM/Utils/System/Joomla.php b/CRM/Utils/System/Joomla.php index 18f7420087..b8cb325574 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 currently logged in user unique identifier - this tends to be the email address or user name. + * + * @return string $userID logged in user unique identifier + */ + function getLoggedInUniqueIdentifier() { + $user = JFactory::getUser(); + return $this->getUniqueIdentifierFromUserObject($user); + } /** * Get User ID from UserFramework system (Joomla) * @param object $user object as described by the CMS @@ -657,6 +666,16 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base { return !empty($user->id) ? $user->id : NULL; } + /** + * Get Unique Identifier from UserFramework system (CMS) + * @param object $user object as described by the User Framework + * @return mixed $uniqueIdentifer Unique identifier from the user Framework system + * + */ + function getUniqueIdentifierFromUserObject($user) { + return ($user->guest) ? NULL : $user->email; + } + /** * 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 3748a225ca..9f5c8a4a42 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -578,6 +578,13 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { return $isloggedIn; } + function getLoggedInUserObject() { + if (function_exists('is_user_logged_in') && + is_user_logged_in()) { + global $current_user; + } + return $current_user; + } /** * Get currently logged in user uf id. * @@ -585,13 +592,18 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { */ public function getLoggedInUfID() { $ufID = NULL; - if (function_exists('is_user_logged_in') && - is_user_logged_in() - ) { - global $current_user; - $ufID = $current_user->ID; - } - return $ufID; + $current_user = $this->getLoggedInUserObject(); + return isset($current_user->ID) ? $current_user->ID : NULL; + } + + /** + * Get currently logged in user unique identifier - this tends to be the email address or user name. + * + * @return string $userID logged in user unique identifier + */ + function getLoggedInUniqueIdentifier() { + $user = $this->getLoggedInUserObject(); + return $this->getUniqueIdentifierFromUserObject($user); } /** @@ -603,6 +615,16 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { return !empty($user->ID) ? $user->ID : NULL; } + /** + * Get Unique Identifier from UserFramework system (CMS) + * @param object $user object as described by the User Framework + * @return mixed $uniqueIdentifer Unique identifier from the user Framework system + * + */ + function getUniqueIdentifierFromUserObject($user) { + return empty($user->user_email) ? NULL : $user->user_email; + } + /** * Get user login URL for hosting CMS (method declared in each CMS system class) * -- 2.25.1