3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This is the basic permission class wrapper
37 class CRM_Core_Permission
{
40 * Static strings used to compose permissions.
45 const EDIT_GROUPS
= 'edit contacts in ', VIEW_GROUPS
= 'view contacts in ';
48 * The various type of permissions.
52 const EDIT
= 1, VIEW
= 2, DELETE
= 3, CREATE
= 4, SEARCH
= 5, ALL
= 6, ADMIN
= 7;
55 * A placeholder permission which always fails.
57 const ALWAYS_DENY_PERMISSION
= "*always deny*";
60 * A placeholder permission which always fails.
62 const ALWAYS_ALLOW_PERMISSION
= "*always allow*";
65 * Various authentication sources.
69 const AUTH_SRC_UNKNOWN
= 0, AUTH_SRC_CHECKSUM
= 1, AUTH_SRC_SITEKEY
= 2, AUTH_SRC_LOGIN
= 4;
72 * Get the current permission of this user.
75 * the permission of the user (edit or view or null)
77 public static function getPermission() {
78 $config = CRM_Core_Config
::singleton();
79 return $config->userPermissionClass
->getPermission();
83 * Given a permission string or array, check for access requirements
84 * @param string|array $permissions
85 * The permission to check as an array or string -see examples.
87 * @param int $contactId
88 * Contact id to check permissions for. Defaults to current logged-in user.
92 * Must have 'access CiviCRM'
93 * (string) 'access CiviCRM'
96 * Ex 2 Must have 'access CiviCRM' and 'access Ajax API'
97 * array('access CiviCRM', 'access Ajax API')
99 * Ex 3 Must have 'access CiviCRM' or 'access Ajax API'
101 * array('access CiviCRM', 'access Ajax API'),
104 * Ex 4 Must have 'access CiviCRM' or 'access Ajax API' AND 'access CiviEvent'
106 * array('access CiviCRM', 'access Ajax API'),
107 * 'access CiviEvent',
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')
118 * true if yes, else false
120 public static function check($permissions, $contactId = NULL) {
121 $permissions = (array) $permissions;
124 $userId = CRM_Core_BAO_UFMatch
::getUFId($contactId);
127 /** @var CRM_Core_Permission_Temp $tempPerm */
128 $tempPerm = CRM_Core_Config
::singleton()->userPermissionTemp
;
130 foreach ($permissions as $permission) {
131 if (is_array($permission)) {
132 foreach ($permission as $orPerm) {
133 if (self
::check($orPerm, $contactId)) {
134 //one of our 'or' permissions has succeeded - stop checking this permission
138 //none of our our conditions was met
142 // This is an individual permission
143 $granted = CRM_Core_Config
::singleton()->userPermissionClass
->check($permission, $userId);
144 // Call the permission_check hook to permit dynamic escalation (CRM-19256)
145 CRM_Utils_Hook
::permission_check($permission, $granted, $contactId);
148 && !($tempPerm && $tempPerm->check($permission))
150 //one of our 'and' conditions has not been met
159 * Determine if any one of the permissions strings applies to current user.
161 * @param array $perms
164 public static function checkAnyPerm($perms) {
165 foreach ($perms as $perm) {
166 if (CRM_Core_Permission
::check($perm)) {
174 * Given a group/role array, check for access requirements
176 * @param array $array
177 * The group/role to check.
180 * true if yes, else false
182 public static function checkGroupRole($array) {
183 $config = CRM_Core_Config
::singleton();
184 return $config->userPermissionClass
->checkGroupRole($array);
188 * Get the permissioned where clause for the user.
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.
198 * the group where clause for this user
200 public static function getPermissionedStaticGroupClause($type, &$tables, &$whereTables) {
201 $config = CRM_Core_Config
::singleton();
202 return $config->userPermissionClass
->getPermissionedStaticGroupClause($type, $tables, $whereTables);
206 * Get all groups from database, filtered by permissions
209 * @param string $groupType
210 * Type of group(Access/Mailing).
211 * @param bool $excludeHidden
212 * exclude hidden groups.
216 * array reference of all groups.
218 public static function group($groupType, $excludeHidden = TRUE) {
219 $config = CRM_Core_Config
::singleton();
220 return $config->userPermissionClass
->group($groupType, $excludeHidden);
226 public static function customGroupAdmin() {
229 // check if user has all powerful permission
230 // or administer civicrm permission (CRM-1905)
231 if (self
::check('access all custom data')) {
236 self
::check('administer Multiple Organizations') &&
237 self
::isMultisiteEnabled()
242 if (self
::check('administer CiviCRM')) {
255 public static function customGroup($type = CRM_Core_Permission
::VIEW
, $reset = FALSE) {
256 $customGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_CustomField', 'custom_group_id',
257 ['fresh' => $reset]);
260 // check if user has all powerful permission
261 // or administer civicrm permission (CRM-1905)
262 if (self
::customGroupAdmin()) {
263 $defaultGroups = array_keys($customGroups);
266 return CRM_ACL_API
::group($type, NULL, 'civicrm_custom_group', $customGroups, $defaultGroups);
271 * @param null $prefix
276 public static function customGroupClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL, $reset = FALSE) {
277 if (self
::customGroupAdmin()) {
281 $groups = self
::customGroup($type, $reset);
282 if (empty($groups)) {
286 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
296 public static function ufGroupValid($gid, $type = CRM_Core_Permission
::VIEW
) {
301 $groups = self
::ufGroup($type);
302 return !empty($groups) && in_array($gid, $groups) ?
TRUE : FALSE;
310 public static function ufGroup($type = CRM_Core_Permission
::VIEW
) {
311 $ufGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_UFField', 'uf_group_id');
313 $allGroups = array_keys($ufGroups);
315 // check if user has all powerful permission
316 if (self
::check('profile listings and forms')) {
321 case CRM_Core_Permission
::VIEW
:
322 if (self
::check('profile view')) {
327 case CRM_Core_Permission
::CREATE
:
328 if (self
::check('profile create')) {
333 case CRM_Core_Permission
::EDIT
:
334 if (self
::check('profile edit')) {
339 case CRM_Core_Permission
::SEARCH
:
340 if (self
::check('profile listings')) {
346 return CRM_ACL_API
::group($type, NULL, 'civicrm_uf_group', $ufGroups);
351 * @param null $prefix
352 * @param bool $returnUFGroupIds
354 * @return array|string
356 public static function ufGroupClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL, $returnUFGroupIds = FALSE) {
357 $groups = self
::ufGroup($type);
358 if ($returnUFGroupIds) {
361 elseif (empty($groups)) {
365 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
371 * @param int $eventID
372 * @param string $context
376 public static function event($type = CRM_Core_Permission
::VIEW
, $eventID = NULL, $context = '') {
377 if (!empty($context)) {
378 if (CRM_Core_Permission
::check($context)) {
382 $events = CRM_Event_PseudoConstant
::event(NULL, TRUE);
385 // check if user has all powerful permission
386 if (self
::check('register for events')) {
387 $includeEvents = array_keys($events);
390 if ($type == CRM_Core_Permission
::VIEW
&&
391 self
::check('view event info')
393 $includeEvents = array_keys($events);
396 $permissionedEvents = CRM_ACL_API
::group($type, NULL, 'civicrm_event', $events, $includeEvents);
398 return $permissionedEvents;
400 if (!empty($permissionedEvents)) {
401 return array_search($eventID, $permissionedEvents) === FALSE ?
NULL : $eventID;
408 * @param null $prefix
412 public static function eventClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL) {
413 $events = self
::event($type);
414 if (empty($events)) {
418 return "{$prefix}id IN ( " . implode(',', $events) . ' ) ';
423 * Checks that component is enabled and optionally that user has basic perm.
425 * @param string $module
426 * Specifies the name of the CiviCRM component.
427 * @param bool $checkPermission
428 * Check not only that module is enabled, but that user has necessary
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".
436 * Access to specified $module is granted.
438 public static function access($module, $checkPermission = TRUE, $requireAllCasesPermOnCiviCase = FALSE) {
439 $config = CRM_Core_Config
::singleton();
441 if (!in_array($module, $config->enableComponents
)) {
445 if ($checkPermission) {
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);
453 return CRM_Core_Permission
::check("administer $module");
456 return CRM_Core_Permission
::check("access $module");
464 * Check permissions for delete and edit actions.
466 * @param string $module
469 * Action to be check across component.
474 public static function checkActionPermission($module, $action) {
475 //check delete related permissions.
476 if ($action & CRM_Core_Action
::DELETE
) {
477 $permissionName = "delete in $module";
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',
489 $permissionName = CRM_Utils_Array
::value($module, $editPermissions);
492 if ($module == 'CiviCase' && !$permissionName) {
493 return CRM_Case_BAO_Case
::accessCiviCase();
496 //check for permission.
497 return CRM_Core_Permission
::check($permissionName);
507 public static function checkMenu(&$args, $op = 'and') {
508 if (!is_array($args)) {
511 foreach ($args as $str) {
512 $res = CRM_Core_Permission
::check($str);
513 if ($op == 'or' && $res) {
516 elseif ($op == 'and' && !$res) {
520 return ($op == 'or') ?
FALSE : TRUE;
529 public static function checkMenuItem(&$item) {
530 if (!array_key_exists('access_callback', $item)) {
531 CRM_Core_Error
::backtrace();
532 CRM_Core_Error
::fatal();
535 // if component_id is present, ensure it is enabled
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());
540 $componentName = Civi
::$statics[__CLASS__
]['componentNameId'][$item['component_id']];
542 $config = CRM_Core_Config
::singleton();
543 if (is_array($config->enableComponents
) && in_array($componentName, $config->enableComponents
)) {
544 // continue with process
551 // the following is imitating drupal 6 code in includes/menu.inc
552 if (empty($item['access_callback']) ||
553 is_numeric($item['access_callback'])
555 return (boolean
) $item['access_callback'];
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'])) {
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'
571 $op = CRM_Utils_Array
::value(1, $item['access_arguments'], 'and');
572 return self
::checkMenu($item['access_arguments'][0], $op);
575 return call_user_func_array($item['access_callback'], $item['access_arguments']);
581 * Include disabled components
582 * @param bool $descriptions
583 * Whether to return descriptions
587 public static function basicPermissions($all = FALSE, $descriptions = FALSE) {
588 $cacheKey = implode('-', [$all, $descriptions]);
589 if (empty(Civi
::$statics[__CLASS__
][__FUNCTION__
][$cacheKey])) {
590 Civi
::$statics[__CLASS__
][__FUNCTION__
][$cacheKey] = self
::assembleBasicPermissions($all, $descriptions);
592 return Civi
::$statics[__CLASS__
][__FUNCTION__
][$cacheKey];
597 * @param bool $descriptions
598 * whether to return descriptions
602 public static function assembleBasicPermissions($all = FALSE, $descriptions = FALSE) {
603 $config = CRM_Core_Config
::singleton();
604 $prefix = ts('CiviCRM') . ': ';
605 $permissions = self
::getCorePermissions($descriptions);
607 if (self
::isMultisiteEnabled()) {
608 $permissions['administer Multiple Organizations'] = [$prefix . ts('administer Multiple Organizations')];
611 if (!$descriptions) {
612 foreach ($permissions as $name => $attr) {
613 $permissions[$name] = array_shift($attr);
617 $components = CRM_Core_Component
::getEnabledComponents();
620 $components = CRM_Core_Component
::getComponents();
623 foreach ($components as $comp) {
624 $perm = $comp->getPermissions(FALSE, $descriptions);
626 $info = $comp->getInfo();
627 foreach ($perm as $p => $attr) {
629 if (!is_array($attr)) {
633 $attr[0] = $info['translatedName'] . ': ' . $attr[0];
636 $permissions[$p] = $attr;
639 $permissions[$p] = $attr[0];
645 // Add any permissions defined in hook_civicrm_permission implementations.
646 $module_permissions = $config->userPermissionClass
->getAllModulePermissions($descriptions);
647 $permissions = array_merge($permissions, $module_permissions);
648 CRM_Financial_BAO_FinancialType
::permissionedFinancialTypes($permissions, $descriptions);
655 public static function getAnonymousPermissionsWarnings() {
656 static $permissions = [];
657 if (empty($permissions)) {
659 'administer CiviCRM',
661 $components = CRM_Core_Component
::getComponents();
662 foreach ($components as $comp) {
663 if (!method_exists($comp, 'getAnonymousPermissionWarnings')) {
666 $permissions = array_merge($permissions, $comp->getAnonymousPermissionWarnings());
673 * @param $anonymous_perms
677 public static function validateForPermissionWarnings($anonymous_perms) {
678 return array_intersect($anonymous_perms, self
::getAnonymousPermissionsWarnings());
682 * Get core permissions.
686 public static function getCorePermissions() {
687 $prefix = ts('CiviCRM') . ': ';
690 $prefix . ts('add contacts'),
691 ts('Create a new contact record in CiviCRM'),
693 'view all contacts' => [
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.'),
697 'edit all contacts' => [
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'),
701 'view my contact' => [
702 $prefix . ts('view my contact'),
704 'edit my contact' => [
705 $prefix . ts('edit my contact'),
707 'delete contacts' => [
708 $prefix . ts('delete contacts'),
710 'access deleted contacts' => [
711 $prefix . ts('access deleted contacts'),
712 ts('Access contacts in the trash'),
714 'import contacts' => [
715 $prefix . ts('import contacts'),
716 ts('Import contacts and activities'),
718 'import SQL datasource' => [
719 $prefix . ts('import SQL datasource'),
720 ts('When importing, consume data directly from a SQL datasource'),
723 $prefix . ts('edit groups'),
724 ts('Create new groups, edit group settings (e.g. group name, visibility...), delete groups'),
726 'administer CiviCRM' => [
727 $prefix . ts('administer CiviCRM'),
728 ts('Perform all tasks in the Administer CiviCRM control panel and Import Contacts'),
730 'skip IDS check' => [
731 $prefix . ts('skip IDS check'),
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.'),
734 'access uploaded files' => [
735 $prefix . ts('access uploaded files'),
736 ts('View / download files including images and photos'),
738 'profile listings and forms' => [
739 $prefix . ts('profile listings and forms'),
740 ts('Warning: Give to trusted roles only; this permission has privacy implications. Add/edit data in online forms and access public searchable directories.'),
742 'profile listings' => [
743 $prefix . ts('profile listings'),
744 ts('Warning: Give to trusted roles only; this permission has privacy implications. Access public searchable directories.'),
746 'profile create' => [
747 $prefix . ts('profile create'),
748 ts('Add data in a profile form.'),
751 $prefix . ts('profile edit'),
752 ts('Edit data in a profile form.'),
755 $prefix . ts('profile view'),
756 ts('View data in a profile.'),
758 'access all custom data' => [
759 $prefix . ts('access all custom data'),
760 ts('View all custom fields regardless of ACL rules'),
762 'view all activities' => [
763 $prefix . ts('view all activities'),
764 ts('View all activities (for visible contacts)'),
766 'delete activities' => [
767 $prefix . ts('Delete activities'),
769 'edit inbound email basic information' => [
770 $prefix . ts('edit inbound email basic information'),
771 ts('Edit all inbound email activities (for visible contacts) basic information. Content editing not allowed.'),
773 'edit inbound email basic information and content' => [
774 $prefix . ts('edit inbound email basic information and content'),
775 ts('Edit all inbound email activities (for visible contacts) basic information and content.'),
777 'access CiviCRM' => [
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.'),
781 'access Contact Dashboard' => [
782 $prefix . ts('access Contact Dashboard'),
783 ts('View Contact Dashboard (for themselves and visible contacts)'),
785 'translate CiviCRM' => [
786 $prefix . ts('translate CiviCRM'),
787 ts('Allow User to enable multilingual'),
790 $prefix . ts('manage tags'),
791 ts('Create and rename tags'),
793 'administer reserved groups' => [
794 $prefix . ts('administer reserved groups'),
795 ts('Edit and disable Reserved Groups (Needs Edit Groups)'),
797 'administer Tagsets' => [
798 $prefix . ts('administer Tagsets'),
800 'administer reserved tags' => [
801 $prefix . ts('administer reserved tags'),
803 'administer dedupe rules' => [
804 $prefix . ts('administer dedupe rules'),
805 ts('Create and edit rules, change the supervised and unsupervised rules'),
807 'merge duplicate contacts' => [
808 $prefix . ts('merge duplicate contacts'),
809 ts('Delete Contacts must also be granted in order for this to work.'),
811 'force merge duplicate contacts' => [
812 $prefix . ts('force merge duplicate contacts'),
813 ts('Delete Contacts must also be granted in order for this to work.'),
815 'view debug output' => [
816 $prefix . ts('view debug output'),
817 ts('View results of debug and backtrace'),
820 'view all notes' => [
821 $prefix . ts('view all notes'),
822 ts("View notes (for visible contacts) even if they're marked admin only"),
824 'add contact notes' => [
825 $prefix . ts('add contact notes'),
826 ts("Create notes for contacts"),
828 'access AJAX API' => [
829 $prefix . ts('access AJAX API'),
830 ts('Allow API access even if Access CiviCRM is not granted'),
832 'access contact reference fields' => [
833 $prefix . ts('access contact reference fields'),
834 ts('Allow entering data into contact reference fields'),
836 'create manual batch' => [
837 $prefix . ts('create manual batch'),
838 ts('Create an accounting batch (with Access to CiviContribute and View Own/All Manual Batches)'),
840 'edit own manual batches' => [
841 $prefix . ts('edit own manual batches'),
842 ts('Edit accounting batches created by user'),
844 'edit all manual batches' => [
845 $prefix . ts('edit all manual batches'),
846 ts('Edit all accounting batches'),
848 'close own manual batches' => [
849 $prefix . ts('close own manual batches'),
850 ts('Close accounting batches created by user (with Access to CiviContribute)'),
852 'close all manual batches' => [
853 $prefix . ts('close all manual batches'),
854 ts('Close all accounting batches (with Access to CiviContribute)'),
856 'reopen own manual batches' => [
857 $prefix . ts('reopen own manual batches'),
858 ts('Reopen accounting batches created by user (with Access to CiviContribute)'),
860 'reopen all manual batches' => [
861 $prefix . ts('reopen all manual batches'),
862 ts('Reopen all accounting batches (with Access to CiviContribute)'),
864 'view own manual batches' => [
865 $prefix . ts('view own manual batches'),
866 ts('View accounting batches created by user (with Access to CiviContribute)'),
868 'view all manual batches' => [
869 $prefix . ts('view all manual batches'),
870 ts('View all accounting batches (with Access to CiviContribute)'),
872 'delete own manual batches' => [
873 $prefix . ts('delete own manual batches'),
874 ts('Delete accounting batches created by user'),
876 'delete all manual batches' => [
877 $prefix . ts('delete all manual batches'),
878 ts('Delete all accounting batches'),
880 'export own manual batches' => [
881 $prefix . ts('export own manual batches'),
882 ts('Export accounting batches created by user'),
884 'export all manual batches' => [
885 $prefix . ts('export all manual batches'),
886 ts('Export all accounting batches'),
888 'administer payment processors' => [
889 $prefix . ts('administer payment processors'),
890 ts('Add, Update, or Disable Payment Processors'),
892 'edit message templates' => [
893 $prefix . ts('edit message templates'),
895 'edit system workflow message templates' => [
896 $prefix . ts('edit system workflow message templates'),
898 'edit user-driven message templates' => [
899 $prefix . ts('edit user-driven message templates'),
901 'view my invoices' => [
902 $prefix . ts('view my invoices'),
903 ts('Allow users to view/ download their own invoices'),
906 $prefix . ts('edit api keys'),
909 'edit own api keys' => [
910 $prefix . ts('edit own api keys'),
911 ts('Edit user\'s own API keys'),
914 $prefix . ts('send SMS'),
923 * For each entity provides an array of permissions required for each action
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
933 * Note: some APIs declare other actions as well
935 * Permissions should use arrays for AND and arrays of arrays for OR
936 * @see CRM_Core_Permission::check
938 * @return array of permissions
940 public static function getEntityActionPermissions() {
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
944 $permissions['default'] = [
945 // applies to getfields, getoptions, etc.
946 'meta' => ['access CiviCRM'],
947 // catch-all, applies to create, get, delete, etc.
948 // If an entity declares it's own 'default' action it will override this one
949 'default' => ['administer CiviCRM'],
952 // Note: Additional permissions in DynamicFKAuthorization
953 $permissions['attachment'] = [
955 ['access CiviCRM', 'access AJAX API'],
959 // Contact permissions
960 $permissions['contact'] = [
969 // managed by query object
971 // managed by _civicrm_api3_check_edit_permissions
974 ['access CiviCRM', 'access AJAX API'],
976 'duplicatecheck' => [
981 // CRM-16963 - Permissions for country.
982 $permissions['country'] = [
987 'administer CiviCRM',
991 // Contact-related data permissions.
992 $permissions['address'] = [
993 // get is managed by BAO::addSelectWhereClause
994 // create/delete are managed by _civicrm_api3_check_edit_permissions
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'];
1003 // Also managed by ACLs - CRM-19448
1004 $permissions['entity_tag'] = ['default' => []];
1005 $permissions['note'] = $permissions['entity_tag'];
1007 // Allow non-admins to get and create tags to support tagset widget
1008 // Delete is still reserved for admins
1009 $permissions['tag'] = [
1010 'get' => ['access CiviCRM'],
1011 'create' => ['access CiviCRM'],
1012 'update' => ['access CiviCRM'],
1015 //relationship permissions
1016 $permissions['relationship'] = [
1017 // get is managed by BAO::addSelectWhereClause
1021 'edit all contacts',
1025 'edit all contacts',
1029 // CRM-17741 - Permissions for RelationshipType.
1030 $permissions['relationship_type'] = [
1035 'administer CiviCRM',
1039 // Activity permissions
1040 $permissions['activity'] = [
1043 'delete activities',
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.
1053 'view all activities',
1056 $permissions['activity_contact'] = $permissions['activity'];
1059 $permissions['case'] = [
1066 'delete in CiviCase',
1069 'administer CiviCase',
1072 'administer CiviCase',
1075 // At minimum the user needs one of the following. Finer-grained access is controlled by CRM_Case_BAO_Case::addSelectWhereClause
1076 ['access my cases and activities', 'access all cases and activities'],
1079 $permissions['case_contact'] = $permissions['case'];
1081 $permissions['case_type'] = [
1082 'default' => ['administer CiviCase'],
1084 // nested array = OR
1085 ['access my cases and activities', 'access all cases and activities'],
1089 // Campaign permissions
1090 $permissions['campaign'] = [
1091 'get' => ['access CiviCRM'],
1093 // nested array = OR
1094 ['administer CiviCampaign', 'manage campaign'],
1097 $permissions['survey'] = $permissions['campaign'];
1099 // Financial permissions
1100 $permissions['contribution'] = [
1103 'access CiviContribute',
1107 'access CiviContribute',
1108 'delete in CiviContribute',
1110 'completetransaction' => [
1111 'edit contributions',
1115 'access CiviContribute',
1116 'edit contributions',
1119 $permissions['line_item'] = $permissions['contribution'];
1121 // Payment permissions
1122 $permissions['payment'] = [
1125 'access CiviContribute',
1129 'access CiviContribute',
1130 'delete in CiviContribute',
1134 'access CiviContribute',
1135 'edit contributions',
1139 'access CiviContribute',
1140 'edit contributions',
1144 'access CiviContribute',
1145 'edit contributions',
1148 $permissions['contribution_recur'] = $permissions['payment'];
1150 // Custom field permissions
1151 $permissions['custom_field'] = [
1153 'administer CiviCRM',
1154 'access all custom data',
1157 $permissions['custom_group'] = $permissions['custom_field'];
1159 // Event permissions
1160 $permissions['event'] = [
1169 'delete in CiviEvent',
1182 // Loc block is only used for events
1183 $permissions['loc_block'] = $permissions['event'];
1185 $permissions['state_province'] = [
1191 // Price sets are shared by several components, user needs access to at least one of them
1192 $permissions['price_set'] = [
1194 ['access CiviEvent', 'access CiviContribute', 'access CiviMember'],
1197 ['access CiviCRM', 'view event info', 'make online contributions'],
1202 $permissions['file'] = [
1205 'access uploaded files',
1208 $permissions['files_by_entity'] = $permissions['file'];
1210 // Group permissions
1211 $permissions['group'] = [
1221 $permissions['group_nesting'] = $permissions['group'];
1222 $permissions['group_organization'] = $permissions['group'];
1224 //Group Contact permission
1225 $permissions['group_contact'] = [
1231 'edit all contacts',
1235 // CiviMail Permissions
1236 $civiMailBasePerms = [
1237 // To get/preview/update, one must have least one of these perms:
1238 // Mailing API implementations enforce nuances of create/approve/schedule permissions.
1241 'schedule mailings',
1244 $permissions['mailing'] = [
1252 'delete in CiviMail',
1256 ['access CiviMail', 'schedule mailings'],
1263 $permissions['mailing_group'] = $permissions['mailing'];
1264 $permissions['mailing_job'] = $permissions['mailing'];
1265 $permissions['mailing_recipients'] = $permissions['mailing'];
1267 $permissions['mailing_a_b'] = [
1275 'delete in CiviMail',
1279 ['access CiviMail', 'schedule mailings'],
1287 // Membership permissions
1288 $permissions['membership'] = [
1291 'access CiviMember',
1295 'access CiviMember',
1296 'delete in CiviMember',
1300 'access CiviMember',
1304 $permissions['membership_status'] = $permissions['membership'];
1305 $permissions['membership_type'] = $permissions['membership'];
1306 $permissions['membership_payment'] = [
1309 'access CiviMember',
1311 'access CiviContribute',
1312 'edit contributions',
1316 'access CiviMember',
1317 'delete in CiviMember',
1318 'access CiviContribute',
1319 'delete in CiviContribute',
1323 'access CiviMember',
1324 'access CiviContribute',
1328 'access CiviMember',
1330 'access CiviContribute',
1331 'edit contributions',
1335 // Participant permissions
1336 $permissions['participant'] = [
1340 'register for events',
1345 'edit event participants',
1350 'view event participants',
1355 'edit event participants',
1358 $permissions['participant_payment'] = [
1362 'register for events',
1363 'access CiviContribute',
1364 'edit contributions',
1369 'edit event participants',
1370 'access CiviContribute',
1371 'delete in CiviContribute',
1376 'view event participants',
1377 'access CiviContribute',
1382 'edit event participants',
1383 'access CiviContribute',
1384 'edit contributions',
1388 // Pledge permissions
1389 $permissions['pledge'] = [
1392 'access CiviPledge',
1397 'access CiviPledge',
1398 'delete in CiviPledge',
1402 'access CiviPledge',
1406 'access CiviPledge',
1411 //CRM-16777: Disable schedule reminder for user that have 'edit all events' and 'administer CiviCRM' permission.
1412 $permissions['action_schedule'] = [
1421 $permissions['pledge_payment'] = [
1424 'access CiviPledge',
1426 'access CiviContribute',
1427 'edit contributions',
1431 'access CiviPledge',
1432 'delete in CiviPledge',
1433 'access CiviContribute',
1434 'delete in CiviContribute',
1438 'access CiviPledge',
1439 'access CiviContribute',
1443 'access CiviPledge',
1445 'access CiviContribute',
1446 'edit contributions',
1450 // Profile permissions
1451 $permissions['profile'] = [
1452 // the profile will take care of this
1456 $permissions['uf_group'] = [
1460 'administer CiviCRM',
1461 'manage event profiles',
1470 'administer CiviCRM',
1471 'manage event profiles',
1475 $permissions['uf_field'] = $permissions['uf_join'] = $permissions['uf_group'];
1476 $permissions['uf_field']['delete'] = [
1479 'administer CiviCRM',
1480 'manage event profiles',
1483 $permissions['option_value'] = $permissions['uf_group'];
1484 $permissions['option_group'] = $permissions['option_value'];
1486 $permissions['custom_value'] = [
1487 'gettree' => ['access CiviCRM'],
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']],
1496 $permissions['report_template']['update'] = 'save Report Criteria';
1497 $permissions['report_template']['create'] = 'save Report Criteria';
1498 return $permissions;
1502 * Translate an unknown action to a canonical form.
1504 * @param string $action
1507 * the standardised action name
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.
1518 elseif ($action == 'setvalue' ||
$snippet == 'upd') {
1521 elseif ($action == 'getfields' ||
$action == 'getfield' ||
$action == 'getspec' ||
$action == 'getoptions') {
1524 elseif ($snippet == 'get') {
1531 * Validate user permission across.
1532 * edit or view or with supportable acls.
1536 public static function giveMeAllACLs() {
1537 if (CRM_Core_Permission
::check('view all contacts') ||
1538 CRM_Core_Permission
::check('edit all contacts')
1543 $session = CRM_Core_Session
::singleton();
1544 $contactID = $session->get('userID');
1547 $aclPermission = self
::getPermission();
1548 if (in_array($aclPermission, [
1549 CRM_Core_Permission
::EDIT
,
1550 CRM_Core_Permission
::VIEW
,
1556 // run acl where hook and see if the user is supplying an ACL clause
1557 // that is not false
1558 $tables = $whereTables = [];
1561 CRM_Utils_Hook
::aclWhereClause(CRM_Core_Permission
::VIEW
,
1562 $tables, $whereTables,
1565 return empty($whereTables) ?
FALSE : TRUE;
1569 * Get component name from given permission.
1571 * @param string $permission
1573 * @return null|string
1574 * the name of component.
1576 public static function getComponentName($permission) {
1577 $componentName = NULL;
1578 $permission = trim($permission);
1579 if (empty($permission)) {
1580 return $componentName;
1583 static $allCompPermissions = [];
1584 if (empty($allCompPermissions)) {
1585 $components = CRM_Core_Component
::getComponents();
1586 foreach ($components as $name => $comp) {
1587 //get all permissions of each components unconditionally
1588 $allCompPermissions[$name] = $comp->getPermissions(TRUE);
1592 if (is_array($allCompPermissions)) {
1593 foreach ($allCompPermissions as $name => $permissions) {
1594 if (array_key_exists($permission, $permissions)) {
1595 $componentName = $name;
1601 return $componentName;
1605 * Get all the contact emails for users that have a specific permission.
1607 * @param string $permissionName
1608 * Name of the permission we are interested in.
1611 * a comma separated list of email addresses
1613 public static function permissionEmails($permissionName) {
1614 $config = CRM_Core_Config
::singleton();
1615 return $config->userPermissionClass
->permissionEmails($permissionName);
1619 * Get all the contact emails for users that have a specific role.
1621 * @param string $roleName
1622 * Name of the role we are interested in.
1625 * a comma separated list of email addresses
1627 public static function roleEmails($roleName) {
1628 $config = CRM_Core_Config
::singleton();
1629 return $config->userRoleClass
->roleEmails($roleName);
1635 public static function isMultisiteEnabled() {
1636 return Civi
::settings()->get('is_enabled') ?
TRUE : FALSE;
1640 * Verify if the user has permission to get the invoice.
1643 * TRUE if the user has download all invoices permission or download my
1644 * invoices permission and the invoice author is the current user.
1646 public static function checkDownloadInvoice() {
1647 $cid = CRM_Core_Session
::getLoggedInContactID();
1648 if (CRM_Core_Permission
::check('access CiviContribute') ||
1649 (CRM_Core_Permission
::check('view my invoices') && $_GET['cid'] == $cid)