From dc2add4f1084df625ae375d455360897e15ef148 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 21 May 2019 08:34:52 +1000 Subject: [PATCH] Move api_key read permission checks from api to BAO --- CRM/Contact/BAO/Contact.php | 27 +++++++++++++++++++++++++++ api/v3/Contact.php | 35 +++-------------------------------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index b4b5c6e2c7..1dffb1d805 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -437,9 +437,36 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { 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. * diff --git a/api/v3/Contact.php b/api/v3/Contact.php index e52a46093f..c1174571e9 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -129,8 +129,6 @@ function civicrm_api3_contact_create($params) { _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'); } @@ -182,37 +180,10 @@ function civicrm_api3_contact_get($params) { $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'); } /** -- 2.25.1