From 9fa1f9b964f4e63a29ad5575041c8c1196819c35 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 27 Jun 2019 18:57:08 -0400 Subject: [PATCH] Move api_key write permission checks from api to BAO --- CRM/Contact/BAO/Contact.php | 13 ++++++++++--- api/v3/Contact.php | 12 ------------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 1dffb1d805..a6f660669d 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -267,18 +267,25 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { return $contact; } - if (!empty($params['contact_id']) && empty($params['contact_type'])) { + $isEdit = !empty($params['contact_id']); + + if ($isEdit && empty($params['contact_type'])) { $params['contact_type'] = self::getContactType($params['contact_id']); } - $isEdit = TRUE; + if (!empty($params['check_permissions']) && isset($params['api_key']) + && !CRM_Core_Permission::check([['edit api keys', 'administer CiviCRM']]) + && !($isEdit && CRM_Core_Permission::check('edit own api keys') && $params['contact_id'] == CRM_Core_Session::getLoggedInContactID()) + ) { + throw new \Civi\API\Exception\UnauthorizedException('Permission denied to modify api key'); + } + if ($invokeHooks) { if (!empty($params['contact_id'])) { CRM_Utils_Hook::pre('edit', $params['contact_type'], $params['contact_id'], $params); } else { CRM_Utils_Hook::pre('create', $params['contact_type'], NULL, $params); - $isEdit = FALSE; } } diff --git a/api/v3/Contact.php b/api/v3/Contact.php index c1174571e9..d5f6fc3613 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -67,18 +67,6 @@ function civicrm_api3_contact_create($params) { return $values; } - if (array_key_exists('api_key', $params) && !empty($params['check_permissions'])) { - if (CRM_Core_Permission::check('edit api keys') || CRM_Core_Permission::check('administer CiviCRM')) { - // OK - } - elseif ($contactID && CRM_Core_Permission::check('edit own api keys') && CRM_Core_Session::singleton()->get('userID') == $contactID) { - // OK - } - else { - throw new \Civi\API\Exception\UnauthorizedException('Permission denied to modify api key'); - } - } - if (!$contactID) { // If we get here, we're ready to create a new contact if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { -- 2.25.1