From 46b6363ca84aff553c548c77d5a0ea903b5b0a08 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 10 May 2013 17:54:31 -0400 Subject: [PATCH] CRM-12556 - api/v3 - Identify contacts by user name Example: drush civicrm-api contact.get id=@user:demo I read "System_Drupal::loadUser()" to get a starting point for writing "System_Drupal::getUserId()"; however, only Drupal defines loadUser(), so there was no starting point for the other CMSs. This will degrade in a sane way on other CMSs. ---------------------------------------- * CRM-12556: Assign api_key for a user via command-line http://issues.civicrm.org/jira/browse/CRM-12556 --- CRM/Utils/System/Base.php | 11 +++++++++ CRM/Utils/System/Drupal.php | 14 ++++++++++++ CRM/Utils/System/Drupal6.php | 14 ++++++++++++ api/v3/utils.php | 44 ++++++++++++++++++++++++++++++++---- 4 files changed, 78 insertions(+), 5 deletions(-) diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index c522696281..3b17c6377b 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -106,6 +106,17 @@ abstract class CRM_Utils_System_Base { */ public abstract function getLoginURL($destination = ''); + /** + * Determine the native ID of the CMS user + * + * @param $username + * @return int|NULL + */ + function getUfId($username) { + $className = get_class($this); + throw new CRM_Core_Exception("Not implemented: {$className}->getUfId"); + } + /** * Set a init session with user object * diff --git a/CRM/Utils/System/Drupal.php b/CRM/Utils/System/Drupal.php index b9eef0e8b4..e55684acf9 100644 --- a/CRM/Utils/System/Drupal.php +++ b/CRM/Utils/System/Drupal.php @@ -658,6 +658,20 @@ AND u.status = 1 } */ + /** + * Determine the native ID of the CMS user + * + * @param $username + * @return int|NULL + */ + function getUfId($username) { + $user = user_load_by_name($username); + if (empty($user->uid)) { + return NULL; + } + return $user->uid; + } + /** * Set a message in the UF to display to a user * diff --git a/CRM/Utils/System/Drupal6.php b/CRM/Utils/System/Drupal6.php index 5ad551d988..a33318231e 100644 --- a/CRM/Utils/System/Drupal6.php +++ b/CRM/Utils/System/Drupal6.php @@ -589,6 +589,20 @@ SELECT name, mail } */ + /** + * Determine the native ID of the CMS user + * + * @param $username + * @return int|NULL + */ + function getUfId($username) { + $user = user_load(array('name' => $username)); + if (empty($user->uid)) { + return NULL; + } + return $user->uid; + } + /** * Set a message in the UF to display to a user * diff --git a/api/v3/utils.php b/api/v3/utils.php index b29b0713a8..c680e00b8e 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1470,11 +1470,14 @@ function _civicrm_api3_swap_out_aliases(&$apiRequest) { */ function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $entity) { //if fieldname exists in params - if (CRM_Utils_Array::value($fieldName, $params)) { - //if value = 'user_contact_id' replace value with logged in user id - if ($params[$fieldName] == "user_contact_id") { - $session = &CRM_Core_Session::singleton(); - $params[$fieldName] = $session->get('userID'); + if (CRM_Utils_Array::value($fieldname, $params)) { + // if value = 'user_contact_id' (or similar), replace value with contact id + if (!is_integer($params[$fieldname])) { + $realContactId = _civicrm_api3_resolve_contactID($params[$fieldname]); + if (!is_numeric($realContactId)) { + throw new API_Exception("\"$fieldname\" cannot be resolved to a contact ID", 2002, array('error_field' => $fieldname,"type"=>"integer")); + } + $params[$fieldname] = $realContactId; } if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) { _civicrm_api3_api_match_pseudoconstant($params, $entity, $fieldName, $fieldInfo); @@ -1499,6 +1502,37 @@ function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $ent } } +/** + * Determine a contact ID using a string expression + * + * @param string $contactIdExpr e.g. "user_contact_id" or "@user:username" + * @return int|NULL + */ +function _civicrm_api3_resolve_contactID($contactIdExpr) { + //if value = 'user_contact_id' replace value with logged in user id + if ($contactIdExpr == "user_contact_id") { + $session = &CRM_Core_Session::singleton(); + if (!is_numeric($session->get('userID'))) { + return NULL; + } + return $session->get('userID'); + } elseif (preg_match('/^@user:(.*)$/', $contactIdExpr, $matches)) { + $config = CRM_Core_Config::singleton(); + + $ufID = $config->userSystem->getUfId($matches[1]); + if (!$ufID) { + return NULL; + } + + $contactID = CRM_Core_BAO_UFMatch::getContactId($ufID); + if (!$ufID) { + return NULL; + } + + return $contactID; + } +} + function _civicrm_api3_validate_html(&$params, &$fieldName, &$fieldInfo) { if ($value = CRM_Utils_Array::value($fieldName, $params)) { if (!CRM_Utils_Rule::xssString($value)) { -- 2.25.1