Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | ||
30 | /** | |
31 | * This class is intended to test ACL permission using the multisite module | |
32 | * | |
7884d958 | 33 | * @package CiviCRM_APIv3 |
34 | * @subpackage API_Contact | |
6a488035 | 35 | */ |
6a488035 | 36 | class api_v3_ACLPermissionTest extends CiviUnitTestCase { |
4e420887 | 37 | protected $_apiversion = 3; |
4e420887 | 38 | public $DBResetRequired = FALSE; |
430ae6dd | 39 | protected $_entity; |
c16ed19b | 40 | protected $allowedContactId = 0; |
430ae6dd | 41 | |
00be9182 | 42 | public function setUp() { |
6a488035 TO |
43 | parent::setUp(); |
44 | $baoObj = new CRM_Core_DAO(); | |
45 | $baoObj->createTestObject('CRM_Pledge_BAO_Pledge', array(), 1, 0); | |
46 | $baoObj->createTestObject('CRM_Core_BAO_Phone', array(), 1, 0); | |
6a488035 TO |
47 | $config = CRM_Core_Config::singleton(); |
48 | $config->userPermissionClass->permissions = array(); | |
49 | } | |
7884d958 | 50 | |
51 | /** | |
52 | * (non-PHPdoc) | |
53 | * @see CiviUnitTestCase::tearDown() | |
54 | */ | |
00be9182 | 55 | public function tearDown() { |
e182b859 | 56 | CRM_Utils_Hook::singleton()->reset(); |
6a488035 | 57 | $tablesToTruncate = array( |
7884d958 | 58 | 'civicrm_contact', |
ae4bb4c9 EM |
59 | 'civicrm_group_contact', |
60 | 'civicrm_group', | |
61 | 'civicrm_acl', | |
62 | 'civicrm_acl_cache', | |
63 | 'civicrm_acl_entity_role', | |
64 | 'civicrm_acl_contact_cache', | |
65 | 'civicrm_contribution', | |
66 | 'civicrm_participant', | |
225d474b | 67 | 'civicrm_uf_match', |
6a488035 TO |
68 | ); |
69 | $this->quickCleanup($tablesToTruncate); | |
70 | $config = CRM_Core_Config::singleton(); | |
71 | unset($config->userPermissionClass->permissions); | |
72 | } | |
7884d958 | 73 | |
74 | /** | |
eceb18cc | 75 | * Function tests that an empty where hook returns no results. |
7884d958 | 76 | */ |
00be9182 | 77 | public function testContactGetNoResultsHook() { |
6a488035 | 78 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults')); |
4e420887 | 79 | $result = $this->callAPISuccess('contact', 'get', array( |
6a488035 TO |
80 | 'check_permissions' => 1, |
81 | 'return' => 'display_name', | |
82 | )); | |
6a488035 TO |
83 | $this->assertEquals(0, $result['count']); |
84 | } | |
85 | ||
1028f75e | 86 | /** |
1a4651ba | 87 | * Function tests that an empty where hook returns exactly 1 result with "view my contact". |
1028f75e | 88 | * |
89 | * CRM-16512 caused contacts with Edit my contact to be able to view all records. | |
90 | */ | |
1a4651ba | 91 | public function testContactGetOneResultHookWithViewMyContact() { |
1028f75e | 92 | $this->createLoggedInUser(); |
93 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults')); | |
94 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM', 'view my contact'); | |
95 | $result = $this->callAPISuccess('contact', 'get', array( | |
96 | 'check_permissions' => 1, | |
97 | 'return' => 'display_name', | |
98 | )); | |
1a4651ba CW |
99 | $this->assertEquals(1, $result['count']); |
100 | } | |
101 | ||
102 | /** | |
103 | * Function tests that a user with "edit my contact" can edit themselves. | |
104 | */ | |
105 | public function testContactEditHookWithEditMyContact() { | |
1a4651ba CW |
106 | $cid = $this->createLoggedInUser(); |
107 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults')); | |
108 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM', 'edit my contact'); | |
109 | $this->callAPISuccess('contact', 'create', array( | |
110 | 'check_permissions' => 1, | |
111 | 'id' => $cid, | |
112 | )); | |
1028f75e | 113 | } |
114 | ||
c16ed19b CW |
115 | /** |
116 | * Ensure contact permissions extend to related entities like email | |
117 | */ | |
118 | public function testRelatedEntityPermissions() { | |
0a61b6e2 | 119 | $this->createLoggedInUser(); |
c16ed19b CW |
120 | $disallowedContact = $this->individualCreate(array(), 0); |
121 | $this->allowedContactId = $this->individualCreate(array(), 1); | |
122 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereOnlyOne')); | |
123 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); | |
124 | $testEntities = array( | |
0a61b6e2 CW |
125 | 'Email' => array('email' => 'null@nothing', 'location_type_id' => 1), |
126 | 'Phone' => array('phone' => '123456', 'location_type_id' => 1), | |
127 | 'IM' => array('name' => 'hello', 'location_type_id' => 1), | |
c16ed19b | 128 | 'Website' => array('url' => 'http://test'), |
0a61b6e2 | 129 | 'Address' => array('street_address' => '123 Sesame St.', 'location_type_id' => 1), |
c16ed19b CW |
130 | ); |
131 | foreach ($testEntities as $entity => $params) { | |
132 | $params += array( | |
133 | 'contact_id' => $disallowedContact, | |
134 | 'check_permissions' => 1, | |
135 | ); | |
136 | // We should be prevented from getting or creating entities for a contact we don't have permission for | |
137 | $this->callAPIFailure($entity, 'create', $params); | |
138 | $results = $this->callAPISuccess($entity, 'get', array('contact_id' => $disallowedContact, 'check_permissions' => 1)); | |
139 | $this->assertEquals(0, $results['count']); | |
140 | ||
141 | // We should be allowed to create and get for contacts we do have permission on | |
142 | $params['contact_id'] = $this->allowedContactId; | |
143 | $this->callAPISuccess($entity, 'create', $params); | |
144 | $results = $this->callAPISuccess($entity, 'get', array('contact_id' => $this->allowedContactId, 'check_permissions' => 1)); | |
145 | $this->assertGreaterThan(0, $results['count']); | |
146 | } | |
147 | } | |
148 | ||
6a488035 | 149 | /** |
eceb18cc | 150 | * Function tests all results are returned. |
7884d958 | 151 | */ |
00be9182 | 152 | public function testContactGetAllResultsHook() { |
6a488035 | 153 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults')); |
4e420887 | 154 | $result = $this->callAPISuccess('contact', 'get', array( |
7884d958 | 155 | 'check_permissions' => 1, |
156 | 'return' => 'display_name', | |
6a488035 TO |
157 | )); |
158 | ||
6a488035 TO |
159 | $this->assertEquals(2, $result['count']); |
160 | } | |
7884d958 | 161 | |
6a488035 | 162 | /** |
eceb18cc | 163 | * Function tests that deleted contacts are not returned. |
7884d958 | 164 | */ |
00be9182 | 165 | public function testContactGetPermissionHookNoDeleted() { |
f5052d4d | 166 | $this->callAPISuccess('contact', 'create', array('id' => 2, 'is_deleted' => 1)); |
6a488035 | 167 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults')); |
4e420887 | 168 | $result = $this->callAPISuccess('contact', 'get', array( |
7884d958 | 169 | 'check_permissions' => 1, |
170 | 'return' => 'display_name', | |
6a488035 | 171 | )); |
6a488035 TO |
172 | $this->assertEquals(1, $result['count']); |
173 | } | |
174 | ||
175 | /** | |
eceb18cc | 176 | * Test permissions limited by hook. |
6a488035 | 177 | */ |
00be9182 | 178 | public function testContactGetHookLimitingHook() { |
6a488035 TO |
179 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereOnlySecond')); |
180 | ||
4e420887 | 181 | $result = $this->callAPISuccess('contact', 'get', array( |
6a488035 TO |
182 | 'check_permissions' => 1, |
183 | 'return' => 'display_name', | |
7884d958 | 184 | )); |
6a488035 TO |
185 | $this->assertEquals(1, $result['count']); |
186 | } | |
187 | ||
7884d958 | 188 | /** |
1028f75e | 189 | * Confirm that without check permissions we still get 2 contacts returned. |
7884d958 | 190 | */ |
00be9182 | 191 | public function testContactGetHookLimitingHookDontCheck() { |
4e420887 | 192 | $result = $this->callAPISuccess('contact', 'get', array( |
193 | 'check_permissions' => 0, | |
194 | 'return' => 'display_name', | |
6a488035 | 195 | )); |
6a488035 TO |
196 | $this->assertEquals(2, $result['count']); |
197 | } | |
7884d958 | 198 | |
6a488035 | 199 | /** |
eceb18cc | 200 | * Check that id works as a filter. |
6a488035 | 201 | */ |
00be9182 | 202 | public function testContactGetIDFilter() { |
6a488035 | 203 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults')); |
4e420887 | 204 | $result = $this->callAPISuccess('contact', 'get', array( |
6a488035 TO |
205 | 'sequential' => 1, |
206 | 'id' => 2, | |
207 | 'check_permissions' => 1, | |
208 | )); | |
209 | ||
6a488035 TO |
210 | $this->assertEquals(1, $result['count']); |
211 | $this->assertEquals(2, $result['id']); | |
212 | } | |
213 | ||
7884d958 | 214 | /** |
eceb18cc | 215 | * Check that address IS returned. |
7884d958 | 216 | */ |
00be9182 | 217 | public function testContactGetAddressReturned() { |
7884d958 | 218 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereOnlySecond')); |
219 | $fullresult = $this->callAPISuccess('contact', 'get', array( | |
220 | 'sequential' => 1, | |
221 | )); | |
222 | //return doesn't work for all keys - can't fix that here so let's skip ... | |
223 | //prefix & suffix are inconsistent due to CRM-7929 | |
224 | // unsure about others but return doesn't work on them | |
225 | $elementsReturnDoesntSupport = array( | |
7884d958 | 226 | 'prefix', |
7884d958 | 227 | 'suffix', |
7884d958 | 228 | 'gender', |
229 | 'current_employer', | |
230 | 'phone_id', | |
231 | 'phone_type_id', | |
232 | 'phone', | |
233 | 'worldregion_id', | |
21dfd5f5 | 234 | 'world_region', |
7884d958 | 235 | ); |
236 | $expectedReturnElements = array_diff(array_keys($fullresult['values'][0]), $elementsReturnDoesntSupport); | |
237 | $result = $this->callAPISuccess('contact', 'get', array( | |
238 | 'check_permissions' => 1, | |
239 | 'return' => $expectedReturnElements, | |
240 | 'sequential' => 1, | |
241 | )); | |
242 | $this->assertEquals(1, $result['count']); | |
243 | foreach ($expectedReturnElements as $element) { | |
244 | $this->assertArrayHasKey($element, $result['values'][0]); | |
6a488035 | 245 | } |
7884d958 | 246 | } |
247 | ||
248 | /** | |
eceb18cc | 249 | * Check that pledge IS not returned. |
7884d958 | 250 | */ |
00be9182 | 251 | public function testContactGetPledgeIDNotReturned() { |
7884d958 | 252 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults')); |
f5052d4d | 253 | $this->callAPISuccess('contact', 'get', array( |
7884d958 | 254 | 'sequential' => 1, |
255 | )); | |
256 | $result = $this->callAPISuccess('contact', 'get', array( | |
257 | 'check_permissions' => 1, | |
258 | 'return' => 'pledge_id', | |
259 | 'sequential' => 1, | |
260 | )); | |
261 | $this->assertArrayNotHasKey('pledge_id', $result['values'][0]); | |
262 | } | |
6a488035 | 263 | |
7884d958 | 264 | /** |
eceb18cc | 265 | * Check that pledge IS not an allowable filter. |
7884d958 | 266 | */ |
00be9182 | 267 | public function testContactGetPledgeIDNotFiltered() { |
7884d958 | 268 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults')); |
f5052d4d | 269 | $this->callAPISuccess('contact', 'get', array( |
7884d958 | 270 | 'sequential' => 1, |
271 | )); | |
272 | $result = $this->callAPISuccess('contact', 'get', array( | |
273 | 'check_permissions' => 1, | |
274 | 'pledge_id' => 1, | |
275 | 'sequential' => 1, | |
276 | )); | |
277 | $this->assertEquals(2, $result['count']); | |
278 | } | |
279 | ||
280 | /** | |
281 | * Check that chaining doesn't bypass permissions | |
282 | */ | |
00be9182 | 283 | public function testContactGetPledgeNotChainable() { |
7884d958 | 284 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereOnlySecond')); |
f5052d4d | 285 | $this->callAPISuccess('contact', 'get', array( |
7884d958 | 286 | 'sequential' => 1, |
287 | )); | |
f5052d4d | 288 | $this->callAPIFailure('contact', 'get', array( |
6a488035 | 289 | 'check_permissions' => 1, |
7884d958 | 290 | 'api.pledge.get' => 1, |
6a488035 | 291 | 'sequential' => 1, |
4e420887 | 292 | ), |
293 | 'Error in call to pledge_get : API permission check failed for pledge/get call; missing permission: access CiviCRM.' | |
7884d958 | 294 | ); |
295 | } | |
6a488035 | 296 | |
00be9182 | 297 | public function setupCoreACL() { |
ae4bb4c9 | 298 | $this->createLoggedInUser(); |
5896d037 | 299 | $this->_permissionedDisabledGroup = $this->groupCreate(array( |
92915c55 TO |
300 | 'title' => 'pick-me-disabled', |
301 | 'is_active' => 0, | |
302 | 'name' => 'pick-me-disabled', | |
303 | )); | |
5896d037 | 304 | $this->_permissionedGroup = $this->groupCreate(array( |
92915c55 TO |
305 | 'title' => 'pick-me-active', |
306 | 'is_active' => 1, | |
307 | 'name' => 'pick-me-active', | |
308 | )); | |
ae4bb4c9 EM |
309 | $this->setupACL(); |
310 | } | |
5896d037 | 311 | |
ae4bb4c9 EM |
312 | /** |
313 | * @dataProvider entities | |
314 | * confirm that without check permissions we still get 2 contacts returned | |
1e1fdcf6 | 315 | * @param $entity |
ae4bb4c9 | 316 | */ |
00be9182 | 317 | public function testEntitiesGetHookLimitingHookNoCheck($entity) { |
ae4bb4c9 EM |
318 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array(); |
319 | $this->setUpEntities($entity); | |
320 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults')); | |
321 | $result = $this->callAPISuccess($entity, 'get', array( | |
322 | 'check_permissions' => 0, | |
323 | 'return' => 'contact_id', | |
324 | )); | |
325 | $this->assertEquals(2, $result['count']); | |
326 | } | |
327 | ||
328 | /** | |
329 | * @dataProvider entities | |
330 | * confirm that without check permissions we still get 2 entities returned | |
1e1fdcf6 | 331 | * @param $entity |
ae4bb4c9 | 332 | */ |
00be9182 | 333 | public function testEntitiesGetCoreACLLimitingHookNoCheck($entity) { |
ae4bb4c9 EM |
334 | $this->setupCoreACL(); |
335 | //CRM_Core_Config::singleton()->userPermissionClass->permissions = array(); | |
336 | $this->setUpEntities($entity); | |
337 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults')); | |
338 | $result = $this->callAPISuccess($entity, 'get', array( | |
339 | 'check_permissions' => 0, | |
340 | 'return' => 'contact_id', | |
341 | )); | |
342 | $this->assertEquals(2, $result['count']); | |
343 | } | |
5896d037 | 344 | |
ae4bb4c9 EM |
345 | /** |
346 | * @dataProvider entities | |
347 | * confirm that with check permissions we don't get entities | |
1e1fdcf6 EM |
348 | * @param $entity |
349 | * @throws \PHPUnit_Framework_IncompleteTestError | |
ae4bb4c9 | 350 | */ |
00be9182 | 351 | public function testEntitiesGetCoreACLLimitingCheck($entity) { |
9730e9e3 | 352 | $this->markTestIncomplete('this does not work in 4.4 but can be enabled in 4.5 or a security release of 4.4 including the important security fix CRM-14877'); |
ae4bb4c9 EM |
353 | $this->setupCoreACL(); |
354 | $this->setUpEntities($entity); | |
ae4bb4c9 EM |
355 | $result = $this->callAPISuccess($entity, 'get', array( |
356 | 'check_permissions' => 1, | |
357 | 'return' => 'contact_id', | |
358 | )); | |
359 | $this->assertEquals(0, $result['count']); | |
360 | } | |
361 | ||
362 | ||
363 | /** | |
364 | * @dataProvider entities | |
365 | * Function tests that an empty where hook returns no results | |
1028f75e | 366 | * @param string $entity |
1e1fdcf6 | 367 | * @throws \PHPUnit_Framework_IncompleteTestError |
ae4bb4c9 | 368 | */ |
00be9182 | 369 | public function testEntityGetNoResultsHook($entity) { |
ae4bb4c9 EM |
370 | $this->markTestIncomplete('hook acls only work with contacts so far'); |
371 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array(); | |
372 | $this->setUpEntities($entity); | |
373 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults')); | |
374 | $result = $this->callAPISuccess($entity, 'get', array( | |
375 | 'check_permission' => 1, | |
376 | )); | |
377 | $this->assertEquals(0, $result['count']); | |
378 | } | |
379 | ||
380 | /** | |
381 | * @return array | |
382 | */ | |
383 | public static function entities() { | |
6c6e6187 | 384 | return array(array('contribution'), array('participant'));// @todo array('pledge' => 'pledge') |
ae4bb4c9 EM |
385 | } |
386 | ||
387 | /** | |
388 | * Create 2 entities | |
1e1fdcf6 | 389 | * @param $entity |
ae4bb4c9 EM |
390 | */ |
391 | public function setUpEntities($entity) { | |
392 | $baoObj = new CRM_Core_DAO(); | |
5896d037 | 393 | $baoObj->createTestObject(_civicrm_api3_get_BAO($entity), array(), 2, 0); |
ae4bb4c9 EM |
394 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array( |
395 | 'access CiviCRM', | |
396 | 'access CiviContribute', | |
397 | 'access CiviEvent', | |
398 | 'view event participants', | |
399 | ); | |
400 | } | |
401 | ||
6a488035 | 402 | /** |
eceb18cc | 403 | * No results returned. |
c16ed19b | 404 | * @implements CRM_Utils_Hook::aclWhereClause |
1e1fdcf6 EM |
405 | * @param $type |
406 | * @param $tables | |
407 | * @param $whereTables | |
408 | * @param $contactID | |
409 | * @param $where | |
6a488035 | 410 | */ |
00be9182 | 411 | public function aclWhereHookNoResults($type, &$tables, &$whereTables, &$contactID, &$where) { |
6a488035 | 412 | } |
7884d958 | 413 | |
6a488035 | 414 | /** |
eceb18cc | 415 | * All results returned. |
f5052d4d | 416 | * @implements CRM_Utils_Hook::aclWhereClause |
1e1fdcf6 EM |
417 | * @param $type |
418 | * @param $tables | |
419 | * @param $whereTables | |
420 | * @param $contactID | |
421 | * @param $where | |
7884d958 | 422 | */ |
00be9182 | 423 | public function aclWhereHookAllResults($type, &$tables, &$whereTables, &$contactID, &$where) { |
6a488035 TO |
424 | $where = " (1) "; |
425 | } | |
7884d958 | 426 | |
6a488035 | 427 | /** |
c16ed19b | 428 | * All but first results returned. |
f5052d4d | 429 | * @implements CRM_Utils_Hook::aclWhereClause |
1e1fdcf6 EM |
430 | * @param $type |
431 | * @param $tables | |
432 | * @param $whereTables | |
433 | * @param $contactID | |
434 | * @param $where | |
7884d958 | 435 | */ |
00be9182 | 436 | public function aclWhereOnlySecond($type, &$tables, &$whereTables, &$contactID, &$where) { |
6a488035 TO |
437 | $where = " contact_a.id > 1"; |
438 | } | |
96025800 | 439 | |
c16ed19b CW |
440 | /** |
441 | * Only specified contact returned. | |
442 | * @implements CRM_Utils_Hook::aclWhereClause | |
443 | * @param $type | |
444 | * @param $tables | |
445 | * @param $whereTables | |
446 | * @param $contactID | |
447 | * @param $where | |
448 | */ | |
449 | public function aclWhereOnlyOne($type, &$tables, &$whereTables, &$contactID, &$where) { | |
450 | $where = " contact_a.id = " . $this->allowedContactId; | |
451 | } | |
452 | ||
6a488035 | 453 | } |