3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
37 * This is the basic permission class wrapper
39 class CRM_Core_Permission
{
42 * Static strings used to compose permissions.
47 const EDIT_GROUPS
= 'edit contacts in ', VIEW_GROUPS
= 'view contacts in ';
50 * The various type of permissions.
54 const EDIT
= 1, VIEW
= 2, DELETE
= 3, CREATE
= 4, SEARCH
= 5, ALL
= 6, ADMIN
= 7;
57 * A placeholder permission which always fails.
59 const ALWAYS_DENY_PERMISSION
= "*always deny*";
62 * A placeholder permission which always fails.
64 const ALWAYS_ALLOW_PERMISSION
= "*always allow*";
67 * Various authentication sources.
71 const AUTH_SRC_UNKNOWN
= 0, AUTH_SRC_CHECKSUM
= 1, AUTH_SRC_SITEKEY
= 2, AUTH_SRC_LOGIN
= 4;
74 * Get the current permission of this user.
77 * the permission of the user (edit or view or null)
79 public static function getPermission() {
80 $config = CRM_Core_Config
::singleton();
81 return $config->userPermissionClass
->getPermission();
85 * Given a permission string or array, check for access requirements
86 * @param mixed $permissions
87 * The permission to check as an array or string -see examples.
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) {
121 $permissions = (array) $permissions;
123 $tempPerm = CRM_Core_Config
::singleton()->userPermissionTemp
;
125 foreach ($permissions as $permission) {
126 if (is_array($permission)) {
127 foreach ($permission as $orPerm) {
128 if (self
::check($orPerm)) {
129 //one of our 'or' permissions has succeeded - stop checking this permission
133 //none of our our conditions was met
138 !CRM_Core_Config
::singleton()->userPermissionClass
->check($permission)
139 && !($tempPerm && $tempPerm->check($permission))
141 //one of our 'and' conditions has not been met
150 * Determine if any one of the permissions strings applies to current user.
152 * @param array $perms
155 public static function checkAnyPerm($perms) {
156 foreach ($perms as $perm) {
157 if (CRM_Core_Permission
::check($perm)) {
165 * Given a group/role array, check for access requirements
167 * @param array $array
168 * The group/role to check.
171 * true if yes, else false
173 public static function checkGroupRole($array) {
174 $config = CRM_Core_Config
::singleton();
175 return $config->userPermissionClass
->checkGroupRole($array);
179 * Get the permissioned where clause for the user.
182 * The type of permission needed.
183 * @param array $tables
184 * (reference ) add the tables that are needed for the select clause.
185 * @param array $whereTables
186 * (reference ) add the tables that are needed for the where clause.
189 * the group where clause for this user
191 public static function getPermissionedStaticGroupClause($type, &$tables, &$whereTables) {
192 $config = CRM_Core_Config
::singleton();
193 return $config->userPermissionClass
->getPermissionedStaticGroupClause($type, $tables, $whereTables);
197 * Get all groups from database, filtered by permissions
200 * @param string $groupType
201 * Type of group(Access/Mailing).
202 * @param bool $excludeHidden
203 * exclude hidden groups.
207 * array reference of all groups.
209 public static function group($groupType, $excludeHidden = TRUE) {
210 $config = CRM_Core_Config
::singleton();
211 return $config->userPermissionClass
->group($groupType, $excludeHidden);
217 public static function customGroupAdmin() {
220 // check if user has all powerful permission
221 // or administer civicrm permission (CRM-1905)
222 if (self
::check('access all custom data')) {
227 self
::check('administer Multiple Organizations') &&
228 self
::isMultisiteEnabled()
233 if (self
::check('administer CiviCRM')) {
246 public static function customGroup($type = CRM_Core_Permission
::VIEW
, $reset = FALSE) {
247 $customGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_CustomField', 'custom_group_id',
248 array('fresh' => $reset));
249 $defaultGroups = array();
251 // check if user has all powerful permission
252 // or administer civicrm permission (CRM-1905)
253 if (self
::customGroupAdmin()) {
254 $defaultGroups = array_keys($customGroups);
257 return CRM_ACL_API
::group($type, NULL, 'civicrm_custom_group', $customGroups, $defaultGroups);
262 * @param null $prefix
267 public static function customGroupClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL, $reset = FALSE) {
268 if (self
::customGroupAdmin()) {
272 $groups = self
::customGroup($type, $reset);
273 if (empty($groups)) {
277 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
287 public static function ufGroupValid($gid, $type = CRM_Core_Permission
::VIEW
) {
292 $groups = self
::ufGroup($type);
293 return !empty($groups) && in_array($gid, $groups) ?
TRUE : FALSE;
301 public static function ufGroup($type = CRM_Core_Permission
::VIEW
) {
302 $ufGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_UFField', 'uf_group_id');
304 $allGroups = array_keys($ufGroups);
306 // check if user has all powerful permission
307 if (self
::check('profile listings and forms')) {
312 case CRM_Core_Permission
::VIEW
:
313 if (self
::check('profile view')) {
318 case CRM_Core_Permission
::CREATE
:
319 if (self
::check('profile create')) {
324 case CRM_Core_Permission
::EDIT
:
325 if (self
::check('profile edit')) {
330 case CRM_Core_Permission
::SEARCH
:
331 if (self
::check('profile listings')) {
337 return CRM_ACL_API
::group($type, NULL, 'civicrm_uf_group', $ufGroups);
342 * @param null $prefix
343 * @param bool $returnUFGroupIds
345 * @return array|string
347 public static function ufGroupClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL, $returnUFGroupIds = FALSE) {
348 $groups = self
::ufGroup($type);
349 if ($returnUFGroupIds) {
352 elseif (empty($groups)) {
356 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
362 * @param int $eventID
363 * @param string $context
367 public static function event($type = CRM_Core_Permission
::VIEW
, $eventID = NULL, $context = '') {
368 if (!empty($context)) {
369 if (CRM_Core_Permission
::check($context)) {
373 $events = CRM_Event_PseudoConstant
::event(NULL, TRUE);
374 $includeEvents = array();
376 // check if user has all powerful permission
377 if (self
::check('register for events')) {
378 $includeEvents = array_keys($events);
381 if ($type == CRM_Core_Permission
::VIEW
&&
382 self
::check('view event info')
384 $includeEvents = array_keys($events);
387 $permissionedEvents = CRM_ACL_API
::group($type, NULL, 'civicrm_event', $events, $includeEvents);
389 return $permissionedEvents;
391 if (!empty($permissionedEvents)) {
392 return array_search($eventID, $permissionedEvents) === FALSE ?
NULL : $eventID;
399 * @param null $prefix
403 public static function eventClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL) {
404 $events = self
::event($type);
405 if (empty($events)) {
409 return "{$prefix}id IN ( " . implode(',', $events) . ' ) ';
415 * @param bool $checkPermission
419 public static function access($module, $checkPermission = TRUE) {
420 $config = CRM_Core_Config
::singleton();
422 if (!in_array($module, $config->enableComponents
)) {
426 if ($checkPermission) {
427 if ($module == 'CiviCase') {
428 return CRM_Case_BAO_Case
::accessCiviCase();
431 return CRM_Core_Permission
::check("access $module");
439 * Check permissions for delete and edit actions.
441 * @param string $module
444 * Action to be check across component.
449 public static function checkActionPermission($module, $action) {
450 //check delete related permissions.
451 if ($action & CRM_Core_Action
::DELETE
) {
452 $permissionName = "delete in $module";
455 $editPermissions = array(
456 'CiviEvent' => 'edit event participants',
457 'CiviMember' => 'edit memberships',
458 'CiviPledge' => 'edit pledges',
459 'CiviContribute' => 'edit contributions',
460 'CiviGrant' => 'edit grants',
461 'CiviMail' => 'access CiviMail',
462 'CiviAuction' => 'add auction items',
464 $permissionName = CRM_Utils_Array
::value($module, $editPermissions);
467 if ($module == 'CiviCase' && !$permissionName) {
468 return CRM_Case_BAO_Case
::accessCiviCase();
471 //check for permission.
472 return CRM_Core_Permission
::check($permissionName);
482 public static function checkMenu(&$args, $op = 'and') {
483 if (!is_array($args)) {
486 foreach ($args as $str) {
487 $res = CRM_Core_Permission
::check($str);
488 if ($op == 'or' && $res) {
491 elseif ($op == 'and' && !$res) {
495 return ($op == 'or') ?
FALSE : TRUE;
504 public static function checkMenuItem(&$item) {
505 if (!array_key_exists('access_callback', $item)) {
506 CRM_Core_Error
::backtrace();
507 CRM_Core_Error
::fatal();
510 // if component_id is present, ensure it is enabled
511 if (isset($item['component_id']) &&
512 $item['component_id']
514 $config = CRM_Core_Config
::singleton();
515 if (is_array($config->enableComponentIDs
) &&
516 in_array($item['component_id'], $config->enableComponentIDs
)
518 // continue with process
525 // the following is imitating drupal 6 code in includes/menu.inc
526 if (empty($item['access_callback']) ||
527 is_numeric($item['access_callback'])
529 return (boolean
) $item['access_callback'];
532 // check whether the following Ajax requests submitted the right key
533 // FIXME: this should be integrated into ACLs proper
534 if (CRM_Utils_Array
::value('page_type', $item) == 3) {
535 if (!CRM_Core_Key
::validate($_REQUEST['key'], $item['path'])) {
540 // check if callback is for checkMenu, if so optimize it
541 if (is_array($item['access_callback']) &&
542 $item['access_callback'][0] == 'CRM_Core_Permission' &&
543 $item['access_callback'][1] == 'checkMenu'
545 $op = CRM_Utils_Array
::value(1, $item['access_arguments'], 'and');
546 return self
::checkMenu($item['access_arguments'][0], $op);
549 return call_user_func_array($item['access_callback'], $item['access_arguments']);
555 * @param bool $descriptions
556 * whether to return descriptions
560 public static function &basicPermissions($all = FALSE, $descriptions = FALSE) {
562 static $permissionsDesc = NULL;
564 if (!$permissionsDesc) {
565 $permissionsDesc = self
::assembleBasicPermissions($all, $descriptions);
568 return $permissionsDesc;
571 static $permissions = NULL;
574 $permissions = self
::assembleBasicPermissions($all, $descriptions);
583 * @param bool $descriptions
584 * whether to return descriptions
588 public static function assembleBasicPermissions($all = FALSE, $descriptions = FALSE) {
589 $config = CRM_Core_Config
::singleton();
590 $prefix = ts('CiviCRM') . ': ';
591 $permissions = self
::getCorePermissions($descriptions);
593 if (self
::isMultisiteEnabled()) {
594 $permissions['administer Multiple Organizations'] = $prefix . ts('administer Multiple Organizations');
598 $components = CRM_Core_Component
::getEnabledComponents();
601 $components = CRM_Core_Component
::getComponents();
604 foreach ($components as $comp) {
605 $perm = $comp->getPermissions(FALSE, $descriptions);
607 $info = $comp->getInfo();
608 foreach ($perm as $p => $attr) {
610 if (!is_array($attr)) {
611 $attr = array($attr);
614 $attr[0] = $info['translatedName'] . ': ' . $attr[0];
617 $permissions[$p] = $attr;
620 $permissions[$p] = $attr[0];
626 // Add any permissions defined in hook_civicrm_permission implementations.
627 $module_permissions = $config->userPermissionClass
->getAllModulePermissions($descriptions);
628 $permissions = array_merge($permissions, $module_permissions);
635 public static function getAnonymousPermissionsWarnings() {
636 static $permissions = array();
637 if (empty($permissions)) {
638 $permissions = array(
639 'administer CiviCRM',
641 $components = CRM_Core_Component
::getComponents();
642 foreach ($components as $comp) {
643 if (!method_exists($comp, 'getAnonymousPermissionWarnings')) {
646 $permissions = array_merge($permissions, $comp->getAnonymousPermissionWarnings());
653 * @param $anonymous_perms
657 public static function validateForPermissionWarnings($anonymous_perms) {
658 return array_intersect($anonymous_perms, self
::getAnonymousPermissionsWarnings());
662 * @param bool $descriptions
663 * whether to return descriptions
667 public static function getCorePermissions($descriptions = FALSE) {
668 $prefix = ts('CiviCRM') . ': ';
669 $permissions = array(
670 'add contacts' => array(
671 $prefix . ts('add contacts'),
672 ts('Create a new contact record in CiviCRM'),
674 'view all contacts' => array(
675 $prefix . ts('view all contacts'),
676 ts('View ANY CONTACT in the CiviCRM database, export contact info and perform activities such as Send Email, Phone Call, etc.'),
678 'edit all contacts' => array(
679 $prefix . ts('edit all contacts'),
680 ts('View, Edit and Delete ANY CONTACT in the CiviCRM database; Create and edit relationships, tags and other info about the contacts'),
682 'view my contact' => array(
683 $prefix . ts('view my contact'),
685 'edit my contact' => array(
686 $prefix . ts('edit my contact'),
688 'delete contacts' => array(
689 $prefix . ts('delete contacts'),
691 'access deleted contacts' => array(
692 $prefix . ts('access deleted contacts'),
693 ts('Access contacts in the trash'),
695 'import contacts' => array(
696 $prefix . ts('import contacts'),
697 ts('Import contacts and activities'),
699 'edit groups' => array(
700 $prefix . ts('edit groups'),
701 ts('Create new groups, edit group settings (e.g. group name, visibility...), delete groups'),
703 'administer CiviCRM' => array(
704 $prefix . ts('administer CiviCRM'),
705 ts('Perform all tasks in the Administer CiviCRM control panel and Import Contacts'),
707 'skip IDS check' => array(
708 $prefix . ts('skip IDS check'),
709 ts('IDS system is bypassed for users with this permission. Prevents false errors for admin users.'),
711 'access uploaded files' => array(
712 $prefix . ts('access uploaded files'),
713 ts('View / download files including images and photos'),
715 'profile listings and forms' => array(
716 $prefix . ts('profile listings and forms'),
717 ts('Access the profile Search form and listings'),
719 'profile listings' => array(
720 $prefix . ts('profile listings'),
722 'profile create' => array(
723 $prefix . ts('profile create'),
724 ts('Use profiles in Create mode'),
726 'profile edit' => array(
727 $prefix . ts('profile edit'),
728 ts('Use profiles in Edit mode'),
730 'profile view' => array(
731 $prefix . ts('profile view'),
733 'access all custom data' => array(
734 $prefix . ts('access all custom data'),
735 ts('View all custom fields regardless of ACL rules'),
737 'view all activities' => array(
738 $prefix . ts('view all activities'),
739 ts('View all activities (for visible contacts)'),
741 'delete activities' => array(
742 $prefix . ts('Delete activities'),
744 'access CiviCRM' => array(
745 $prefix . ts('access CiviCRM'),
746 ts('Master control for access to the main CiviCRM backend and API'),
748 'access Contact Dashboard' => array(
749 $prefix . ts('access Contact Dashboard'),
750 ts('View Contact Dashboard (for themselves and visible contacts)'),
752 'translate CiviCRM' => array(
753 $prefix . ts('translate CiviCRM'),
754 ts('Allow User to enable multilingual'),
756 'administer reserved groups' => array(
757 $prefix . ts('administer reserved groups'),
758 ts('Edit and disable Reserved Groups (Needs Edit Groups)'),
760 'administer Tagsets' => array(
761 $prefix . ts('administer Tagsets'),
763 'administer reserved tags' => array(
764 $prefix . ts('administer reserved tags'),
766 'administer dedupe rules' => array(
767 $prefix . ts('administer dedupe rules'),
768 ts('Create and edit rules, change the supervised and unsupervised rules'),
770 'merge duplicate contacts' => array(
771 $prefix . ts('merge duplicate contacts'),
772 ts('Delete Contacts must also be granted in order for this to work.'),
774 'view debug output' => array(
775 $prefix . ts('view debug output'),
776 ts('View results of debug and backtrace'),
778 'view all notes' => array(
779 $prefix . ts('view all notes'),
780 ts("View notes (for visible contacts) even if they're marked admin only"),
782 'access AJAX API' => array(
783 $prefix . ts('access AJAX API'),
784 ts('Allow API access even if Access CiviCRM is not granted'),
786 'access contact reference fields' => array(
787 $prefix . ts('access contact reference fields'),
788 ts('Allow entering data into contact reference fields'),
790 'create manual batch' => array(
791 $prefix . ts('create manual batch'),
792 ts('Create an accounting batch (with Access to CiviContribute and View Own/All Manual Batches)'),
794 'edit own manual batches' => array(
795 $prefix . ts('edit own manual batches'),
796 ts('Edit accounting batches created by user'),
798 'edit all manual batches' => array(
799 $prefix . ts('edit all manual batches'),
800 ts('Edit all accounting batches'),
802 'view own manual batches' => array(
803 $prefix . ts('view own manual batches'),
804 ts('View accounting batches created by user (with Access to CiviContribute)'),
806 'view all manual batches' => array(
807 $prefix . ts('view all manual batches'),
808 ts('View all accounting batches (with Access to CiviContribute)'),
810 'delete own manual batches' => array(
811 $prefix . ts('delete own manual batches'),
812 ts('Delete accounting batches created by user'),
814 'delete all manual batches' => array(
815 $prefix . ts('delete all manual batches'),
816 ts('Delete all accounting batches'),
818 'export own manual batches' => array(
819 $prefix . ts('export own manual batches'),
820 ts('Export accounting batches created by user'),
822 'export all manual batches' => array(
823 $prefix . ts('export all manual batches'),
824 ts('Export all accounting batches'),
826 'administer payment processors' => array(
827 $prefix . ts('administer payment processors'),
828 ts('Add, Update, or Disable Payment Processors'),
830 'edit message templates' => array(
831 $prefix . ts('edit message templates'),
833 'view my invoices' => array(
834 $prefix . ts('view my invoices'),
835 ts('Allow users to view/ download their own invoices'),
839 if (!$descriptions) {
840 foreach ($permissions as $name => $attr) {
841 $permissions[$name] = array_shift($attr);
849 * Validate user permission across.
850 * edit or view or with supportable acls.
854 public static function giveMeAllACLs() {
855 if (CRM_Core_Permission
::check('view all contacts') ||
856 CRM_Core_Permission
::check('edit all contacts')
861 $session = CRM_Core_Session
::singleton();
862 $contactID = $session->get('userID');
865 $aclPermission = self
::getPermission();
866 if (in_array($aclPermission, array(
867 CRM_Core_Permission
::EDIT
,
868 CRM_Core_Permission
::VIEW
,
874 // run acl where hook and see if the user is supplying an ACL clause
876 $tables = $whereTables = array();
879 CRM_Utils_Hook
::aclWhereClause(CRM_Core_Permission
::VIEW
,
880 $tables, $whereTables,
883 return empty($whereTables) ?
FALSE : TRUE;
887 * Get component name from given permission.
889 * @param string $permission
891 * @return null|string
892 * the name of component.
894 public static function getComponentName($permission) {
895 $componentName = NULL;
896 $permission = trim($permission);
897 if (empty($permission)) {
898 return $componentName;
901 static $allCompPermissions = array();
902 if (empty($allCompPermissions)) {
903 $components = CRM_Core_Component
::getComponents();
904 foreach ($components as $name => $comp) {
905 //get all permissions of each components unconditionally
906 $allCompPermissions[$name] = $comp->getPermissions(TRUE);
910 if (is_array($allCompPermissions)) {
911 foreach ($allCompPermissions as $name => $permissions) {
912 if (array_key_exists($permission, $permissions)) {
913 $componentName = $name;
919 return $componentName;
923 * Get all the contact emails for users that have a specific permission.
925 * @param string $permissionName
926 * Name of the permission we are interested in.
929 * a comma separated list of email addresses
931 public static function permissionEmails($permissionName) {
932 $config = CRM_Core_Config
::singleton();
933 return $config->userPermissionClass
->permissionEmails($permissionName);
937 * Get all the contact emails for users that have a specific role.
939 * @param string $roleName
940 * Name of the role we are interested in.
943 * a comma separated list of email addresses
945 public static function roleEmails($roleName) {
946 $config = CRM_Core_Config
::singleton();
947 return $config->userRoleClass
->roleEmails($roleName);
953 public static function isMultisiteEnabled() {
954 return CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
960 * Verify if the user has permission to get the invoice.
963 * TRUE if the user has download all invoices permission or download my
964 * invoices permission and the invoice author is the current user.
966 public static function checkDownloadInvoice() {
968 $cid = CRM_Core_BAO_UFMatch
::getContactId($user->uid
);
969 if (CRM_Core_Permission
::check('access CiviContribute') ||
970 (CRM_Core_Permission
::check('view my invoices') && $_GET['cid'] == $cid)