Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20159 backport for 4.6 #10411

Merged
merged 1 commit into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions api/v3/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ 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');
}

Expand Down Expand Up @@ -158,6 +160,7 @@ function civicrm_api3_contact_get($params) {
$options = array();
_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');
}

Expand All @@ -175,6 +178,35 @@ function civicrm_api3_contact_getcount($params) {
return (int) $count;
}

/**
* Filter the result.
*
* @param array $result
*
* @return array
* @throws \CRM_Core_Exception
*/
function _civicrm_api3_contact_formatResult($params, $result) {
$apiKeyPerms = array('edit api keys', 'administer CiviCRM');
$allowApiKey = empty($params['check_permissions']) || CRM_Core_Permission::check(array($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']);
}
}
}
}
return $result;
}

/**
* Adjust Metadata for Get action.
*
Expand Down
69 changes: 69 additions & 0 deletions tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,75 @@ public function testCreateNoNameOrganization() {
$this->callAPIFailure('contact', 'create', $params);
}

/**
* Check that permissions on API key are restricted
*/
public function testCreateApiKey() {
$config = CRM_Core_Config::singleton();
$contactId = $this->individualCreate(array(
'first_name' => 'A',
'last_name' => 'B',
));

// Allow edit -- because permissions aren't being checked
$config->userPermissionClass->permissions = array();
$result = $this->callAPISuccess('Contact', 'create', array(
'id' => $contactId,
'api_key' => 'original',
));
$this->assertEquals('original', $result['values'][$contactId]['api_key']);

// Allow edit -- because we have adequate permission
$config->userPermissionClass->permissions = array('access CiviCRM', 'edit all contacts', 'edit api keys');
$result = $this->callAPISuccess('Contact', 'create', array(
'check_permissions' => 1,
'id' => $contactId,
'api_key' => 'abcd1234',
));
$this->assertEquals('abcd1234', $result['values'][$contactId]['api_key']);

// Disallow edit -- because we don't have permission
$config->userPermissionClass->permissions = array('access CiviCRM', 'edit all contacts');
$result = $this->callAPIFailure('Contact', 'create', array(
'check_permissions' => 1,
'id' => $contactId,
'api_key' => 'defg4321',
));
$this->assertRegExp(';Permission denied to modify api key;', $result['error_message']);

// Return everything -- because permissions are not being checked
$config->userPermissionClass->permissions = array();
$result = $this->callAPISuccess('Contact', 'create', array(
'id' => $contactId,
'first_name' => 'A2',
));
$this->assertEquals('A2', $result['values'][$contactId]['first_name']);
$this->assertEquals('B', $result['values'][$contactId]['last_name']);
$this->assertEquals('abcd1234', $result['values'][$contactId]['api_key']);

// Return everything -- because we have adequate permission
$config->userPermissionClass->permissions = array('access CiviCRM', 'edit all contacts', 'edit api keys');
$result = $this->callAPISuccess('Contact', 'create', array(
'check_permissions' => 1,
'id' => $contactId,
'first_name' => 'A3',
));
$this->assertEquals('A3', $result['values'][$contactId]['first_name']);
$this->assertEquals('B', $result['values'][$contactId]['last_name']);
$this->assertEquals('abcd1234', $result['values'][$contactId]['api_key']);

// Restricted return -- because we don't have permission
$config->userPermissionClass->permissions = array('access CiviCRM', 'edit all contacts');
$result = $this->callAPISuccess('Contact', 'create', array(
'check_permissions' => 1,
'id' => $contactId,
'first_name' => 'A4',
));
$this->assertEquals('A4', $result['values'][$contactId]['first_name']);
$this->assertEquals('B', $result['values'][$contactId]['last_name']);
$this->assertTrue(empty($result['values'][$contactId]['api_key']));
}

/**
* Check with complete array + custom field.
*
Expand Down