CRM-12556 - api/v3 - Identify contacts by user name
authorTim Otten <totten@civicrm.org>
Fri, 10 May 2013 21:54:31 +0000 (17:54 -0400)
committerTim Otten <totten@civicrm.org>
Sat, 20 Jul 2013 17:45:10 +0000 (10:45 -0700)
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
CRM/Utils/System/Drupal.php
CRM/Utils/System/Drupal6.php
api/v3/utils.php

index c522696281bdd0c26f35e4ba6ac20cbfa9905385..3b17c6377bd6b9eb34450833c239bbd08abff643 100644 (file)
@@ -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
    *
index b9eef0e8b450e545ee2bcdb3cd651a78caa9f426..e55684acf92d9a2dc826fb39dceba34fd50cd355 100644 (file)
@@ -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
    *
index 5ad551d98819c37652f37d507dc7db6c0ae5def2..a33318231e229ae938b6eeca242a7f5845d306b0 100644 (file)
@@ -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
    *
index b29b0713a816aeafdccd8287d4a20d9e030ad13e..c680e00b8e4e208ef25674c7de450a92234201df 100644 (file)
@@ -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)) {