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