self::processGreetings($contact);
}
+ if (!empty($params['check_permissions'])) {
+ $contacts = [&$contact];
+ self::unsetProtectedFields($contacts);
+ }
+
return $contact;
}
+ /**
+ * Format the output of the create contact function
+ * @param CRM_Contact_DAO_Contact[]|array[] $contacts
+ */
+ public static function unsetProtectedFields(&$contacts) {
+ if (!CRM_Core_Permission::check([['edit api keys', 'administer CiviCRM']])) {
+ $currentUser = CRM_Core_Session::getLoggedInContactID();
+ $editOwn = $currentUser && CRM_Core_Permission::check('edit own api keys');
+ foreach ($contacts as &$contact) {
+ $cid = is_object($contact) ? $contact->id : CRM_Utils_Array::value('id', $contact);
+ if (!($editOwn && $cid == $currentUser)) {
+ if (is_object($contact)) {
+ unset($contact->api_key);
+ }
+ else {
+ unset($contact['api_key']);
+ }
+ }
+ }
+ }
+ }
+
/**
* Ensure greeting parameters are set.
*
_civicrm_api3_object_to_array_unique_fields($contact, $values[$contact->id]);
}
- $values = _civicrm_api3_contact_formatResult($params, $values);
-
return civicrm_api3_create_success($values, $params, 'Contact', 'create');
}
$options = [];
_civicrm_api3_contact_get_supportanomalies($params, $options);
$contacts = _civicrm_api3_get_using_query_object('Contact', $params, $options);
- $contacts = _civicrm_api3_contact_formatResult($params, $contacts);
- return civicrm_api3_create_success($contacts, $params, 'Contact');
-}
-
-/**
- * Filter the result.
- *
- * @param array $result
- *
- * @return array
- * @throws \CRM_Core_Exception
- */
-function _civicrm_api3_contact_formatResult($params, $result) {
- $apiKeyPerms = ['edit api keys', 'administer CiviCRM'];
- $allowApiKey = empty($params['check_permissions']) || CRM_Core_Permission::check([$apiKeyPerms]);
- if (!$allowApiKey) {
- if (is_array($result)) {
- // Single-value $result
- if (isset($result['api_key'])) {
- unset($result['api_key']);
- }
-
- // Multi-value $result
- foreach ($result as $key => $row) {
- if (is_array($row)) {
- unset($result[$key]['api_key']);
- }
- }
- }
+ if (!empty($params['check_permissions'])) {
+ CRM_Contact_BAO_Contact::unsetProtectedFields($contacts);
}
- return $result;
+ return civicrm_api3_create_success($contacts, $params, 'Contact');
}
/**