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-20441 Fix issue where multiple activity Ids were not supported wh… #10212

Merged
merged 1 commit into from
Apr 22, 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
22 changes: 17 additions & 5 deletions api/v3/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,23 @@ function civicrm_api3_activity_get($params) {
"Cannot access activities. Required permission: 'view all activities''"
);
}

if (!CRM_Activity_BAO_Activity::checkPermission($params['id'], CRM_Core_Action::VIEW)) {
throw new \Civi\API\Exception\UnauthorizedException(
'You do not have permission to view this activity'
);
$ids = array();
if (is_array($params['id'])) {
foreach ($params['id'] as $operator => $values) {
if (in_array($operator, CRM_Core_DAO::acceptedSQLOperators())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could result in permission escalation if the operator is not "IN". E.g. passing 'id' => array('NOT IN' => 123) would let you see all activities if you have access to view 123.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw should we limit it to just IN for the moment or include Like as well?

$ids = $values;
}
}
}
else {
$ids = array($params['id']);
}
foreach ($ids as $id) {
if (!CRM_Activity_BAO_Activity::checkPermission($id, CRM_Core_Action::VIEW)) {
throw new \Civi\API\Exception\UnauthorizedException(
'You do not have permission to view this activity'
);
}
}
}

Expand Down
59 changes: 59 additions & 0 deletions tests/phpunit/api/v3/ACLPermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class api_v3_ACLPermissionTest extends CiviUnitTestCase {
public $DBResetRequired = FALSE;
protected $_entity;
protected $allowedContactId = 0;
protected $allowedContacts = array();

public function setUp() {
parent::setUp();
Expand Down Expand Up @@ -468,6 +469,19 @@ public function aclWhereOnlyOne($type, &$tables, &$whereTables, &$contactID, &$w
$where = " contact_a.id = " . $this->allowedContactId;
}

/**
* Only specified contact returned.
* @implements CRM_Utils_Hook::aclWhereClause
* @param $type
* @param $tables
* @param $whereTables
* @param $contactID
* @param $where
*/
public function aclWhereMultipleContacts($type, &$tables, &$whereTables, &$contactID, &$where) {
$where = " contact_a.id IN (" . implode(', ', $this->allowedContacts) . ")";
}

/**
* Basic check that an unpermissioned call keeps working and permissioned call fails.
*/
Expand Down Expand Up @@ -559,6 +573,51 @@ public function testGetActivityACLSourceContactDeleted() {
$this->callAPISuccess('Activity', 'getsingle', array('check_permissions' => 1, 'id' => $activity['id']));
}

/**
* Test get activities multiple ids with check permissions
* CRM-20441
*/
public function testActivitiesGetMultipleIdsCheckPermissions() {
$this->createLoggedInUser();
$activity = $this->activityCreate();
$activity2 = $this->activityCreate();
$this->setPermissions(array('access CiviCRM'));
$this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults'));
// Get activities associated with contact $this->_contactID.
$params = array(
'id' => array('IN' => array($activity['id'], $activity2['id'])),
'check_permissions' => TRUE,
);
$result = $this->callAPISuccess('activity', 'get', $params);
$this->assertEquals(2, $result['count']);
}

/**
* Test get activities multiple ids with check permissions
* Limit access to One contact
* CRM-20441
*/
public function testActivitiesGetMultipleIdsCheckPermissionsLimitedACL() {
$this->createLoggedInUser();
$activity = $this->activityCreate();
$contacts = $this->getActivityContacts($activity);
$this->setPermissions(array('access CiviCRM'));
foreach ($contacts as $contact_id) {
$this->allowedContacts[] = $contact_id;
}
$this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereMultipleContacts'));
$contact2 = $this->individualCreate();
$activity2 = $this->activityCreate(array('source_contact_id' => $contact2));
// Get activities associated with contact $this->_contactID.
$params = array(
'id' => array('IN' => array($activity['id'])),
'check_permissions' => TRUE,
);
$result = $this->callAPISuccess('activity', 'get', $params);
$this->assertEquals(1, $result['count']);
$this->callAPIFailure('activity', 'get', array_merge($params, array('id' => array('IN', array($activity2['id'])))));
}

/**
* Get the contacts for the activity.
*
Expand Down