3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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
76 * @return string the permission of the user (edit or view or null)
78 public static function getPermission() {
79 $config = CRM_Core_Config
::singleton();
80 return $config->userPermissionClass
->getPermission();
84 * given a permission string or array, check for access requirements
85 * @param mixed $permissions the permission to check as an array or string -see examples
90 * Must have 'access CiviCRM'
91 * (string) 'access CiviCRM'
94 * Ex 2 Must have 'access CiviCRM' and 'access Ajax API'
95 * array('access CiviCRM', 'access Ajax API')
97 * Ex 3 Must have 'access CiviCRM' or 'access Ajax API'
99 * array('access CiviCRM', 'access Ajax API'),
102 * Ex 4 Must have 'access CiviCRM' or 'access Ajax API' AND 'access CiviEvent'
104 * array('access CiviCRM', 'access Ajax API'),
105 * 'access CiviEvent',
108 * Note that in permissions.php this is keyed by the action eg.
109 * (access Civi || access AJAX) && (access CiviEvent || access CiviContribute)
110 * 'myaction' => array(
111 * array('access CiviCRM', 'access Ajax API'),
112 * array('access CiviEvent', 'access CiviContribute')
115 * @return boolean true if yes, else false
119 static function check($permissions) {
120 $permissions = (array) $permissions;
122 foreach ($permissions as $permission) {
123 if(is_array($permission)) {
124 foreach ($permission as $orPerm) {
125 if(self
::check($orPerm)) {
126 //one of our 'or' permissions has succeeded - stop checking this permission
130 //none of our our conditions was met
134 if(!CRM_Core_Config
::singleton()->userPermissionClass
->check($permission)) {
135 //one of our 'and' conditions has not been met
144 * Determine if any one of the permissions strings applies to current user
146 * @param array $perms
149 public static function checkAnyPerm($perms) {
150 foreach ($perms as $perm) {
151 if (CRM_Core_Permission
::check($perm)) {
159 * Given a group/role array, check for access requirements
161 * @param array $array the group/role to check
163 * @return boolean true if yes, else false
167 static function checkGroupRole($array) {
168 $config = CRM_Core_Config
::singleton();
169 return $config->userPermissionClass
->checkGroupRole($array);
173 * Get the permissioned where clause for the user
175 * @param int $type the type of permission needed
176 * @param array $tables (reference ) add the tables that are needed for the select clause
177 * @param array $whereTables (reference ) add the tables that are needed for the where clause
179 * @return string the group where clause for this user
182 public static function getPermissionedStaticGroupClause($type, &$tables, &$whereTables) {
183 $config = CRM_Core_Config
::singleton();
184 return $config->userPermissionClass
->getPermissionedStaticGroupClause($type, $tables, $whereTables);
188 * Get all groups from database, filtered by permissions
191 * @param string $groupType type of group(Access/Mailing)
192 * @param bool|\boolen $excludeHidden exclude hidden groups.
197 * @return array - array reference of all groups.
199 public static function group($groupType, $excludeHidden = TRUE) {
200 $config = CRM_Core_Config
::singleton();
201 return $config->userPermissionClass
->group($groupType, $excludeHidden);
204 public static function customGroupAdmin() {
207 // check if user has all powerful permission
208 // or administer civicrm permission (CRM-1905)
209 if (self
::check('access all custom data')) {
214 self
::check('administer Multiple Organizations') &&
215 self
::isMultisiteEnabled()
220 if (self
::check('administer CiviCRM')) {
227 public static function customGroup($type = CRM_Core_Permission
::VIEW
, $reset = FALSE) {
228 $customGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_CustomField', 'custom_group_id',
229 array('fresh' => $reset));
230 $defaultGroups = array();
232 // check if user has all powerful permission
233 // or administer civicrm permission (CRM-1905)
234 if (self
::customGroupAdmin()) {
235 $defaultGroups = array_keys($customGroups);
238 return CRM_ACL_API
::group($type, NULL, 'civicrm_custom_group', $customGroups, $defaultGroups);
241 static function customGroupClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL, $reset = FALSE) {
242 if (self
::customGroupAdmin()) {
246 $groups = self
::customGroup($type, $reset);
247 if (empty($groups)) {
251 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
255 public static function ufGroupValid($gid, $type = CRM_Core_Permission
::VIEW
) {
260 $groups = self
::ufGroup($type);
261 return in_array($gid, $groups) ?
TRUE : FALSE;
264 public static function ufGroup($type = CRM_Core_Permission
::VIEW
) {
265 $ufGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_UFField', 'uf_group_id');
267 $allGroups = array_keys($ufGroups);
269 // check if user has all powerful permission
270 if (self
::check('profile listings and forms')) {
275 case CRM_Core_Permission
::VIEW
:
276 if (self
::check('profile view')) {
281 case CRM_Core_Permission
::CREATE
:
282 if (self
::check('profile create')) {
287 case CRM_Core_Permission
::EDIT
:
288 if (self
::check('profile edit')) {
293 case CRM_Core_Permission
::SEARCH
:
294 if (self
::check('profile listings')) {
300 return CRM_ACL_API
::group($type, NULL, 'civicrm_uf_group', $ufGroups);
303 static function ufGroupClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL, $returnUFGroupIds = FALSE) {
304 $groups = self
::ufGroup($type);
305 if ($returnUFGroupIds) {
308 elseif (empty($groups)) {
312 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
316 public static function event($type = CRM_Core_Permission
::VIEW
, $eventID = NULL) {
317 $events = CRM_Event_PseudoConstant
::event(NULL, TRUE);
318 $includeEvents = array();
320 // check if user has all powerful permission
321 if (self
::check('register for events')) {
322 $includeEvents = array_keys($events);
325 if ($type == CRM_Core_Permission
::VIEW
&&
326 self
::check('view event info')
328 $includeEvents = array_keys($events);
331 $permissionedEvents = CRM_ACL_API
::group($type, NULL, 'civicrm_event', $events, $includeEvents);
333 return $permissionedEvents;
335 if (!empty($permissionedEvents)) {
336 return array_search($eventID, $permissionedEvents) === FALSE ?
NULL : $eventID;
341 static function eventClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL) {
342 $events = self
::event($type);
343 if (empty($events)) {
347 return "{$prefix}id IN ( " . implode(',', $events) . ' ) ';
351 static function access($module, $checkPermission = TRUE) {
352 $config = CRM_Core_Config
::singleton();
354 if (!in_array($module, $config->enableComponents
)) {
358 if ($checkPermission) {
359 if ($module == 'CiviCase') {
360 return CRM_Case_BAO_Case
::accessCiviCase();
363 return CRM_Core_Permission
::check("access $module");
371 * check permissions for delete and edit actions
373 * @param string $module component name.
374 * @param $action action to be check across component
379 static function checkActionPermission($module, $action) {
380 //check delete related permissions.
381 if ($action & CRM_Core_Action
::DELETE
) {
382 $permissionName = "delete in $module";
385 $editPermissions = array(
386 'CiviEvent' => 'edit event participants',
387 'CiviMember' => 'edit memberships',
388 'CiviPledge' => 'edit pledges',
389 'CiviContribute' => 'edit contributions',
390 'CiviGrant' => 'edit grants',
391 'CiviMail' => 'access CiviMail',
392 'CiviAuction' => 'add auction items',
394 $permissionName = CRM_Utils_Array
::value($module, $editPermissions);
397 if ($module == 'CiviCase' && !$permissionName) {
398 return CRM_Case_BAO_Case
::accessCiviCase();
401 //check for permission.
402 return CRM_Core_Permission
::check($permissionName);
406 static function checkMenu(&$args, $op = 'and') {
407 if (!is_array($args)) {
410 foreach ($args as $str) {
411 $res = CRM_Core_Permission
::check($str);
412 if ($op == 'or' && $res) {
415 elseif ($op == 'and' && !$res) {
419 return ($op == 'or') ?
FALSE : TRUE;
422 static function checkMenuItem(&$item) {
423 if (!array_key_exists('access_callback', $item)) {
424 CRM_Core_Error
::backtrace();
425 CRM_Core_Error
::fatal();
428 // if component_id is present, ensure it is enabled
429 if (isset($item['component_id']) &&
430 $item['component_id']
432 $config = CRM_Core_Config
::singleton();
433 if (is_array($config->enableComponentIDs
) &&
434 in_array($item['component_id'], $config->enableComponentIDs
)
436 // continue with process
443 // the following is imitating drupal 6 code in includes/menu.inc
444 if (empty($item['access_callback']) ||
445 is_numeric($item['access_callback'])
447 return (boolean
) $item['access_callback'];
450 // check whether the following Ajax requests submitted the right key
451 // FIXME: this should be integrated into ACLs proper
452 if (CRM_Utils_Array
::value('page_type', $item) == 3) {
453 if (!CRM_Core_Key
::validate($_REQUEST['key'], $item['path'])) {
458 // check if callback is for checkMenu, if so optimize it
459 if (is_array($item['access_callback']) &&
460 $item['access_callback'][0] == 'CRM_Core_Permission' &&
461 $item['access_callback'][1] == 'checkMenu'
463 $op = CRM_Utils_Array
::value(1, $item['access_arguments'], 'and');
464 return self
::checkMenu($item['access_arguments'][0], $op);
467 return call_user_func_array($item['access_callback'], $item['access_arguments']);
471 static function &basicPermissions($all = FALSE) {
472 static $permissions = NULL;
475 $config = CRM_Core_Config
::singleton();
476 $prefix = ts('CiviCRM') . ': ';
477 $permissions = self
::getCorePermissions();
479 if (self
::isMultisiteEnabled()) {
480 $permissions['administer Multiple Organizations'] = $prefix . ts('administer Multiple Organizations');
484 $components = CRM_Core_Component
::getEnabledComponents();
487 $components = CRM_Core_Component
::getComponents();
490 foreach ($components as $comp) {
491 $perm = $comp->getPermissions();
493 $info = $comp->getInfo();
494 foreach ($perm as $p) {
495 $permissions[$p] = $info['translatedName'] . ': ' . $p;
500 // Add any permissions defined in hook_civicrm_permission implementations.
501 $module_permissions = $config->userPermissionClass
->getAllModulePermissions();
502 $permissions = array_merge($permissions, $module_permissions);
508 static function getAnonymousPermissionsWarnings() {
509 static $permissions = array();
510 if (empty($permissions)) {
511 $permissions = array(
514 $components = CRM_Core_Component
::getComponents();
515 foreach ($components as $comp) {
516 if (!method_exists($comp, 'getAnonymousPermissionWarnings')) {
519 $permissions = array_merge($permissions, $comp->getAnonymousPermissionWarnings());
525 static function validateForPermissionWarnings($anonymous_perms) {
526 return array_intersect($anonymous_perms, self
::getAnonymousPermissionsWarnings());
529 static function getCorePermissions() {
530 $prefix = ts('CiviCRM') . ': ';
531 $permissions = array(
532 'add contacts' => $prefix . ts('add contacts'),
533 'view all contacts' => $prefix . ts('view all contacts'),
534 'edit all contacts' => $prefix . ts('edit all contacts'),
535 'view my contact' => $prefix . ts('view my contact'),
536 'edit my contact' => $prefix . ts('edit my contact'),
537 'delete contacts' => $prefix . ts('delete contacts'),
538 'access deleted contacts' => $prefix . ts('access deleted contacts'),
539 'import contacts' => $prefix . ts('import contacts'),
540 'edit groups' => $prefix . ts('edit groups'),
541 'administer CiviCRM' => $prefix . ts('administer CiviCRM'),
542 'skip IDS check' => $prefix . ts('skip IDS check'),
543 'access uploaded files' => $prefix . ts('access uploaded files'),
544 'profile listings and forms' => $prefix . ts('profile listings and forms'),
545 'profile listings' => $prefix . ts('profile listings'),
546 'profile create' => $prefix . ts('profile create'),
547 'profile edit' => $prefix . ts('profile edit'),
548 'profile view' => $prefix . ts('profile view'),
549 'access all custom data' => $prefix . ts('access all custom data'),
550 'view all activities' => $prefix . ts('view all activities'),
551 'delete activities' => $prefix . ts('delete activities'),
552 'access CiviCRM' => $prefix . ts('access CiviCRM'),
553 'access Contact Dashboard' => $prefix . ts('access Contact Dashboard'),
554 'translate CiviCRM' => $prefix . ts('translate CiviCRM'),
555 'administer reserved groups' => $prefix . ts('administer reserved groups'),
556 'administer Tagsets' => $prefix . ts('administer Tagsets'),
557 'administer reserved tags' => $prefix . ts('administer reserved tags'),
558 'administer dedupe rules' => $prefix . ts('administer dedupe rules'),
559 'merge duplicate contacts' => $prefix . ts('merge duplicate contacts'),
560 'view debug output' => $prefix . ts('view debug output'),
561 'view all notes' => $prefix . ts('view all notes'),
562 'access AJAX API' => $prefix . ts('access AJAX API'),
563 'access contact reference fields' => $prefix . ts('access contact reference fields'),
564 'create manual batch' => $prefix . ts('create manual batch'),
565 'edit own manual batches' => $prefix . ts('edit own manual batches'),
566 'edit all manual batches' => $prefix . ts('edit all manual batches'),
567 'view own manual batches' => $prefix . ts('view own manual batches'),
568 'view all manual batches' => $prefix . ts('view all manual batches'),
569 'delete own manual batches' => $prefix . ts('delete own manual batches'),
570 'delete all manual batches' => $prefix . ts('delete all manual batches'),
571 'export own manual batches' => $prefix . ts('export own manual batches'),
572 'export all manual batches' => $prefix . ts('export all manual batches'),
573 'administer payment processors' => $prefix . ts('administer payment processors'),
580 * Validate user permission across
581 * edit or view or with supportable acls.
583 * return boolean true/false.
585 static function giveMeAllACLs() {
586 if (CRM_Core_Permission
::check('view all contacts') ||
587 CRM_Core_Permission
::check('edit all contacts')
592 $session = CRM_Core_Session
::singleton();
593 $contactID = $session->get('userID');
596 $aclPermission = self
::getPermission();
597 if (in_array($aclPermission, array(
598 CRM_Core_Permission
::EDIT
,
599 CRM_Core_Permission
::VIEW
,
605 // run acl where hook and see if the user is supplying an ACL clause
607 $tables = $whereTables = array();
610 CRM_Utils_Hook
::aclWhereClause(CRM_Core_Permission
::VIEW
,
611 $tables, $whereTables,
614 return empty($whereTables) ?
FALSE : TRUE;
618 * Function to get component name from given permission.
620 * @param string $permission
622 * return string $componentName the name of component.
624 * @return int|null|string
627 static function getComponentName($permission) {
628 $componentName = NULL;
629 $permission = trim($permission);
630 if (empty($permission)) {
631 return $componentName;
634 static $allCompPermissions = array();
635 if (empty($allCompPermissions)) {
636 $components = CRM_Core_Component
::getComponents();
637 foreach ($components as $name => $comp) {
638 //get all permissions of each components unconditionally
639 $allCompPermissions[$name] = $comp->getPermissions(TRUE);
643 if (is_array($allCompPermissions)) {
644 foreach ($allCompPermissions as $name => $permissions) {
645 if (in_array($permission, $permissions)) {
646 $componentName = $name;
652 return $componentName;
656 * Get all the contact emails for users that have a specific permission
658 * @param string $permissionName name of the permission we are interested in
660 * @return string a comma separated list of email addresses
662 public static function permissionEmails($permissionName) {
663 $config = CRM_Core_Config
::singleton();
664 return $config->userPermissionClass
->permissionEmails($permissionName);
668 * Get all the contact emails for users that have a specific role
670 * @param string $roleName name of the role we are interested in
672 * @return string a comma separated list of email addresses
674 public static function roleEmails($roleName) {
675 $config = CRM_Core_Config
::singleton();
676 return $config->userRoleClass
->roleEmails($roleName);
679 static function isMultisiteEnabled() {
680 return CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,