-
-
Notifications
You must be signed in to change notification settings - Fork 827
/
Copy pathACLPermissionTest.php
1706 lines (1601 loc) · 54.7 KB
/
ACLPermissionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
use Civi\Api4\ACLEntityRole;
use Civi\Api4\Contact;
use Civi\Api4\CustomField;
use Civi\Api4\CustomGroup;
use Civi\Api4\CustomValue;
use Civi\Api4\Entity;
/**
* This class is intended to test ACL permission using the multisite module
*
* @package CiviCRM_APIv3
* @subpackage API_Contact
* @group headless
*/
class api_v3_ACLPermissionTest extends CiviUnitTestCase {
use Civi\Test\ACLPermissionTrait;
/**
* Should financials be checked after the test but before tear down.
*
* The setup methodology in this class bypasses valid financial creation
* so we don't check.
*
* @var bool
*/
protected $isValidateFinancialsOnPostAssert = FALSE;
protected $_entity;
/**
* @var int
*/
protected $_permissionedGroup;
/**
* @var int
*/
protected $_permissionedDisabledGroup;
/**
* @var string
*/
protected $aclGroupHookType;
public function setUp(): void {
parent::setUp();
CRM_Core_BAO_ConfigSetting::enableAllComponents();
CRM_Core_DAO::createTestObject('CRM_Pledge_BAO_Pledge', [], 1, 0);
$this->callAPISuccess('Phone', 'create', ['id' => $this->individualCreate(['email' => '']), 'phone' => '911', 'location_type_id' => 'Home']);
$this->prepareForACLs();
}
/**
* @see CiviUnitTestCase::tearDown()
*/
public function tearDown(): void {
$this->cleanUpAfterACLs();
$tablesToTruncate = [
'civicrm_contact',
'civicrm_address',
'civicrm_group_contact',
'civicrm_group',
'civicrm_acl',
'civicrm_acl_cache',
'civicrm_acl_entity_role',
'civicrm_acl_contact_cache',
'civicrm_contribution',
'civicrm_line_item',
'civicrm_participant',
'civicrm_uf_match',
'civicrm_activity',
'civicrm_activity_contact',
'civicrm_note',
'civicrm_entity_tag',
'civicrm_tag',
'civicrm_membership',
];
$this->quickCleanup($tablesToTruncate);
}
/**
* Function tests that an empty where hook returns no results.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testContactGetNoResultsHook(int $version): void {
$this->_apiversion = $version;
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookNoResults',
]);
$result = $this->callAPISuccess('contact', 'get', [
'check_permissions' => 1,
'return' => 'display_name',
]);
$this->assertEquals(0, $result['count']);
}
/**
* Function tests that an empty where hook returns exactly 1 result with "view my contact".
*
* CRM-16512 caused contacts with Edit my contact to be able to view all records.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testContactGetOneResultHookWithViewMyContact(int $version): void {
$this->_apiversion = $version;
$this->createLoggedInUser();
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookNoResults',
]);
CRM_Core_Config::singleton()->userPermissionClass->permissions = [
'access CiviCRM',
'view my contact',
];
$result = $this->callAPISuccess('contact', 'get', [
'check_permissions' => 1,
'return' => 'display_name',
]);
$this->assertEquals(1, $result['count']);
}
/**
* Function tests that a user with "edit my contact" can edit themselves.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testContactEditHookWithEditMyContact(int $version): void {
$this->_apiversion = $version;
$cid = $this->createLoggedInUser();
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookNoResults',
]);
CRM_Core_Config::singleton()->userPermissionClass->permissions = [
'access CiviCRM',
'edit my contact',
];
$this->callAPISuccess('contact', 'create', [
'check_permissions' => 1,
'id' => $cid,
'first_name' => 'NewName',
]);
}
/**
* Ensure contact permissions do not block contact-less location entities.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testAddressWithoutContactIDAccess(int $version): void {
$this->_apiversion = $version;
$ownID = $this->createLoggedInUser();
CRM_Core_Config::singleton()->userPermissionClass->permissions = [
'access CiviCRM',
'view all contacts',
];
$this->callAPISuccess('Address', 'create', [
'city' => 'Mouseville',
'location_type_id' => 'Main',
'api.LocBlock.create' => 1,
'contact_id' => $ownID,
]);
$this->callAPISuccessGetSingle('Address', [
'city' => 'Mouseville',
'check_permissions' => 1,
]);
CRM_Core_DAO::executeQuery('UPDATE civicrm_address SET contact_id = NULL WHERE contact_id = %1', [
1 => [
$ownID,
'Integer',
],
]);
$this->callAPISuccessGetSingle('Address', [
'city' => 'Mouseville',
'check_permissions' => 1,
]);
}
/**
* Ensure contact permissions extend to related entities like email
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testRelatedEntityPermissions(int $version): void {
$this->_apiversion = $version;
$this->createLoggedInUser();
$disallowedContact = $this->individualCreate([]);
$this->allowedContactId = $this->individualCreate([], 1);
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereOnlyOne',
]);
CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM'];
$testEntities = [
'Email' => ['email' => 'null@nothing', 'location_type_id' => 1],
'Phone' => ['phone' => '123456', 'location_type_id' => 1],
'IM' => ['name' => 'hello', 'location_type_id' => 1],
'Website' => ['url' => 'http://test'],
'Address' => [
'street_address' => '123 Sesame St.',
'location_type_id' => 1,
],
];
// v3 GroupContact API is nonstandard
if ($version === 4) {
$testEntities['GroupContact'] = ['group_id' => $this->groupCreate()];
}
foreach ($testEntities as $entity => $params) {
$params += [
'contact_id' => $disallowedContact,
'check_permissions' => 1,
];
// We should be prevented from getting or creating entities for a contact we don't have permission for
$this->callAPIFailure($entity, 'create', $params);
$this->callAPISuccess($entity, 'create', ['check_permissions' => 0] + $params);
$results = $this->callAPISuccess($entity, 'get', [
'contact_id' => $disallowedContact,
'check_permissions' => 1,
]);
$this->assertEquals(0, $results['count']);
// We should be allowed to create and get for contacts we do have permission on
$params['contact_id'] = $this->allowedContactId;
$this->callAPISuccess($entity, 'create', $params);
$results = $this->callAPISuccess($entity, 'get', [
'contact_id' => $this->allowedContactId,
'check_permissions' => 1,
]);
$this->assertGreaterThan(0, $results['count']);
}
$newTag = civicrm_api3('Tag', 'create', [
'name' => 'Foo123',
]);
$relatedEntities = [
'Note' => ['note' => 'abc'],
'EntityTag' => ['tag_id' => $newTag['id']],
];
foreach ($relatedEntities as $entity => $params) {
$params += [
'entity_id' => $disallowedContact,
'entity_table' => 'civicrm_contact',
'check_permissions' => 1,
];
// We should be prevented from getting or creating entities for a contact we don't have permission for
$this->callAPIFailure($entity, 'create', $params);
$this->callAPISuccess($entity, 'create', ['check_permissions' => 0] + $params);
$results = $this->callAPISuccess($entity, 'get', [
'entity_id' => $disallowedContact,
'entity_table' => 'civicrm_contact',
'check_permissions' => 1,
]);
$this->assertEquals(0, $results['count']);
// We should be allowed to create and get for entities we do have permission on
$params['entity_id'] = $this->allowedContactId;
$this->callAPISuccess($entity, 'create', $params);
$results = $this->callAPISuccess($entity, 'get', [
'entity_id' => $this->allowedContactId,
'entity_table' => 'civicrm_contact',
'check_permissions' => 1,
]);
$this->assertGreaterThan(0, $results['count']);
}
}
/**
* Function tests all results are returned.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testContactGetAllResultsHook(int $version): void {
$this->_apiversion = $version;
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookAllResults',
]);
$result = $this->callAPISuccess('contact', 'get', [
'check_permissions' => 1,
'return' => 'display_name',
]);
$this->assertEquals(2, $result['count']);
}
/**
* Function tests that deleted contacts are not returned.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testContactGetPermissionHookNoDeleted(int $version): void {
$this->_apiversion = $version;
$this->callAPISuccess('contact', 'create', ['id' => 2, 'is_deleted' => 1]);
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookAllResults',
]);
$result = $this->callAPISuccess('contact', 'get', [
'check_permissions' => 1,
'return' => 'display_name',
]);
$this->assertEquals(1, $result['count']);
}
/**
* Test permissions limited by hook.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testContactGetHookLimitingHook(int $version): void {
$this->_apiversion = $version;
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereOnlySecond',
]);
$result = $this->callAPISuccess('contact', 'get', [
'check_permissions' => 1,
'return' => 'display_name',
]);
$this->assertEquals(1, $result['count']);
}
/**
* Confirm that without check permissions we still get 2 contacts returned.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testContactGetHookLimitingHookDontCheck(int $version): void {
$this->_apiversion = $version;
$result = $this->callAPISuccess('contact', 'get', [
'check_permissions' => 0,
'return' => 'display_name',
]);
$this->assertEquals(2, $result['count']);
}
/**
* Check that id works as a filter.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testContactGetIDFilter(int $version): void {
$this->_apiversion = $version;
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookAllResults',
]);
$result = $this->callAPISuccess('contact', 'get', [
'sequential' => 1,
'id' => 2,
'check_permissions' => 1,
]);
$this->assertEquals(1, $result['count']);
$this->assertEquals(2, $result['id']);
}
/**
* Check that address IS returned.
*/
public function testContactGetAddressReturned(): void {
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereOnlySecond',
]);
$fullResult = $this->callAPISuccess('contact', 'get', [
'sequential' => 1,
]);
//return doesn't work for all keys - can't fix that here so let's skip ...
//prefix & suffix are inconsistent due to CRM-7929
// unsure about others but return doesn't work on them
$elementsReturnDoesntSupport = [
'prefix',
'suffix',
'gender',
'current_employer',
'phone_id',
'phone_type_id',
'phone',
'worldregion_id',
'world_region',
];
$expectedReturnElements = array_diff(array_keys($fullResult['values'][0]), $elementsReturnDoesntSupport);
$result = $this->callAPISuccess('contact', 'get', [
'check_permissions' => 1,
'return' => $expectedReturnElements,
'sequential' => 1,
]);
$this->assertEquals(1, $result['count']);
foreach ($expectedReturnElements as $element) {
$this->assertArrayHasKey($element, $result['values'][0]);
}
}
/**
* Check that pledge IS not returned.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testContactGetPledgeIDNotReturned(int $version): void {
$this->_apiversion = $version;
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookAllResults',
]);
$this->callAPISuccess('contact', 'get', [
'sequential' => 1,
]);
$result = $this->callAPISuccess('contact', 'get', [
'check_permissions' => 1,
'return' => 'pledge_id',
'sequential' => 1,
]);
$this->assertArrayNotHasKey('pledge_id', $result['values'][0]);
}
/**
* Check that pledge IS not an allowable filter.
*/
public function testContactGetPledgeIDNotFiltered(): void {
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookAllResults',
]);
$this->callAPISuccess('contact', 'get', [
'sequential' => 1,
]);
$result = $this->callAPISuccess('contact', 'get', [
'check_permissions' => 1,
'pledge_id' => 1,
'sequential' => 1,
]);
$this->assertEquals(2, $result['count']);
}
/**
* Check that chaining doesn't bypass permissions
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testContactGetPledgeNotChainable(int $version): void {
$this->_apiversion = $version;
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereOnlySecond',
]);
$this->callAPISuccess('contact', 'get', [
'sequential' => 1,
]);
$this->callAPIFailure('contact', 'get', [
'check_permissions' => 1,
'api.pledge.get' => 1,
'sequential' => 1,
],
'Error in call to Pledge_get : API permission check failed for Pledge/get call; insufficient permission: require access CiviCRM and access CiviPledge'
);
}
public function setupCoreACL(): void {
$this->createLoggedInUser();
$this->_permissionedDisabledGroup = $this->groupCreate([
'title' => 'pick-me-disabled',
'is_active' => 0,
'name' => 'pick-me-disabled',
]);
$this->_permissionedGroup = $this->groupCreate([
'title' => 'pick-me-active',
'is_active' => 1,
'name' => 'pick-me-active',
]);
$this->setupACL();
}
/**
* @dataProvider entities
* confirm that without check permissions we still get 2 contacts returned
*
* @param string $entity
* @param int $apiVersion
*/
public function testEntitiesGetHookLimitingHookNoCheck(string $entity, int $apiVersion): void {
$this->_apiversion = $apiVersion;
CRM_Core_Config::singleton()->userPermissionClass->permissions = [];
$this->setUpEntities($entity);
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookNoResults',
]);
$result = $this->callAPISuccess($entity, 'get', [
'check_permissions' => 0,
'return' => 'contact_id',
]);
$this->assertEquals(2, $result['count'], "failed with entity : $entity and api version $apiVersion");
}
/**
* @dataProvider entities
* confirm that without check permissions we still get 2 entities returned
*
* @param string $entity
* @param int $apiVersion
*/
public function testEntitiesGetCoreACLLimitingHookNoCheck(string $entity, int $apiVersion): void {
$this->_apiversion = $apiVersion;
$this->setupCoreACL();
//CRM_Core_Config::singleton()->userPermissionClass->permissions = array();
$this->setUpEntities($entity);
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookNoResults',
]);
$result = $this->callAPISuccess($entity, 'get', [
'check_permissions' => 0,
'return' => 'contact_id',
]);
$this->assertEquals(2, $result['count'], "failed with entity : $entity and api version $apiVersion");
}
/**
* @dataProvider entities
* confirm that with check permissions we don't get entities
*
* @param $entity
* @param $apiVersion
*/
public function testEntitiesGetCoreACLLimitingCheck($entity, $apiVersion): void {
$this->_apiversion = $apiVersion;
$this->setupCoreACL();
$this->setUpEntities($entity);
$result = $this->callAPISuccess($entity, 'get', [
'check_permissions' => 1,
'return' => 'contact_id',
]);
$this->assertEquals(0, $result['count'], "failed with entity : $entity and api version $apiVersion");
}
/**
* @dataProvider entities
* Function tests that an empty where hook returns no results
*
* @param string $entity
* @param int $apiVersion
*/
public function testEntityGetNoResultsHook(string $entity, int $apiVersion): void {
$this->_apiversion = $apiVersion;
$this->markTestIncomplete('hook acls only work with contacts so far');
CRM_Core_Config::singleton()->userPermissionClass->permissions = [];
$this->setUpEntities($entity);
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookNoResults',
]);
$result = $this->callAPISuccess($entity, 'get', [
'check_permission' => 1,
]);
$this->assertEquals(0, $result['count']);
}
/**
* @return array
*/
public static function entities(): array {
return [
['contribution', 3],
['participant', 3],
];
}
/**
* Create 2 entities.
*
* @param string $entity
*/
public function setUpEntities(string $entity): void {
CRM_Core_DAO::createTestObject(_civicrm_api3_get_BAO($entity), [], 2, 0);
CRM_Core_Config::singleton()->userPermissionClass->permissions = [
'access CiviCRM',
'access CiviContribute',
'access CiviEvent',
'view event participants',
'access CiviMember',
];
}
/**
* Basic check that an un-permissioned call keeps working and permissioned call fails.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testGetActivityNoPermissions(int $version): void {
$this->_apiversion = $version;
$this->setPermissions([]);
$this->callAPISuccess('Activity', 'get');
$this->callAPIFailure('Activity', 'get', ['check_permissions' => 1]);
}
/**
* View all activities is enough regardless of contact ACLs.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testGetActivityViewAllActivitiesDoesntCutItAnymore(int $version): void {
$this->_apiversion = $version;
$activity = $this->activityCreate();
$this->setPermissions(['view all activities', 'access CiviCRM']);
$this->callAPISuccessGetCount('Activity', [
'check_permissions' => 1,
'id' => $activity['id'],
], 0);
}
/**
* View all activities is required unless id is passed in.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testGetActivityViewAllContactsEnoughWithoutID(int $version): void {
$this->_apiversion = $version;
$this->setPermissions(['view all contacts', 'access CiviCRM']);
$this->callAPISuccess('Activity', 'get', ['check_permissions' => 1]);
}
/**
* Without view all activities contact level acls are used.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testGetActivityViewAllContactsEnoughWIthID(int $version): void {
$this->_apiversion = $version;
$activity = $this->activityCreate();
$this->setPermissions(['view all contacts', 'access CiviCRM']);
$this->callAPISuccess('Activity', 'getsingle', [
'check_permissions' => 1,
'id' => $activity['id'],
]);
}
/**
* Check the error message is not a permission error.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testGetActivityAccessCiviCRMEnough(int $version): void {
$this->_apiversion = $version;
$activity = $this->activityCreate();
$this->setPermissions(['access CiviCRM']);
$this->callAPIFailure('Activity', 'getsingle', [
'check_permissions' => 1,
'id' => $activity['id'],
'return' => 'id',
], 'Expected one Activity but found 0');
$this->callAPISuccessGetCount('Activity', [
'check_permissions' => 1,
'id' => $activity['id'],
], 0);
}
/**
* Check that component related activity filtering.
*
* If the contact does NOT have permission to 'view all contacts' but they DO have permission
* to view the contact in question they will only see the activities of components they have access too.
*
* (logically the same component limit should apply when they have access to view all too but....
* adding test for 'how it is at the moment.)
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testGetActivityCheckPermissionsByComponent(int $version): void {
$this->_apiversion = $version;
$activity = $this->activityCreate(['activity_type_id' => 'Contribution']);
$activity2 = $this->activityCreate(['activity_type_id' => 'Pledge Reminder']);
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookAllResults',
]);
$this->setPermissions(['access CiviCRM', 'access CiviContribute']);
$this->callAPISuccessGetCount('Activity', [
'check_permissions' => 1,
'id' => ['IN' => [$activity['id'], $activity2['id']]],
], 1);
$this->callAPISuccessGetCount('Activity', [
'check_permissions' => 1,
'id' => ['IN' => [$activity['id'], $activity2['id']]],
], 1);
}
/**
* Check that component related activity filtering works for CiviCase.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testGetActivityCheckPermissionsByCaseComponent(int $version): void {
$this->_apiversion = $version;
$activity = $this->activityCreate(['activity_type_id' => 'Open Case']);
$activity2 = $this->activityCreate(['activity_type_id' => 'Pledge Reminder']);
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookAllResults',
]);
$this->setPermissions([
'access CiviCRM',
'access CiviContribute',
'access all cases and activities',
]);
$this->callAPISuccessGetCount('Activity', [
'check_permissions' => 1,
'id' => ['IN' => [$activity['id'], $activity2['id']]],
], 1);
$this->callAPISuccessGetCount('Activity', [
'check_permissions' => 1,
'id' => ['IN' => [$activity['id'], $activity2['id']]],
], 1);
}
/**
* Check that activities can be retrieved by ACL.
*
* The activities api applies ACLs in a very limited circumstance, if id is passed in.
* Otherwise it sticks with the blunt original permissions.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testGetActivityByACL(int $version): void {
$this->_apiversion = $version;
$this->setPermissions(['access CiviCRM']);
$activity = $this->activityCreate();
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookAllResults',
]);
$this->callAPISuccessGetCount('Activity', [
'check_permissions' => 1,
'id' => $activity['id'],
], 1);
$this->callAPISuccessGetCount('Activity', [
'check_permissions' => 1,
'id' => $activity['id'],
]);
}
/**
* To leverage ACL permission to view an activity you must be able to see any
* of the contacts. FIXME: Api4
*
* @throws \CRM_Core_Exception
*/
public function testGetActivityByAclCannotViewAllContacts(): void {
$activity = $this->activityCreate(['assignee_contact_id' => $this->individualCreate()]);
$contacts = $this->getActivityContacts($activity);
$this->setPermissions(['access CiviCRM']);
foreach ($contacts as $role => $contact_id) {
$this->allowedContactId = $contact_id;
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereOnlyOne',
]);
$this->cleanupCachedPermissions();
$result = $this->callAPISuccessGetSingle('Activity', [
'check_permissions' => 1,
'id' => $activity['id'],
'return' => [
'source_contact_id',
'target_contact_id',
'assignee_contact_id',
],
]);
foreach ([
'source_contact',
'target_contact',
'assignee_contact',
] as $roleName) {
$roleKey = $roleName . '_id';
if ($role !== $roleKey) {
$this->assertTrue(empty($result[$roleKey]), "Only contact in $role is permissioned to be returned, not $roleKey");
}
else {
$this->assertEquals([$contact_id], (array) $result[$roleKey]);
$this->assertNotEmpty($result[$roleName . '_name']);
}
}
}
}
/**
* To leverage ACL permission to view an activity you must be able to see any of the contacts.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testGetActivityByAclCannotViewAnyContacts(int $version): void {
$this->_apiversion = $version;
$activity = $this->activityCreate();
$contacts = $this->getActivityContacts($activity);
$this->setPermissions(['access CiviCRM']);
foreach ($contacts as $contact_id) {
$this->callAPIFailure('Activity', 'getsingle', [
'check_permissions' => 1,
'id' => $activity['id'],
]);
}
}
/**
* Check that if the source contact is deleted but we can view the others we can see the activity.
*
* CRM-18409.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
* @throws \CRM_Core_Exception
*/
public function testGetActivityACLSourceContactDeleted(int $version): void {
$this->_apiversion = $version;
$this->setPermissions(['access CiviCRM', 'delete contacts']);
$activity = $this->activityCreate();
$contacts = $this->getActivityContacts($activity);
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookAllResults',
]);
$this->contactDelete($contacts['source_contact_id']);
$this->callAPISuccessGetCount('Activity', [
'check_permissions' => 1,
'id' => $activity['id'],
], 1);
}
/**
* Test get activities multiple ids with check permissions
*
* @see https://issues.civicrm.org/jira/browse/CRM-20441
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testActivitiesGetMultipleIdsCheckPermissions(int $version): void {
$this->_apiversion = $version;
$this->createLoggedInUser();
$activity = $this->activityCreate();
$activity2 = $this->activityCreate();
$this->setPermissions(['access CiviCRM']);
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookAllResults',
]);
// Get activities associated with contact $this->_contactID.
$params = [
'id' => ['IN' => [$activity['id'], $activity2['id']]],
'check_permissions' => TRUE,
];
$this->callAPISuccessGetCount('Activity', $params, 2);
}
/**
* Test get activities multiple ids with check permissions
* Limit access to One contact
*
* @see https://issues.civicrm.org/jira/browse/CRM-20441
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testActivitiesGetMultipleIdsCheckPermissionsLimitedACL(int $version): void {
$this->_apiversion = $version;
$this->createLoggedInUser();
$activity = $this->activityCreate();
$contacts = $this->getActivityContacts($activity);
$this->setPermissions(['access CiviCRM']);
foreach ($contacts as $contact_id) {
$this->allowedContacts[] = $contact_id;
}
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereMultipleContacts',
]);
$contact2 = $this->individualCreate();
$activity2 = $this->activityCreate(['source_contact_id' => $contact2]);
// Get activities associated with contact $this->_contactID.
$params = [
'id' => ['IN' => [$activity['id']]],
'check_permissions' => TRUE,
];
$result = $this->callAPISuccess('activity', 'get', $params);
$this->assertEquals(1, $result['count']);
$this->callAPIFailure('activity', 'getsingle', array_merge($params, [
'id' => [
'IN' => [$activity2['id']],
],
]));
}
/**
* Test get activities multiple ids with check permissions
*
* @see https://issues.civicrm.org/jira/browse/CRM-20441
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testActivitiesGetMultipleIdsCheckPermissionsNotIN(int $version): void {
$this->_apiversion = $version;
$this->createLoggedInUser();
$activity = $this->activityCreate();
$activity2 = $this->activityCreate();
$this->setPermissions(['access CiviCRM']);
$this->hookClass->setHook('civicrm_aclWhereClause', [
$this,
'aclWhereHookAllResults',
]);
// Get activities associated with contact $this->_contactID.
$params = [
'id' => ['NOT IN' => [$activity['id'], $activity2['id']]],
'check_permissions' => TRUE,
];
$result = $this->callAPISuccess('activity', 'get', $params);
$this->assertEquals(0, $result['count']);
}
/**
* Get the contacts for the activity.
*
* @param $activity
*
* @return array
*/