Merge pull request #14326 from civicrm/5.14
[civicrm-core.git] / CRM / Core / Permission.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
35 * This is the basic permission class wrapper
36 */
37class CRM_Core_Permission {
38
39 /**
d09edf64 40 * Static strings used to compose permissions.
6a488035
TO
41 *
42 * @const
43 * @var string
44 */
7da04cde 45 const EDIT_GROUPS = 'edit contacts in ', VIEW_GROUPS = 'view contacts in ';
6a488035
TO
46
47 /**
d09edf64 48 * The various type of permissions.
6a488035
TO
49 *
50 * @var int
51 */
7da04cde 52 const EDIT = 1, VIEW = 2, DELETE = 3, CREATE = 4, SEARCH = 5, ALL = 6, ADMIN = 7;
6a488035 53
085823c1 54 /**
d09edf64 55 * A placeholder permission which always fails.
085823c1
TO
56 */
57 const ALWAYS_DENY_PERMISSION = "*always deny*";
58
59 /**
d09edf64 60 * A placeholder permission which always fails.
085823c1
TO
61 */
62 const ALWAYS_ALLOW_PERMISSION = "*always allow*";
63
6dd18b98 64 /**
d09edf64 65 * Various authentication sources.
6dd18b98
DS
66 *
67 * @var int
68 */
7da04cde 69 const AUTH_SRC_UNKNOWN = 0, AUTH_SRC_CHECKSUM = 1, AUTH_SRC_SITEKEY = 2, AUTH_SRC_LOGIN = 4;
6dd18b98 70
6a488035 71 /**
d09edf64 72 * Get the current permission of this user.
6a488035 73 *
a6c01b45
CW
74 * @return string
75 * the permission of the user (edit or view or null)
6a488035
TO
76 */
77 public static function getPermission() {
78 $config = CRM_Core_Config::singleton();
41f314b6 79 return $config->userPermissionClass->getPermission();
6a488035
TO
80 }
81
82 /**
100fef9d 83 * Given a permission string or array, check for access requirements
18be3201 84 * @param string|array $permissions
6a0b768e 85 * The permission to check as an array or string -see examples.
18be3201
CW
86 *
87 * @param int $contactId
88 * Contact id to check permissions for. Defaults to current logged-in user.
6a488035 89 *
60ec9f43
E
90 * Ex 1
91 *
92 * Must have 'access CiviCRM'
93 * (string) 'access CiviCRM'
94 *
95 *
96 * Ex 2 Must have 'access CiviCRM' and 'access Ajax API'
97 * array('access CiviCRM', 'access Ajax API')
98 *
99 * Ex 3 Must have 'access CiviCRM' or 'access Ajax API'
100 * array(
101 * array('access CiviCRM', 'access Ajax API'),
102 * ),
103 *
104 * Ex 4 Must have 'access CiviCRM' or 'access Ajax API' AND 'access CiviEvent'
105 * array(
106 * array('access CiviCRM', 'access Ajax API'),
107 * 'access CiviEvent',
108 * ),
109 *
110 * Note that in permissions.php this is keyed by the action eg.
111 * (access Civi || access AJAX) && (access CiviEvent || access CiviContribute)
112 * 'myaction' => array(
113 * array('access CiviCRM', 'access Ajax API'),
114 * array('access CiviEvent', 'access CiviContribute')
115 * ),
6a488035 116 *
acb1052e 117 * @return bool
a6c01b45 118 * true if yes, else false
6a488035 119 */
18be3201 120 public static function check($permissions, $contactId = NULL) {
60ec9f43 121 $permissions = (array) $permissions;
18be3201
CW
122 $userId = NULL;
123 if ($contactId) {
124 $userId = CRM_Core_BAO_UFMatch::getUFId($contactId);
125 }
60ec9f43 126
18be3201 127 /** @var CRM_Core_Permission_Temp $tempPerm */
59735506
TO
128 $tempPerm = CRM_Core_Config::singleton()->userPermissionTemp;
129
60ec9f43 130 foreach ($permissions as $permission) {
22e263ad 131 if (is_array($permission)) {
60ec9f43 132 foreach ($permission as $orPerm) {
18be3201 133 if (self::check($orPerm, $contactId)) {
60ec9f43 134 //one of our 'or' permissions has succeeded - stop checking this permission
b8a19656 135 return TRUE;
60ec9f43
E
136 }
137 }
138 //none of our our conditions was met
139 return FALSE;
140 }
141 else {
3ae62cab 142 // This is an individual permission
18be3201 143 $granted = CRM_Core_Config::singleton()->userPermissionClass->check($permission, $userId);
3ae62cab 144 // Call the permission_check hook to permit dynamic escalation (CRM-19256)
fa4dac9c 145 CRM_Utils_Hook::permission_check($permission, $granted, $contactId);
59735506 146 if (
3ae62cab 147 !$granted
59735506
TO
148 && !($tempPerm && $tempPerm->check($permission))
149 ) {
60ec9f43
E
150 //one of our 'and' conditions has not been met
151 return FALSE;
152 }
153 }
154 }
155 return TRUE;
6a488035
TO
156 }
157
dc92f2f8 158 /**
d09edf64 159 * Determine if any one of the permissions strings applies to current user.
dc92f2f8
TO
160 *
161 * @param array $perms
162 * @return bool
163 */
164 public static function checkAnyPerm($perms) {
165 foreach ($perms as $perm) {
166 if (CRM_Core_Permission::check($perm)) {
167 return TRUE;
168 }
169 }
170 return FALSE;
171 }
172
6a488035
TO
173 /**
174 * Given a group/role array, check for access requirements
175 *
6a0b768e
TO
176 * @param array $array
177 * The group/role to check.
6a488035 178 *
acb1052e 179 * @return bool
a6c01b45 180 * true if yes, else false
6a488035 181 */
00be9182 182 public static function checkGroupRole($array) {
6a488035 183 $config = CRM_Core_Config::singleton();
41f314b6 184 return $config->userPermissionClass->checkGroupRole($array);
6a488035
TO
185 }
186
187 /**
d09edf64 188 * Get the permissioned where clause for the user.
6a488035 189 *
6a0b768e
TO
190 * @param int $type
191 * The type of permission needed.
192 * @param array $tables
193 * (reference ) add the tables that are needed for the select clause.
194 * @param array $whereTables
195 * (reference ) add the tables that are needed for the where clause.
6a488035 196 *
a6c01b45
CW
197 * @return string
198 * the group where clause for this user
6a488035
TO
199 */
200 public static function getPermissionedStaticGroupClause($type, &$tables, &$whereTables) {
201 $config = CRM_Core_Config::singleton();
41f314b6 202 return $config->userPermissionClass->getPermissionedStaticGroupClause($type, $tables, $whereTables);
6a488035
TO
203 }
204
205 /**
206 * Get all groups from database, filtered by permissions
207 * for this user
208 *
6a0b768e
TO
209 * @param string $groupType
210 * Type of group(Access/Mailing).
3f8d2862
CW
211 * @param bool $excludeHidden
212 * exclude hidden groups.
6a488035 213 *
6a488035 214 *
a6c01b45
CW
215 * @return array
216 * array reference of all groups.
6a488035
TO
217 */
218 public static function group($groupType, $excludeHidden = TRUE) {
219 $config = CRM_Core_Config::singleton();
41f314b6 220 return $config->userPermissionClass->group($groupType, $excludeHidden);
6a488035
TO
221 }
222
a0ee3941
EM
223 /**
224 * @return bool
225 */
6a488035
TO
226 public static function customGroupAdmin() {
227 $admin = FALSE;
228
229 // check if user has all powerful permission
230 // or administer civicrm permission (CRM-1905)
231 if (self::check('access all custom data')) {
232 return TRUE;
233 }
234
634e1a1a
DL
235 if (
236 self::check('administer Multiple Organizations') &&
6a488035
TO
237 self::isMultisiteEnabled()
238 ) {
239 return TRUE;
240 }
241
242 if (self::check('administer CiviCRM')) {
243 return TRUE;
244 }
245
246 return FALSE;
247 }
248
a0ee3941
EM
249 /**
250 * @param int $type
251 * @param bool $reset
252 *
253 * @return array
254 */
6a488035 255 public static function customGroup($type = CRM_Core_Permission::VIEW, $reset = FALSE) {
41f314b6 256 $customGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id',
be2fb01f
CW
257 ['fresh' => $reset]);
258 $defaultGroups = [];
6a488035
TO
259
260 // check if user has all powerful permission
261 // or administer civicrm permission (CRM-1905)
262 if (self::customGroupAdmin()) {
263 $defaultGroups = array_keys($customGroups);
264 }
265
266 return CRM_ACL_API::group($type, NULL, 'civicrm_custom_group', $customGroups, $defaultGroups);
267 }
268
a0ee3941
EM
269 /**
270 * @param int $type
271 * @param null $prefix
272 * @param bool $reset
273 *
274 * @return string
275 */
00be9182 276 public static function customGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $reset = FALSE) {
6a488035
TO
277 if (self::customGroupAdmin()) {
278 return ' ( 1 ) ';
279 }
280
281 $groups = self::customGroup($type, $reset);
282 if (empty($groups)) {
283 return ' ( 0 ) ';
284 }
285 else {
286 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
287 }
288 }
289
a0ee3941 290 /**
100fef9d 291 * @param int $gid
a0ee3941
EM
292 * @param int $type
293 *
294 * @return bool
295 */
6a488035
TO
296 public static function ufGroupValid($gid, $type = CRM_Core_Permission::VIEW) {
297 if (empty($gid)) {
298 return TRUE;
299 }
300
301 $groups = self::ufGroup($type);
684b0b22 302 return !empty($groups) && in_array($gid, $groups) ? TRUE : FALSE;
6a488035
TO
303 }
304
a0ee3941
EM
305 /**
306 * @param int $type
307 *
308 * @return array
309 */
6a488035 310 public static function ufGroup($type = CRM_Core_Permission::VIEW) {
ff4f7744 311 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
6a488035
TO
312
313 $allGroups = array_keys($ufGroups);
314
315 // check if user has all powerful permission
316 if (self::check('profile listings and forms')) {
317 return $allGroups;
318 }
319
320 switch ($type) {
321 case CRM_Core_Permission::VIEW:
322 if (self::check('profile view')) {
323 return $allGroups;
324 }
325 break;
326
327 case CRM_Core_Permission::CREATE:
328 if (self::check('profile create')) {
329 return $allGroups;
330 }
331 break;
332
333 case CRM_Core_Permission::EDIT:
334 if (self::check('profile edit')) {
335 return $allGroups;
336 }
337 break;
338
339 case CRM_Core_Permission::SEARCH:
340 if (self::check('profile listings')) {
341 return $allGroups;
342 }
343 break;
344 }
345
346 return CRM_ACL_API::group($type, NULL, 'civicrm_uf_group', $ufGroups);
347 }
348
a0ee3941
EM
349 /**
350 * @param int $type
351 * @param null $prefix
352 * @param bool $returnUFGroupIds
353 *
354 * @return array|string
355 */
00be9182 356 public static function ufGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $returnUFGroupIds = FALSE) {
6a488035
TO
357 $groups = self::ufGroup($type);
358 if ($returnUFGroupIds) {
359 return $groups;
360 }
361 elseif (empty($groups)) {
362 return ' ( 0 ) ';
363 }
364 else {
365 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
366 }
367 }
368
a0ee3941
EM
369 /**
370 * @param int $type
100fef9d 371 * @param int $eventID
a0ee3941
EM
372 * @param string $context
373 *
374 * @return array|null
375 */
e2d09ab4 376 public static function event($type = CRM_Core_Permission::VIEW, $eventID = NULL, $context = '') {
22e263ad
TO
377 if (!empty($context)) {
378 if (CRM_Core_Permission::check($context)) {
e2d09ab4
EM
379 return TRUE;
380 }
381 }
6a488035 382 $events = CRM_Event_PseudoConstant::event(NULL, TRUE);
be2fb01f 383 $includeEvents = [];
6a488035
TO
384
385 // check if user has all powerful permission
386 if (self::check('register for events')) {
387 $includeEvents = array_keys($events);
388 }
389
390 if ($type == CRM_Core_Permission::VIEW &&
391 self::check('view event info')
392 ) {
393 $includeEvents = array_keys($events);
394 }
395
396 $permissionedEvents = CRM_ACL_API::group($type, NULL, 'civicrm_event', $events, $includeEvents);
397 if (!$eventID) {
398 return $permissionedEvents;
399 }
41f314b6 400 if (!empty($permissionedEvents)) {
2efcf0c2 401 return array_search($eventID, $permissionedEvents) === FALSE ? NULL : $eventID;
41f314b6 402 }
dfa720e7 403 return NULL;
6a488035
TO
404 }
405
a0ee3941
EM
406 /**
407 * @param int $type
408 * @param null $prefix
409 *
410 * @return string
411 */
00be9182 412 public static function eventClause($type = CRM_Core_Permission::VIEW, $prefix = NULL) {
6a488035
TO
413 $events = self::event($type);
414 if (empty($events)) {
415 return ' ( 0 ) ';
416 }
417 else {
418 return "{$prefix}id IN ( " . implode(',', $events) . ' ) ';
419 }
420 }
421
a0ee3941 422 /**
44d17ec8
BS
423 * Checks that component is enabled and optionally that user has basic perm.
424 *
425 * @param string $module
426 * Specifies the name of the CiviCRM component.
a0ee3941 427 * @param bool $checkPermission
44d17ec8
BS
428 * Check not only that module is enabled, but that user has necessary
429 * permission.
430 * @param bool $requireAllCasesPermOnCiviCase
431 * Significant only if $module == CiviCase
432 * Require "access all cases and activities", not just
433 * "access my cases and activities".
a0ee3941
EM
434 *
435 * @return bool
44d17ec8 436 * Access to specified $module is granted.
a0ee3941 437 */
44d17ec8 438 public static function access($module, $checkPermission = TRUE, $requireAllCasesPermOnCiviCase = FALSE) {
6a488035
TO
439 $config = CRM_Core_Config::singleton();
440
441 if (!in_array($module, $config->enableComponents)) {
442 return FALSE;
443 }
444
445 if ($checkPermission) {
44d17ec8
BS
446 switch ($module) {
447 case 'CiviCase':
448 $access_all_cases = CRM_Core_Permission::check("access all cases and activities");
449 $access_my_cases = CRM_Core_Permission::check("access my cases and activities");
450 return $access_all_cases || (!$requireAllCasesPermOnCiviCase && $access_my_cases);
451
452 case 'CiviCampaign':
453 return CRM_Core_Permission::check("administer $module");
454
455 default:
456 return CRM_Core_Permission::check("access $module");
6a488035
TO
457 }
458 }
459
460 return TRUE;
461 }
462
463 /**
d09edf64 464 * Check permissions for delete and edit actions.
6a488035 465 *
6a0b768e
TO
466 * @param string $module
467 * Component name.
468 * @param int $action
469 * Action to be check across component.
6a488035 470 *
77b97be7
EM
471 *
472 * @return bool
473 */
00be9182 474 public static function checkActionPermission($module, $action) {
6a488035
TO
475 //check delete related permissions.
476 if ($action & CRM_Core_Action::DELETE) {
477 $permissionName = "delete in $module";
478 }
479 else {
be2fb01f 480 $editPermissions = [
6a488035
TO
481 'CiviEvent' => 'edit event participants',
482 'CiviMember' => 'edit memberships',
483 'CiviPledge' => 'edit pledges',
484 'CiviContribute' => 'edit contributions',
485 'CiviGrant' => 'edit grants',
486 'CiviMail' => 'access CiviMail',
487 'CiviAuction' => 'add auction items',
be2fb01f 488 ];
6a488035
TO
489 $permissionName = CRM_Utils_Array::value($module, $editPermissions);
490 }
491
492 if ($module == 'CiviCase' && !$permissionName) {
493 return CRM_Case_BAO_Case::accessCiviCase();
494 }
495 else {
496 //check for permission.
497 return CRM_Core_Permission::check($permissionName);
498 }
499 }
500
a0ee3941
EM
501 /**
502 * @param $args
503 * @param string $op
504 *
505 * @return bool
506 */
00be9182 507 public static function checkMenu(&$args, $op = 'and') {
6a488035
TO
508 if (!is_array($args)) {
509 return $args;
510 }
511 foreach ($args as $str) {
512 $res = CRM_Core_Permission::check($str);
513 if ($op == 'or' && $res) {
514 return TRUE;
515 }
516 elseif ($op == 'and' && !$res) {
517 return FALSE;
518 }
519 }
520 return ($op == 'or') ? FALSE : TRUE;
521 }
522
a0ee3941
EM
523 /**
524 * @param $item
525 *
526 * @return bool|mixed
527 * @throws Exception
528 */
00be9182 529 public static function checkMenuItem(&$item) {
6a488035
TO
530 if (!array_key_exists('access_callback', $item)) {
531 CRM_Core_Error::backtrace();
532 CRM_Core_Error::fatal();
533 }
534
535 // if component_id is present, ensure it is enabled
3d31a5c6
TO
536 if (isset($item['component_id']) && $item['component_id']) {
537 if (!isset(Civi::$statics[__CLASS__]['componentNameId'])) {
538 Civi::$statics[__CLASS__]['componentNameId'] = array_flip(CRM_Core_Component::getComponentIDs());
539 }
540 $componentName = Civi::$statics[__CLASS__]['componentNameId'][$item['component_id']];
541
6a488035 542 $config = CRM_Core_Config::singleton();
3d31a5c6 543 if (is_array($config->enableComponents) && in_array($componentName, $config->enableComponents)) {
6a488035
TO
544 // continue with process
545 }
546 else {
547 return FALSE;
548 }
549 }
550
551 // the following is imitating drupal 6 code in includes/menu.inc
552 if (empty($item['access_callback']) ||
553 is_numeric($item['access_callback'])
554 ) {
555 return (boolean ) $item['access_callback'];
556 }
557
558 // check whether the following Ajax requests submitted the right key
559 // FIXME: this should be integrated into ACLs proper
560 if (CRM_Utils_Array::value('page_type', $item) == 3) {
561 if (!CRM_Core_Key::validate($_REQUEST['key'], $item['path'])) {
562 return FALSE;
563 }
564 }
565
566 // check if callback is for checkMenu, if so optimize it
567 if (is_array($item['access_callback']) &&
568 $item['access_callback'][0] == 'CRM_Core_Permission' &&
569 $item['access_callback'][1] == 'checkMenu'
570 ) {
571 $op = CRM_Utils_Array::value(1, $item['access_arguments'], 'and');
572 return self::checkMenu($item['access_arguments'][0], $op);
573 }
574 else {
575 return call_user_func_array($item['access_callback'], $item['access_arguments']);
576 }
577 }
578
a0ee3941
EM
579 /**
580 * @param bool $all
9476d8d1 581 * Include disabled components
221b21b4 582 * @param bool $descriptions
9476d8d1 583 * Whether to return descriptions
a0ee3941
EM
584 *
585 * @return array
586 */
9476d8d1 587 public static function basicPermissions($all = FALSE, $descriptions = FALSE) {
be2fb01f 588 $cacheKey = implode('-', [$all, $descriptions]);
9476d8d1
CW
589 if (empty(Civi::$statics[__CLASS__][__FUNCTION__][$cacheKey])) {
590 Civi::$statics[__CLASS__][__FUNCTION__][$cacheKey] = self::assembleBasicPermissions($all, $descriptions);
221b21b4 591 }
9476d8d1 592 return Civi::$statics[__CLASS__][__FUNCTION__][$cacheKey];
221b21b4
AH
593 }
594
595 /**
596 * @param bool $all
597 * @param bool $descriptions
598 * whether to return descriptions
599 *
600 * @return array
601 */
602 public static function assembleBasicPermissions($all = FALSE, $descriptions = FALSE) {
603 $config = CRM_Core_Config::singleton();
604 $prefix = ts('CiviCRM') . ': ';
605 $permissions = self::getCorePermissions($descriptions);
606
607 if (self::isMultisiteEnabled()) {
be2fb01f 608 $permissions['administer Multiple Organizations'] = [$prefix . ts('administer Multiple Organizations')];
221b21b4
AH
609 }
610
8c282315 611 if (!$descriptions) {
612 foreach ($permissions as $name => $attr) {
613 $permissions[$name] = array_shift($attr);
614 }
615 }
221b21b4
AH
616 if (!$all) {
617 $components = CRM_Core_Component::getEnabledComponents();
618 }
619 else {
620 $components = CRM_Core_Component::getComponents();
621 }
622
623 foreach ($components as $comp) {
624 $perm = $comp->getPermissions(FALSE, $descriptions);
625 if ($perm) {
626 $info = $comp->getInfo();
2666861c
KL
627 foreach ($perm as $p => $attr) {
628
629 if (!is_array($attr)) {
be2fb01f 630 $attr = [$attr];
2666861c
KL
631 }
632
633 $attr[0] = $info['translatedName'] . ': ' . $attr[0];
634
635 if ($descriptions) {
221b21b4
AH
636 $permissions[$p] = $attr;
637 }
2666861c
KL
638 else {
639 $permissions[$p] = $attr[0];
6a488035
TO
640 }
641 }
642 }
6a488035
TO
643 }
644
221b21b4 645 // Add any permissions defined in hook_civicrm_permission implementations.
f1db52b9 646 $module_permissions = $config->userPermissionClass->getAllModulePermissions($descriptions);
221b21b4 647 $permissions = array_merge($permissions, $module_permissions);
cb2f7dd1 648 CRM_Financial_BAO_FinancialType::permissionedFinancialTypes($permissions, $descriptions);
6a488035
TO
649 return $permissions;
650 }
651
a0ee3941
EM
652 /**
653 * @return array
654 */
00be9182 655 public static function getAnonymousPermissionsWarnings() {
be2fb01f 656 static $permissions = [];
81bb85ea 657 if (empty($permissions)) {
be2fb01f 658 $permissions = [
21dfd5f5 659 'administer CiviCRM',
be2fb01f 660 ];
81bb85ea
AC
661 $components = CRM_Core_Component::getComponents();
662 foreach ($components as $comp) {
663 if (!method_exists($comp, 'getAnonymousPermissionWarnings')) {
664 continue;
665 }
666 $permissions = array_merge($permissions, $comp->getAnonymousPermissionWarnings());
667 }
668 }
669 return $permissions;
670 }
671
a0ee3941
EM
672 /**
673 * @param $anonymous_perms
674 *
675 * @return array
676 */
00be9182 677 public static function validateForPermissionWarnings($anonymous_perms) {
81bb85ea
AC
678 return array_intersect($anonymous_perms, self::getAnonymousPermissionsWarnings());
679 }
680
a0ee3941 681 /**
6793d6a9 682 * Get core permissions.
221b21b4 683 *
a0ee3941
EM
684 * @return array
685 */
8c282315 686 public static function getCorePermissions() {
6a488035 687 $prefix = ts('CiviCRM') . ': ';
be2fb01f
CW
688 $permissions = [
689 'add contacts' => [
221b21b4
AH
690 $prefix . ts('add contacts'),
691 ts('Create a new contact record in CiviCRM'),
be2fb01f
CW
692 ],
693 'view all contacts' => [
221b21b4
AH
694 $prefix . ts('view all contacts'),
695 ts('View ANY CONTACT in the CiviCRM database, export contact info and perform activities such as Send Email, Phone Call, etc.'),
be2fb01f
CW
696 ],
697 'edit all contacts' => [
221b21b4
AH
698 $prefix . ts('edit all contacts'),
699 ts('View, Edit and Delete ANY CONTACT in the CiviCRM database; Create and edit relationships, tags and other info about the contacts'),
be2fb01f
CW
700 ],
701 'view my contact' => [
221b21b4 702 $prefix . ts('view my contact'),
be2fb01f
CW
703 ],
704 'edit my contact' => [
221b21b4 705 $prefix . ts('edit my contact'),
be2fb01f
CW
706 ],
707 'delete contacts' => [
221b21b4 708 $prefix . ts('delete contacts'),
be2fb01f
CW
709 ],
710 'access deleted contacts' => [
221b21b4
AH
711 $prefix . ts('access deleted contacts'),
712 ts('Access contacts in the trash'),
be2fb01f
CW
713 ],
714 'import contacts' => [
221b21b4
AH
715 $prefix . ts('import contacts'),
716 ts('Import contacts and activities'),
be2fb01f
CW
717 ],
718 'import SQL datasource' => [
40bc3c68
TO
719 $prefix . ts('import SQL datasource'),
720 ts('When importing, consume data directly from a SQL datasource'),
be2fb01f
CW
721 ],
722 'edit groups' => [
221b21b4
AH
723 $prefix . ts('edit groups'),
724 ts('Create new groups, edit group settings (e.g. group name, visibility...), delete groups'),
be2fb01f
CW
725 ],
726 'administer CiviCRM' => [
221b21b4
AH
727 $prefix . ts('administer CiviCRM'),
728 ts('Perform all tasks in the Administer CiviCRM control panel and Import Contacts'),
be2fb01f
CW
729 ],
730 'skip IDS check' => [
221b21b4 731 $prefix . ts('skip IDS check'),
d9a37cbc 732 ts('Warning: Give to trusted roles only; this permission has security implications. IDS system is bypassed for users with this permission. Prevents false errors for admin users.'),
be2fb01f
CW
733 ],
734 'access uploaded files' => [
221b21b4
AH
735 $prefix . ts('access uploaded files'),
736 ts('View / download files including images and photos'),
be2fb01f
CW
737 ],
738 'profile listings and forms' => [
221b21b4 739 $prefix . ts('profile listings and forms'),
d9a37cbc 740 ts('Warning: Give to trusted roles only; this permission has privacy implications. Add/edit data in online forms and access public searchable directories.'),
be2fb01f
CW
741 ],
742 'profile listings' => [
221b21b4 743 $prefix . ts('profile listings'),
d9a37cbc 744 ts('Warning: Give to trusted roles only; this permission has privacy implications. Access public searchable directories.'),
be2fb01f
CW
745 ],
746 'profile create' => [
221b21b4 747 $prefix . ts('profile create'),
d9a37cbc 748 ts('Add data in a profile form.'),
be2fb01f
CW
749 ],
750 'profile edit' => [
221b21b4 751 $prefix . ts('profile edit'),
d9a37cbc 752 ts('Edit data in a profile form.'),
be2fb01f
CW
753 ],
754 'profile view' => [
221b21b4 755 $prefix . ts('profile view'),
d9a37cbc 756 ts('View data in a profile.'),
be2fb01f
CW
757 ],
758 'access all custom data' => [
221b21b4
AH
759 $prefix . ts('access all custom data'),
760 ts('View all custom fields regardless of ACL rules'),
be2fb01f
CW
761 ],
762 'view all activities' => [
221b21b4
AH
763 $prefix . ts('view all activities'),
764 ts('View all activities (for visible contacts)'),
be2fb01f
CW
765 ],
766 'delete activities' => [
221b21b4 767 $prefix . ts('Delete activities'),
be2fb01f
CW
768 ],
769 'edit inbound email basic information' => [
ee90a98c
CR
770 $prefix . ts('edit inbound email basic information'),
771 ts('Edit all inbound email activities (for visible contacts) basic information. Content editing not allowed.'),
be2fb01f
CW
772 ],
773 'edit inbound email basic information and content' => [
ee90a98c
CR
774 $prefix . ts('edit inbound email basic information and content'),
775 ts('Edit all inbound email activities (for visible contacts) basic information and content.'),
be2fb01f
CW
776 ],
777 'access CiviCRM' => [
d9a37cbc
H
778 $prefix . ts('access CiviCRM backend and API'),
779 ts('Master control for access to the main CiviCRM backend and API. Give to trusted roles only.'),
be2fb01f
CW
780 ],
781 'access Contact Dashboard' => [
221b21b4
AH
782 $prefix . ts('access Contact Dashboard'),
783 ts('View Contact Dashboard (for themselves and visible contacts)'),
be2fb01f
CW
784 ],
785 'translate CiviCRM' => [
221b21b4
AH
786 $prefix . ts('translate CiviCRM'),
787 ts('Allow User to enable multilingual'),
be2fb01f
CW
788 ],
789 'manage tags' => [
eaaaef83
I
790 $prefix . ts('manage tags'),
791 ts('Create and rename tags'),
be2fb01f
CW
792 ],
793 'administer reserved groups' => [
221b21b4
AH
794 $prefix . ts('administer reserved groups'),
795 ts('Edit and disable Reserved Groups (Needs Edit Groups)'),
be2fb01f
CW
796 ],
797 'administer Tagsets' => [
221b21b4 798 $prefix . ts('administer Tagsets'),
be2fb01f
CW
799 ],
800 'administer reserved tags' => [
221b21b4 801 $prefix . ts('administer reserved tags'),
be2fb01f
CW
802 ],
803 'administer dedupe rules' => [
221b21b4
AH
804 $prefix . ts('administer dedupe rules'),
805 ts('Create and edit rules, change the supervised and unsupervised rules'),
be2fb01f
CW
806 ],
807 'merge duplicate contacts' => [
221b21b4
AH
808 $prefix . ts('merge duplicate contacts'),
809 ts('Delete Contacts must also be granted in order for this to work.'),
be2fb01f
CW
810 ],
811 'force merge duplicate contacts' => [
fd630ef9 812 $prefix . ts('force merge duplicate contacts'),
813 ts('Delete Contacts must also be granted in order for this to work.'),
be2fb01f
CW
814 ],
815 'view debug output' => [
221b21b4
AH
816 $prefix . ts('view debug output'),
817 ts('View results of debug and backtrace'),
be2fb01f 818 ],
02d451ab 819
be2fb01f 820 'view all notes' => [
221b21b4
AH
821 $prefix . ts('view all notes'),
822 ts("View notes (for visible contacts) even if they're marked admin only"),
be2fb01f
CW
823 ],
824 'add contact notes' => [
088101a4
O
825 $prefix . ts('add contact notes'),
826 ts("Create notes for contacts"),
be2fb01f
CW
827 ],
828 'access AJAX API' => [
221b21b4
AH
829 $prefix . ts('access AJAX API'),
830 ts('Allow API access even if Access CiviCRM is not granted'),
be2fb01f
CW
831 ],
832 'access contact reference fields' => [
221b21b4
AH
833 $prefix . ts('access contact reference fields'),
834 ts('Allow entering data into contact reference fields'),
be2fb01f
CW
835 ],
836 'create manual batch' => [
221b21b4
AH
837 $prefix . ts('create manual batch'),
838 ts('Create an accounting batch (with Access to CiviContribute and View Own/All Manual Batches)'),
be2fb01f
CW
839 ],
840 'edit own manual batches' => [
221b21b4
AH
841 $prefix . ts('edit own manual batches'),
842 ts('Edit accounting batches created by user'),
be2fb01f
CW
843 ],
844 'edit all manual batches' => [
221b21b4
AH
845 $prefix . ts('edit all manual batches'),
846 ts('Edit all accounting batches'),
be2fb01f
CW
847 ],
848 'close own manual batches' => [
47a98aef 849 $prefix . ts('close own manual batches'),
5ad18cc2 850 ts('Close accounting batches created by user (with Access to CiviContribute)'),
be2fb01f
CW
851 ],
852 'close all manual batches' => [
47a98aef 853 $prefix . ts('close all manual batches'),
5ad18cc2 854 ts('Close all accounting batches (with Access to CiviContribute)'),
be2fb01f
CW
855 ],
856 'reopen own manual batches' => [
47a98aef 857 $prefix . ts('reopen own manual batches'),
5ad18cc2 858 ts('Reopen accounting batches created by user (with Access to CiviContribute)'),
be2fb01f
CW
859 ],
860 'reopen all manual batches' => [
47a98aef 861 $prefix . ts('reopen all manual batches'),
5ad18cc2 862 ts('Reopen all accounting batches (with Access to CiviContribute)'),
be2fb01f
CW
863 ],
864 'view own manual batches' => [
221b21b4
AH
865 $prefix . ts('view own manual batches'),
866 ts('View accounting batches created by user (with Access to CiviContribute)'),
be2fb01f
CW
867 ],
868 'view all manual batches' => [
221b21b4
AH
869 $prefix . ts('view all manual batches'),
870 ts('View all accounting batches (with Access to CiviContribute)'),
be2fb01f
CW
871 ],
872 'delete own manual batches' => [
221b21b4
AH
873 $prefix . ts('delete own manual batches'),
874 ts('Delete accounting batches created by user'),
be2fb01f
CW
875 ],
876 'delete all manual batches' => [
221b21b4
AH
877 $prefix . ts('delete all manual batches'),
878 ts('Delete all accounting batches'),
be2fb01f
CW
879 ],
880 'export own manual batches' => [
221b21b4
AH
881 $prefix . ts('export own manual batches'),
882 ts('Export accounting batches created by user'),
be2fb01f
CW
883 ],
884 'export all manual batches' => [
221b21b4
AH
885 $prefix . ts('export all manual batches'),
886 ts('Export all accounting batches'),
be2fb01f
CW
887 ],
888 'administer payment processors' => [
221b21b4
AH
889 $prefix . ts('administer payment processors'),
890 ts('Add, Update, or Disable Payment Processors'),
be2fb01f
CW
891 ],
892 'edit message templates' => [
221b21b4 893 $prefix . ts('edit message templates'),
be2fb01f
CW
894 ],
895 'edit system workflow message templates' => [
40a732a9 896 $prefix . ts('edit system workflow message templates'),
be2fb01f
CW
897 ],
898 'edit user-driven message templates' => [
40a732a9 899 $prefix . ts('edit user-driven message templates'),
be2fb01f
CW
900 ],
901 'view my invoices' => [
a664e7b3 902 $prefix . ts('view my invoices'),
dc6b437a 903 ts('Allow users to view/ download their own invoices'),
be2fb01f
CW
904 ],
905 'edit api keys' => [
d4463076
TO
906 $prefix . ts('edit api keys'),
907 ts('Edit API keys'),
be2fb01f
CW
908 ],
909 'edit own api keys' => [
d4463076
TO
910 $prefix . ts('edit own api keys'),
911 ts('Edit user\'s own API keys'),
be2fb01f
CW
912 ],
913 'send SMS' => [
63483feb
MM
914 $prefix . ts('send SMS'),
915 ts('Send an SMS'),
be2fb01f
CW
916 ],
917 ];
6a488035
TO
918
919 return $permissions;
920 }
921
bf9a7c0f
ES
922 /**
923 * For each entity provides an array of permissions required for each action
924 *
925 * The action is the array key, possible values:
926 * * create: applies to create (with no id in params)
927 * * update: applies to update, setvalue, create (with id in params)
928 * * get: applies to getcount, getsingle, getvalue and other gets
929 * * delete: applies to delete, replace
930 * * meta: applies to getfields, getoptions, getspec
931 * * default: catch-all for anything not declared
932 *
933 * Note: some APIs declare other actions as well
934 *
935 * Permissions should use arrays for AND and arrays of arrays for OR
1a5a2ade 936 * @see CRM_Core_Permission::check
bf9a7c0f
ES
937 *
938 * @return array of permissions
939 */
940 public static function getEntityActionPermissions() {
be2fb01f 941 $permissions = [];
bf9a7c0f
ES
942 // These are the default permissions - if any entity does not declare permissions for a given action,
943 // (or the entity does not declare permissions at all) - then the action will be used from here
be2fb01f 944 $permissions['default'] = [
bf9a7c0f 945 // applies to getfields, getoptions, etc.
be2fb01f 946 'meta' => ['access CiviCRM'],
bf9a7c0f
ES
947 // catch-all, applies to create, get, delete, etc.
948 // If an entity declares it's own 'default' action it will override this one
be2fb01f
CW
949 'default' => ['administer CiviCRM'],
950 ];
bf9a7c0f
ES
951
952 // Note: Additional permissions in DynamicFKAuthorization
be2fb01f
CW
953 $permissions['attachment'] = [
954 'default' => [
955 ['access CiviCRM', 'access AJAX API'],
956 ],
957 ];
bf9a7c0f
ES
958
959 // Contact permissions
be2fb01f
CW
960 $permissions['contact'] = [
961 'create' => [
bf9a7c0f
ES
962 'access CiviCRM',
963 'add contacts',
be2fb01f
CW
964 ],
965 'delete' => [
bf9a7c0f
ES
966 'access CiviCRM',
967 'delete contacts',
be2fb01f 968 ],
bf9a7c0f 969 // managed by query object
be2fb01f 970 'get' => [],
bf9a7c0f 971 // managed by _civicrm_api3_check_edit_permissions
be2fb01f
CW
972 'update' => [],
973 'getquick' => [
974 ['access CiviCRM', 'access AJAX API'],
975 ],
976 'duplicatecheck' => [
f257308b 977 'access CiviCRM',
be2fb01f
CW
978 ],
979 ];
bf9a7c0f
ES
980
981 // CRM-16963 - Permissions for country.
be2fb01f
CW
982 $permissions['country'] = [
983 'get' => [
bf9a7c0f 984 'access CiviCRM',
be2fb01f
CW
985 ],
986 'default' => [
bf9a7c0f 987 'administer CiviCRM',
be2fb01f
CW
988 ],
989 ];
bf9a7c0f
ES
990
991 // Contact-related data permissions.
be2fb01f 992 $permissions['address'] = [
bf9a7c0f
ES
993 // get is managed by BAO::addSelectWhereClause
994 // create/delete are managed by _civicrm_api3_check_edit_permissions
be2fb01f
CW
995 'default' => [],
996 ];
bf9a7c0f
ES
997 $permissions['email'] = $permissions['address'];
998 $permissions['phone'] = $permissions['address'];
999 $permissions['website'] = $permissions['address'];
1000 $permissions['im'] = $permissions['address'];
1001 $permissions['open_i_d'] = $permissions['address'];
1002
1003 // Also managed by ACLs - CRM-19448
be2fb01f 1004 $permissions['entity_tag'] = ['default' => []];
bf9a7c0f
ES
1005 $permissions['note'] = $permissions['entity_tag'];
1006
1007 // Allow non-admins to get and create tags to support tagset widget
1008 // Delete is still reserved for admins
be2fb01f
CW
1009 $permissions['tag'] = [
1010 'get' => ['access CiviCRM'],
1011 'create' => ['access CiviCRM'],
1012 'update' => ['access CiviCRM'],
1013 ];
bf9a7c0f
ES
1014
1015 //relationship permissions
be2fb01f 1016 $permissions['relationship'] = [
bf9a7c0f 1017 // get is managed by BAO::addSelectWhereClause
be2fb01f
CW
1018 'get' => [],
1019 'delete' => [
bf9a7c0f
ES
1020 'access CiviCRM',
1021 'edit all contacts',
be2fb01f
CW
1022 ],
1023 'default' => [
bf9a7c0f
ES
1024 'access CiviCRM',
1025 'edit all contacts',
be2fb01f
CW
1026 ],
1027 ];
bf9a7c0f
ES
1028
1029 // CRM-17741 - Permissions for RelationshipType.
be2fb01f
CW
1030 $permissions['relationship_type'] = [
1031 'get' => [
bf9a7c0f 1032 'access CiviCRM',
be2fb01f
CW
1033 ],
1034 'default' => [
bf9a7c0f 1035 'administer CiviCRM',
be2fb01f
CW
1036 ],
1037 ];
bf9a7c0f
ES
1038
1039 // Activity permissions
be2fb01f
CW
1040 $permissions['activity'] = [
1041 'delete' => [
bf9a7c0f
ES
1042 'access CiviCRM',
1043 'delete activities',
be2fb01f
CW
1044 ],
1045 'get' => [
bf9a7c0f
ES
1046 'access CiviCRM',
1047 // Note that view all activities is also required within the api
1048 // if the id is not passed in. Where the id is passed in the activity
1049 // specific check functions are used and tested.
be2fb01f
CW
1050 ],
1051 'default' => [
bf9a7c0f
ES
1052 'access CiviCRM',
1053 'view all activities',
be2fb01f
CW
1054 ],
1055 ];
cdacd6ab 1056 $permissions['activity_contact'] = $permissions['activity'];
bf9a7c0f
ES
1057
1058 // Case permissions
be2fb01f
CW
1059 $permissions['case'] = [
1060 'create' => [
bf9a7c0f
ES
1061 'access CiviCRM',
1062 'add cases',
be2fb01f
CW
1063 ],
1064 'delete' => [
bf9a7c0f
ES
1065 'access CiviCRM',
1066 'delete in CiviCase',
be2fb01f
CW
1067 ],
1068 'restore' => [
8572e6de 1069 'administer CiviCase',
be2fb01f
CW
1070 ],
1071 'merge' => [
a6bc7218 1072 'administer CiviCase',
be2fb01f
CW
1073 ],
1074 'default' => [
bf9a7c0f 1075 // At minimum the user needs one of the following. Finer-grained access is controlled by CRM_Case_BAO_Case::addSelectWhereClause
be2fb01f
CW
1076 ['access my cases and activities', 'access all cases and activities'],
1077 ],
1078 ];
bf9a7c0f
ES
1079 $permissions['case_contact'] = $permissions['case'];
1080
be2fb01f
CW
1081 $permissions['case_type'] = [
1082 'default' => ['administer CiviCase'],
1083 'get' => [
bf9a7c0f 1084 // nested array = OR
be2fb01f
CW
1085 ['access my cases and activities', 'access all cases and activities'],
1086 ],
1087 ];
bf9a7c0f
ES
1088
1089 // Campaign permissions
be2fb01f
CW
1090 $permissions['campaign'] = [
1091 'get' => ['access CiviCRM'],
1092 'default' => [
bf9a7c0f 1093 // nested array = OR
be2fb01f
CW
1094 ['administer CiviCampaign', 'manage campaign'],
1095 ],
1096 ];
bf9a7c0f
ES
1097 $permissions['survey'] = $permissions['campaign'];
1098
1099 // Financial permissions
be2fb01f
CW
1100 $permissions['contribution'] = [
1101 'get' => [
bf9a7c0f
ES
1102 'access CiviCRM',
1103 'access CiviContribute',
be2fb01f
CW
1104 ],
1105 'delete' => [
bf9a7c0f
ES
1106 'access CiviCRM',
1107 'access CiviContribute',
1108 'delete in CiviContribute',
be2fb01f
CW
1109 ],
1110 'completetransaction' => [
bf9a7c0f 1111 'edit contributions',
be2fb01f
CW
1112 ],
1113 'default' => [
bf9a7c0f
ES
1114 'access CiviCRM',
1115 'access CiviContribute',
1116 'edit contributions',
be2fb01f
CW
1117 ],
1118 ];
bf9a7c0f
ES
1119 $permissions['line_item'] = $permissions['contribution'];
1120
1121 // Payment permissions
be2fb01f
CW
1122 $permissions['payment'] = [
1123 'get' => [
bf9a7c0f
ES
1124 'access CiviCRM',
1125 'access CiviContribute',
be2fb01f
CW
1126 ],
1127 'delete' => [
bf9a7c0f
ES
1128 'access CiviCRM',
1129 'access CiviContribute',
1130 'delete in CiviContribute',
be2fb01f
CW
1131 ],
1132 'cancel' => [
bf9a7c0f
ES
1133 'access CiviCRM',
1134 'access CiviContribute',
1135 'edit contributions',
be2fb01f
CW
1136 ],
1137 'create' => [
bf9a7c0f
ES
1138 'access CiviCRM',
1139 'access CiviContribute',
1140 'edit contributions',
be2fb01f
CW
1141 ],
1142 'default' => [
bf9a7c0f
ES
1143 'access CiviCRM',
1144 'access CiviContribute',
1145 'edit contributions',
be2fb01f
CW
1146 ],
1147 ];
e4124a88 1148 $permissions['contribution_recur'] = $permissions['payment'];
bf9a7c0f
ES
1149
1150 // Custom field permissions
be2fb01f
CW
1151 $permissions['custom_field'] = [
1152 'default' => [
bf9a7c0f
ES
1153 'administer CiviCRM',
1154 'access all custom data',
be2fb01f
CW
1155 ],
1156 ];
bf9a7c0f
ES
1157 $permissions['custom_group'] = $permissions['custom_field'];
1158
1159 // Event permissions
be2fb01f
CW
1160 $permissions['event'] = [
1161 'create' => [
bf9a7c0f
ES
1162 'access CiviCRM',
1163 'access CiviEvent',
1164 'edit all events',
be2fb01f
CW
1165 ],
1166 'delete' => [
bf9a7c0f
ES
1167 'access CiviCRM',
1168 'access CiviEvent',
1169 'delete in CiviEvent',
be2fb01f
CW
1170 ],
1171 'get' => [
bf9a7c0f
ES
1172 'access CiviCRM',
1173 'access CiviEvent',
1174 'view event info',
be2fb01f
CW
1175 ],
1176 'update' => [
bf9a7c0f
ES
1177 'access CiviCRM',
1178 'access CiviEvent',
1179 'edit all events',
be2fb01f
CW
1180 ],
1181 ];
bf9a7c0f
ES
1182 // Loc block is only used for events
1183 $permissions['loc_block'] = $permissions['event'];
1184
be2fb01f
CW
1185 $permissions['state_province'] = [
1186 'get' => [
fc2d1728 1187 'access CiviCRM',
be2fb01f
CW
1188 ],
1189 ];
fc2d1728 1190
bf9a7c0f 1191 // Price sets are shared by several components, user needs access to at least one of them
be2fb01f
CW
1192 $permissions['price_set'] = [
1193 'default' => [
1194 ['access CiviEvent', 'access CiviContribute', 'access CiviMember'],
1195 ],
1196 'get' => [
1197 ['access CiviCRM', 'view event info', 'make online contributions'],
1198 ],
1199 ];
bf9a7c0f
ES
1200
1201 // File permissions
be2fb01f
CW
1202 $permissions['file'] = [
1203 'default' => [
bf9a7c0f
ES
1204 'access CiviCRM',
1205 'access uploaded files',
be2fb01f
CW
1206 ],
1207 ];
bf9a7c0f
ES
1208 $permissions['files_by_entity'] = $permissions['file'];
1209
1210 // Group permissions
be2fb01f
CW
1211 $permissions['group'] = [
1212 'get' => [
bf9a7c0f 1213 'access CiviCRM',
be2fb01f
CW
1214 ],
1215 'default' => [
bf9a7c0f
ES
1216 'access CiviCRM',
1217 'edit groups',
be2fb01f
CW
1218 ],
1219 ];
bf9a7c0f
ES
1220
1221 $permissions['group_nesting'] = $permissions['group'];
1222 $permissions['group_organization'] = $permissions['group'];
1223
1224 //Group Contact permission
be2fb01f
CW
1225 $permissions['group_contact'] = [
1226 'get' => [
bf9a7c0f 1227 'access CiviCRM',
be2fb01f
CW
1228 ],
1229 'default' => [
bf9a7c0f
ES
1230 'access CiviCRM',
1231 'edit all contacts',
be2fb01f
CW
1232 ],
1233 ];
bf9a7c0f
ES
1234
1235 // CiviMail Permissions
be2fb01f 1236 $civiMailBasePerms = [
bf9a7c0f
ES
1237 // To get/preview/update, one must have least one of these perms:
1238 // Mailing API implementations enforce nuances of create/approve/schedule permissions.
1239 'access CiviMail',
1240 'create mailings',
1241 'schedule mailings',
1242 'approve mailings',
be2fb01f
CW
1243 ];
1244 $permissions['mailing'] = [
1245 'get' => [
bf9a7c0f
ES
1246 'access CiviCRM',
1247 $civiMailBasePerms,
be2fb01f
CW
1248 ],
1249 'delete' => [
bf9a7c0f
ES
1250 'access CiviCRM',
1251 $civiMailBasePerms,
1252 'delete in CiviMail',
be2fb01f
CW
1253 ],
1254 'submit' => [
bf9a7c0f 1255 'access CiviCRM',
be2fb01f
CW
1256 ['access CiviMail', 'schedule mailings'],
1257 ],
1258 'default' => [
bf9a7c0f
ES
1259 'access CiviCRM',
1260 $civiMailBasePerms,
be2fb01f
CW
1261 ],
1262 ];
bf9a7c0f
ES
1263 $permissions['mailing_group'] = $permissions['mailing'];
1264 $permissions['mailing_job'] = $permissions['mailing'];
1265 $permissions['mailing_recipients'] = $permissions['mailing'];
1266
be2fb01f
CW
1267 $permissions['mailing_a_b'] = [
1268 'get' => [
bf9a7c0f
ES
1269 'access CiviCRM',
1270 'access CiviMail',
be2fb01f
CW
1271 ],
1272 'delete' => [
bf9a7c0f
ES
1273 'access CiviCRM',
1274 'access CiviMail',
1275 'delete in CiviMail',
be2fb01f
CW
1276 ],
1277 'submit' => [
bf9a7c0f 1278 'access CiviCRM',
be2fb01f
CW
1279 ['access CiviMail', 'schedule mailings'],
1280 ],
1281 'default' => [
bf9a7c0f
ES
1282 'access CiviCRM',
1283 'access CiviMail',
be2fb01f
CW
1284 ],
1285 ];
bf9a7c0f
ES
1286
1287 // Membership permissions
be2fb01f
CW
1288 $permissions['membership'] = [
1289 'get' => [
bf9a7c0f
ES
1290 'access CiviCRM',
1291 'access CiviMember',
be2fb01f
CW
1292 ],
1293 'delete' => [
bf9a7c0f
ES
1294 'access CiviCRM',
1295 'access CiviMember',
1296 'delete in CiviMember',
be2fb01f
CW
1297 ],
1298 'default' => [
bf9a7c0f
ES
1299 'access CiviCRM',
1300 'access CiviMember',
1301 'edit memberships',
be2fb01f
CW
1302 ],
1303 ];
bf9a7c0f
ES
1304 $permissions['membership_status'] = $permissions['membership'];
1305 $permissions['membership_type'] = $permissions['membership'];
be2fb01f
CW
1306 $permissions['membership_payment'] = [
1307 'create' => [
bf9a7c0f
ES
1308 'access CiviCRM',
1309 'access CiviMember',
1310 'edit memberships',
1311 'access CiviContribute',
1312 'edit contributions',
be2fb01f
CW
1313 ],
1314 'delete' => [
bf9a7c0f
ES
1315 'access CiviCRM',
1316 'access CiviMember',
1317 'delete in CiviMember',
1318 'access CiviContribute',
1319 'delete in CiviContribute',
be2fb01f
CW
1320 ],
1321 'get' => [
bf9a7c0f
ES
1322 'access CiviCRM',
1323 'access CiviMember',
1324 'access CiviContribute',
be2fb01f
CW
1325 ],
1326 'update' => [
bf9a7c0f
ES
1327 'access CiviCRM',
1328 'access CiviMember',
1329 'edit memberships',
1330 'access CiviContribute',
1331 'edit contributions',
be2fb01f
CW
1332 ],
1333 ];
bf9a7c0f
ES
1334
1335 // Participant permissions
be2fb01f
CW
1336 $permissions['participant'] = [
1337 'create' => [
bf9a7c0f
ES
1338 'access CiviCRM',
1339 'access CiviEvent',
1340 'register for events',
be2fb01f
CW
1341 ],
1342 'delete' => [
bf9a7c0f
ES
1343 'access CiviCRM',
1344 'access CiviEvent',
1345 'edit event participants',
be2fb01f
CW
1346 ],
1347 'get' => [
bf9a7c0f
ES
1348 'access CiviCRM',
1349 'access CiviEvent',
1350 'view event participants',
be2fb01f
CW
1351 ],
1352 'update' => [
bf9a7c0f
ES
1353 'access CiviCRM',
1354 'access CiviEvent',
1355 'edit event participants',
be2fb01f
CW
1356 ],
1357 ];
1358 $permissions['participant_payment'] = [
1359 'create' => [
bf9a7c0f
ES
1360 'access CiviCRM',
1361 'access CiviEvent',
1362 'register for events',
1363 'access CiviContribute',
1364 'edit contributions',
be2fb01f
CW
1365 ],
1366 'delete' => [
bf9a7c0f
ES
1367 'access CiviCRM',
1368 'access CiviEvent',
1369 'edit event participants',
1370 'access CiviContribute',
1371 'delete in CiviContribute',
be2fb01f
CW
1372 ],
1373 'get' => [
bf9a7c0f
ES
1374 'access CiviCRM',
1375 'access CiviEvent',
1376 'view event participants',
1377 'access CiviContribute',
be2fb01f
CW
1378 ],
1379 'update' => [
bf9a7c0f
ES
1380 'access CiviCRM',
1381 'access CiviEvent',
1382 'edit event participants',
1383 'access CiviContribute',
1384 'edit contributions',
be2fb01f
CW
1385 ],
1386 ];
bf9a7c0f
ES
1387
1388 // Pledge permissions
be2fb01f
CW
1389 $permissions['pledge'] = [
1390 'create' => [
bf9a7c0f
ES
1391 'access CiviCRM',
1392 'access CiviPledge',
1393 'edit pledges',
be2fb01f
CW
1394 ],
1395 'delete' => [
bf9a7c0f
ES
1396 'access CiviCRM',
1397 'access CiviPledge',
1398 'delete in CiviPledge',
be2fb01f
CW
1399 ],
1400 'get' => [
bf9a7c0f
ES
1401 'access CiviCRM',
1402 'access CiviPledge',
be2fb01f
CW
1403 ],
1404 'update' => [
bf9a7c0f
ES
1405 'access CiviCRM',
1406 'access CiviPledge',
1407 'edit pledges',
be2fb01f
CW
1408 ],
1409 ];
bf9a7c0f
ES
1410
1411 //CRM-16777: Disable schedule reminder for user that have 'edit all events' and 'administer CiviCRM' permission.
be2fb01f
CW
1412 $permissions['action_schedule'] = [
1413 'update' => [
1414 [
bf9a7c0f
ES
1415 'access CiviCRM',
1416 'edit all events',
be2fb01f
CW
1417 ],
1418 ],
1419 ];
bf9a7c0f 1420
be2fb01f
CW
1421 $permissions['pledge_payment'] = [
1422 'create' => [
bf9a7c0f
ES
1423 'access CiviCRM',
1424 'access CiviPledge',
1425 'edit pledges',
1426 'access CiviContribute',
1427 'edit contributions',
be2fb01f
CW
1428 ],
1429 'delete' => [
bf9a7c0f
ES
1430 'access CiviCRM',
1431 'access CiviPledge',
1432 'delete in CiviPledge',
1433 'access CiviContribute',
1434 'delete in CiviContribute',
be2fb01f
CW
1435 ],
1436 'get' => [
bf9a7c0f
ES
1437 'access CiviCRM',
1438 'access CiviPledge',
1439 'access CiviContribute',
be2fb01f
CW
1440 ],
1441 'update' => [
bf9a7c0f
ES
1442 'access CiviCRM',
1443 'access CiviPledge',
1444 'edit pledges',
1445 'access CiviContribute',
1446 'edit contributions',
be2fb01f
CW
1447 ],
1448 ];
bf9a7c0f
ES
1449
1450 // Profile permissions
be2fb01f 1451 $permissions['profile'] = [
518fa0ee
SL
1452 // the profile will take care of this
1453 'get' => [],
be2fb01f 1454 ];
bf9a7c0f 1455
be2fb01f
CW
1456 $permissions['uf_group'] = [
1457 'create' => [
bf9a7c0f 1458 'access CiviCRM',
be2fb01f 1459 [
bf9a7c0f
ES
1460 'administer CiviCRM',
1461 'manage event profiles',
be2fb01f
CW
1462 ],
1463 ],
1464 'get' => [
bf9a7c0f 1465 'access CiviCRM',
be2fb01f
CW
1466 ],
1467 'update' => [
bf9a7c0f 1468 'access CiviCRM',
be2fb01f 1469 [
bf9a7c0f
ES
1470 'administer CiviCRM',
1471 'manage event profiles',
be2fb01f
CW
1472 ],
1473 ],
1474 ];
bf9a7c0f 1475 $permissions['uf_field'] = $permissions['uf_join'] = $permissions['uf_group'];
be2fb01f 1476 $permissions['uf_field']['delete'] = [
bf9a7c0f 1477 'access CiviCRM',
be2fb01f 1478 [
bf9a7c0f
ES
1479 'administer CiviCRM',
1480 'manage event profiles',
be2fb01f
CW
1481 ],
1482 ];
bf9a7c0f
ES
1483 $permissions['option_value'] = $permissions['uf_group'];
1484 $permissions['option_group'] = $permissions['option_value'];
1485
be2fb01f
CW
1486 $permissions['custom_value'] = [
1487 'gettree' => ['access CiviCRM'],
1488 ];
f26fa703 1489
be2fb01f
CW
1490 $permissions['message_template'] = [
1491 'get' => ['access CiviCRM'],
1492 'create' => [['edit message templates', 'edit user-driven message templates', 'edit system workflow message templates']],
1493 'update' => [['edit message templates', 'edit user-driven message templates', 'edit system workflow message templates']],
1494 ];
4341efe4
JV
1495
1496 $permissions['report_template']['update'] = 'save Report Criteria';
1497 $permissions['report_template']['create'] = 'save Report Criteria';
bf9a7c0f
ES
1498 return $permissions;
1499 }
1500
1501 /**
1502 * Translate an unknown action to a canonical form.
1503 *
1504 * @param string $action
1505 *
1506 * @return string
1507 * the standardised action name
1508 */
1509 public static function getGenericAction($action) {
1510 $snippet = substr($action, 0, 3);
1511 if ($action == 'replace' || $snippet == 'del') {
1512 // 'Replace' is a combination of get+create+update+delete; however, the permissions
1513 // on each of those will be tested separately at runtime. This is just a sniff-test
1514 // based on the heuristic that 'delete' tends to be the most closely guarded
1515 // of the necessary permissions.
1516 $action = 'delete';
1517 }
1518 elseif ($action == 'setvalue' || $snippet == 'upd') {
1519 $action = 'update';
1520 }
1521 elseif ($action == 'getfields' || $action == 'getfield' || $action == 'getspec' || $action == 'getoptions') {
1522 $action = 'meta';
1523 }
1524 elseif ($snippet == 'get') {
1525 $action = 'get';
1526 }
1527 return $action;
1528 }
1529
6a488035 1530 /**
d09edf64 1531 * Validate user permission across.
6a488035
TO
1532 * edit or view or with supportable acls.
1533 *
acb1052e
WA
1534 * @return bool
1535 */
00be9182 1536 public static function giveMeAllACLs() {
6a488035
TO
1537 if (CRM_Core_Permission::check('view all contacts') ||
1538 CRM_Core_Permission::check('edit all contacts')
1539 ) {
1540 return TRUE;
1541 }
1542
1543 $session = CRM_Core_Session::singleton();
1544 $contactID = $session->get('userID');
1545
6a488035
TO
1546 //check for acl.
1547 $aclPermission = self::getPermission();
be2fb01f 1548 if (in_array($aclPermission, [
6a488035 1549 CRM_Core_Permission::EDIT,
41f314b6 1550 CRM_Core_Permission::VIEW,
be2fb01f 1551 ])
41f314b6 1552 ) {
6a488035
TO
1553 return TRUE;
1554 }
1555
1556 // run acl where hook and see if the user is supplying an ACL clause
1557 // that is not false
be2fb01f 1558 $tables = $whereTables = [];
6a488035
TO
1559 $where = NULL;
1560
1561 CRM_Utils_Hook::aclWhereClause(CRM_Core_Permission::VIEW,
1562 $tables, $whereTables,
1563 $contactID, $where
1564 );
1565 return empty($whereTables) ? FALSE : TRUE;
1566 }
1567
1568 /**
100fef9d 1569 * Get component name from given permission.
6a488035 1570 *
41f314b6 1571 * @param string $permission
6a488035 1572 *
76e7a76c
CW
1573 * @return null|string
1574 * the name of component.
6a488035 1575 */
00be9182 1576 public static function getComponentName($permission) {
6a488035
TO
1577 $componentName = NULL;
1578 $permission = trim($permission);
1579 if (empty($permission)) {
1580 return $componentName;
1581 }
1582
be2fb01f 1583 static $allCompPermissions = [];
6a488035
TO
1584 if (empty($allCompPermissions)) {
1585 $components = CRM_Core_Component::getComponents();
1586 foreach ($components as $name => $comp) {
33777e4a
PJ
1587 //get all permissions of each components unconditionally
1588 $allCompPermissions[$name] = $comp->getPermissions(TRUE);
6a488035
TO
1589 }
1590 }
1591
1592 if (is_array($allCompPermissions)) {
1593 foreach ($allCompPermissions as $name => $permissions) {
b9021475 1594 if (array_key_exists($permission, $permissions)) {
6a488035
TO
1595 $componentName = $name;
1596 break;
1597 }
1598 }
1599 }
1600
1601 return $componentName;
1602 }
1603
1604 /**
d09edf64 1605 * Get all the contact emails for users that have a specific permission.
6a488035 1606 *
6a0b768e
TO
1607 * @param string $permissionName
1608 * Name of the permission we are interested in.
6a488035 1609 *
a6c01b45
CW
1610 * @return string
1611 * a comma separated list of email addresses
6a488035
TO
1612 */
1613 public static function permissionEmails($permissionName) {
1614 $config = CRM_Core_Config::singleton();
41f314b6 1615 return $config->userPermissionClass->permissionEmails($permissionName);
6a488035
TO
1616 }
1617
1618 /**
d09edf64 1619 * Get all the contact emails for users that have a specific role.
6a488035 1620 *
6a0b768e
TO
1621 * @param string $roleName
1622 * Name of the role we are interested in.
6a488035 1623 *
a6c01b45
CW
1624 * @return string
1625 * a comma separated list of email addresses
6a488035
TO
1626 */
1627 public static function roleEmails($roleName) {
1628 $config = CRM_Core_Config::singleton();
41f314b6 1629 return $config->userRoleClass->roleEmails($roleName);
6a488035
TO
1630 }
1631
a0ee3941
EM
1632 /**
1633 * @return bool
1634 */
00be9182 1635 public static function isMultisiteEnabled() {
aaffa79f 1636 return Civi::settings()->get('is_enabled') ? TRUE : FALSE;
6a488035 1637 }
96025800 1638
dc6b437a
GC
1639 /**
1640 * Verify if the user has permission to get the invoice.
1641 *
1642 * @return bool
1643 * TRUE if the user has download all invoices permission or download my
1644 * invoices permission and the invoice author is the current user.
1645 */
1646 public static function checkDownloadInvoice() {
4cfd4f45 1647 $cid = CRM_Core_Session::getLoggedInContactID();
dc6b437a
GC
1648 if (CRM_Core_Permission::check('access CiviContribute') ||
1649 (CRM_Core_Permission::check('view my invoices') && $_GET['cid'] == $cid)
1650 ) {
1651 return TRUE;
1652 }
1653 return FALSE;
1654 }
1655
6a488035 1656}