3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * get the current permission of this user
69 * @return string the permission of the user (edit or view or null)
71 public static function getPermission() {
72 $config = CRM_Core_Config
::singleton();
73 return $config->userPermissionClass
->getPermission( );
77 * given a permission string, check for access requirements
79 * @param string $str the permission to check
81 * @return boolean true if yes, else false
85 static function check($str) {
86 $config = CRM_Core_Config
::singleton();
87 return $config->userPermissionClass
->check( $str );
91 * Determine if any one of the permissions strings applies to current user
96 public static function checkAnyPerm($perms) {
97 foreach ($perms as $perm) {
98 if (CRM_Core_Permission
::check($perm)) {
106 * Given a group/role array, check for access requirements
108 * @param array $array the group/role to check
110 * @return boolean true if yes, else false
114 static function checkGroupRole($array) {
115 $config = CRM_Core_Config
::singleton();
116 return $config->userPermissionClass
->checkGroupRole( $array );
120 * Get the permissioned where clause for the user
122 * @param int $type the type of permission needed
123 * @param array $tables (reference ) add the tables that are needed for the select clause
124 * @param array $whereTables (reference ) add the tables that are needed for the where clause
126 * @return string the group where clause for this user
129 public static function getPermissionedStaticGroupClause($type, &$tables, &$whereTables) {
130 $config = CRM_Core_Config
::singleton();
131 return $config->userPermissionClass
->getPermissionedStaticGroupClause( $type, $tables, $whereTables );
135 * Get all groups from database, filtered by permissions
138 * @param string $groupType type of group(Access/Mailing)
139 * @param boolen $excludeHidden exclude hidden groups.
144 * @return array - array reference of all groups.
147 public static function group($groupType, $excludeHidden = TRUE) {
148 $config = CRM_Core_Config
::singleton();
149 return $config->userPermissionClass
->group( $groupType, $excludeHidden );
152 public static function customGroupAdmin() {
155 // check if user has all powerful permission
156 // or administer civicrm permission (CRM-1905)
157 if (self
::check('access all custom data')) {
161 if (self
::check('administer Multiple Organizations') &&
162 self
::isMultisiteEnabled()
167 if (self
::check('administer CiviCRM')) {
174 public static function customGroup($type = CRM_Core_Permission
::VIEW
, $reset = FALSE) {
175 $customGroups = CRM_Core_PseudoConstant
::customGroup($reset);
176 $defaultGroups = array();
178 // check if user has all powerful permission
179 // or administer civicrm permission (CRM-1905)
180 if (self
::customGroupAdmin()) {
181 $defaultGroups = array_keys($customGroups);
184 return CRM_ACL_API
::group($type, NULL, 'civicrm_custom_group', $customGroups, $defaultGroups);
187 static function customGroupClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL, $reset = FALSE) {
188 if (self
::customGroupAdmin()) {
192 $groups = self
::customGroup($type, $reset);
193 if (empty($groups)) {
197 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
201 public static function ufGroupValid($gid, $type = CRM_Core_Permission
::VIEW
) {
206 $groups = self
::ufGroup($type);
207 return in_array($gid, $groups) ?
TRUE : FALSE;
210 public static function ufGroup($type = CRM_Core_Permission
::VIEW
) {
211 $ufGroups = CRM_Core_PseudoConstant
::ufGroup();
213 $allGroups = array_keys($ufGroups);
215 // check if user has all powerful permission
216 if (self
::check('profile listings and forms')) {
221 case CRM_Core_Permission
::VIEW
:
222 if (self
::check('profile view')) {
227 case CRM_Core_Permission
::CREATE
:
228 if (self
::check('profile create')) {
233 case CRM_Core_Permission
::EDIT
:
234 if (self
::check('profile edit')) {
239 case CRM_Core_Permission
::SEARCH
:
240 if (self
::check('profile listings')) {
246 return CRM_ACL_API
::group($type, NULL, 'civicrm_uf_group', $ufGroups);
249 static function ufGroupClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL, $returnUFGroupIds = FALSE) {
250 $groups = self
::ufGroup($type);
251 if ($returnUFGroupIds) {
254 elseif (empty($groups)) {
258 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
262 public static function event($type = CRM_Core_Permission
::VIEW
, $eventID = NULL) {
263 $events = CRM_Event_PseudoConstant
::event(NULL, TRUE);
264 $includeEvents = array();
266 // check if user has all powerful permission
267 if (self
::check('register for events')) {
268 $includeEvents = array_keys($events);
271 if ($type == CRM_Core_Permission
::VIEW
&&
272 self
::check('view event info')
274 $includeEvents = array_keys($events);
277 $permissionedEvents = CRM_ACL_API
::group($type, NULL, 'civicrm_event', $events, $includeEvents);
279 return $permissionedEvents;
281 return array_search($eventID, $permissionedEvents) === FALSE ?
NULL : $eventID;
284 static function eventClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL) {
285 $events = self
::event($type);
286 if (empty($events)) {
290 return "{$prefix}id IN ( " . implode(',', $events) . ' ) ';
294 static function access($module, $checkPermission = TRUE) {
295 $config = CRM_Core_Config
::singleton();
297 if (!in_array($module, $config->enableComponents
)) {
301 if ($checkPermission) {
302 if ($module == 'CiviCase') {
303 return CRM_Case_BAO_Case
::accessCiviCase();
306 return CRM_Core_Permission
::check("access $module");
314 * check permissions for delete and edit actions
316 * @param string $module component name.
317 * @param $action action to be check across component
320 static function checkActionPermission($module, $action) {
321 //check delete related permissions.
322 if ($action & CRM_Core_Action
::DELETE
) {
323 $permissionName = "delete in $module";
326 $editPermissions = array(
327 'CiviEvent' => 'edit event participants',
328 'CiviMember' => 'edit memberships',
329 'CiviPledge' => 'edit pledges',
330 'CiviContribute' => 'edit contributions',
331 'CiviGrant' => 'edit grants',
332 'CiviMail' => 'access CiviMail',
333 'CiviAuction' => 'add auction items',
335 $permissionName = CRM_Utils_Array
::value($module, $editPermissions);
338 if ($module == 'CiviCase' && !$permissionName) {
339 return CRM_Case_BAO_Case
::accessCiviCase();
342 //check for permission.
343 return CRM_Core_Permission
::check($permissionName);
347 static function checkMenu(&$args, $op = 'and') {
348 if (!is_array($args)) {
351 foreach ($args as $str) {
352 $res = CRM_Core_Permission
::check($str);
353 if ($op == 'or' && $res) {
356 elseif ($op == 'and' && !$res) {
360 return ($op == 'or') ?
FALSE : TRUE;
363 static function checkMenuItem(&$item) {
364 if (!array_key_exists('access_callback', $item)) {
365 CRM_Core_Error
::backtrace();
366 CRM_Core_Error
::fatal();
369 // if component_id is present, ensure it is enabled
370 if (isset($item['component_id']) &&
371 $item['component_id']
373 $config = CRM_Core_Config
::singleton();
374 if (is_array($config->enableComponentIDs
) &&
375 in_array($item['component_id'], $config->enableComponentIDs
)
377 // continue with process
384 // the following is imitating drupal 6 code in includes/menu.inc
385 if (empty($item['access_callback']) ||
386 is_numeric($item['access_callback'])
388 return (boolean
) $item['access_callback'];
391 // check whether the following Ajax requests submitted the right key
392 // FIXME: this should be integrated into ACLs proper
393 if (CRM_Utils_Array
::value('page_type', $item) == 3) {
394 if (!CRM_Core_Key
::validate($_REQUEST['key'], $item['path'])) {
399 // check if callback is for checkMenu, if so optimize it
400 if (is_array($item['access_callback']) &&
401 $item['access_callback'][0] == 'CRM_Core_Permission' &&
402 $item['access_callback'][1] == 'checkMenu'
404 $op = CRM_Utils_Array
::value(1, $item['access_arguments'], 'and');
405 return self
::checkMenu($item['access_arguments'][0], $op);
408 return call_user_func_array($item['access_callback'], $item['access_arguments']);
412 static function &basicPermissions($all = FALSE) {
413 static $permissions = NULL;
416 $prefix = ts('CiviCRM') . ': ';
417 $permissions = self
::getCorePermissions();
419 if (self
::isMultisiteEnabled()) {
420 $permissions['administer Multiple Organizations'] = $prefix . ts('administer Multiple Organizations');
423 $config = CRM_Core_Config
::singleton();
426 $components = CRM_Core_Component
::getEnabledComponents();
429 $components = CRM_Core_Component
::getComponents();
432 foreach ($components as $comp) {
433 $perm = $comp->getPermissions();
435 $info = $comp->getInfo();
436 foreach ($perm as $p) {
437 $permissions[$p] = $info['translatedName'] . ': ' . $p;
442 // Add any permissions defined in hook_civicrm_permission implementations.
443 $config = CRM_Core_Config
::singleton();
444 $module_permissions = $config->userPermissionClass
->getAllModulePermissions();
445 $permissions = array_merge($permissions, $module_permissions);
451 static function getCorePermissions() {
452 $prefix = ts('CiviCRM') . ': ';
453 $permissions = array(
454 'add contacts' => $prefix . ts('add contacts'),
455 'view all contacts' => $prefix . ts('view all contacts'),
456 'edit all contacts' => $prefix . ts('edit all contacts'),
457 'delete contacts' => $prefix . ts('delete contacts'),
458 'access deleted contacts' => $prefix . ts('access deleted contacts'),
459 'import contacts' => $prefix . ts('import contacts'),
460 'edit groups' => $prefix . ts('edit groups'),
461 'administer CiviCRM' => $prefix . ts('administer CiviCRM'),
462 'access uploaded files' => $prefix . ts('access uploaded files'),
463 'profile listings and forms' => $prefix . ts('profile listings and forms'),
464 'profile listings' => $prefix . ts('profile listings'),
465 'profile create' => $prefix . ts('profile create'),
466 'profile edit' => $prefix . ts('profile edit'),
467 'profile view' => $prefix . ts('profile view'),
468 'access all custom data' => $prefix . ts('access all custom data'),
469 'view all activities' => $prefix . ts('view all activities'),
470 'delete activities' => $prefix . ts('delete activities'),
471 'access CiviCRM' => $prefix . ts('access CiviCRM'),
472 'access Contact Dashboard' => $prefix . ts('access Contact Dashboard'),
473 'translate CiviCRM' => $prefix . ts('translate CiviCRM'),
474 'administer reserved groups' => $prefix . ts('administer reserved groups'),
475 'administer Tagsets' => $prefix . ts('administer Tagsets'),
476 'administer reserved tags' => $prefix . ts('administer reserved tags'),
477 'administer dedupe rules' => $prefix . ts('administer dedupe rules'),
478 'merge duplicate contacts' => $prefix . ts('merge duplicate contacts'),
479 'view debug output' => $prefix . ts('view debug output'),
480 'view all notes' => $prefix . ts('view all notes'),
481 'access AJAX API' => $prefix . ts('access AJAX API'),
482 'access contact reference fields' => $prefix . ts('access contact reference fields'),
483 'create manual batch' => $prefix . ts('create manual batch'),
484 'edit own manual batches' => $prefix . ts('edit own manual batches'),
485 'edit all manual batches' => $prefix . ts('edit all manual batches'),
486 'view own manual batches' => $prefix . ts('view own manual batches'),
487 'view all manual batches' => $prefix . ts('view all manual batches'),
488 'delete own manual batches' => $prefix . ts('delete own manual batches'),
489 'delete all manual batches' => $prefix . ts('delete all manual batches'),
490 'export own manual batches' => $prefix . ts('export own manual batches'),
491 'export all manual batches' => $prefix . ts('export all manual batches'),
498 * Validate user permission across
499 * edit or view or with supportable acls.
501 * return boolean true/false.
503 static function giveMeAllACLs() {
504 if (CRM_Core_Permission
::check('view all contacts') ||
505 CRM_Core_Permission
::check('edit all contacts')
510 $session = CRM_Core_Session
::singleton();
511 $contactID = $session->get('userID');
514 $aclPermission = self
::getPermission();
515 if (in_array($aclPermission, array(
516 CRM_Core_Permission
::EDIT
,
517 CRM_Core_Permission
::VIEW
,
522 // run acl where hook and see if the user is supplying an ACL clause
524 $tables = $whereTables = array();
527 CRM_Utils_Hook
::aclWhereClause(CRM_Core_Permission
::VIEW
,
528 $tables, $whereTables,
531 return empty($whereTables) ?
FALSE : TRUE;
535 * Function to get component name from given permission.
537 * @param string $permission
539 * return string $componentName the name of component.
542 static function getComponentName($permission) {
543 $componentName = NULL;
544 $permission = trim($permission);
545 if (empty($permission)) {
546 return $componentName;
549 static $allCompPermissions = array();
550 if (empty($allCompPermissions)) {
551 $components = CRM_Core_Component
::getComponents();
552 foreach ($components as $name => $comp) {
553 //get all permissions of each components unconditionally
554 $allCompPermissions[$name] = $comp->getPermissions(TRUE);
558 if (is_array($allCompPermissions)) {
559 foreach ($allCompPermissions as $name => $permissions) {
560 if (in_array($permission, $permissions)) {
561 $componentName = $name;
567 return $componentName;
571 * Get all the contact emails for users that have a specific permission
573 * @param string $permissionName name of the permission we are interested in
575 * @return string a comma separated list of email addresses
577 public static function permissionEmails($permissionName) {
578 $config = CRM_Core_Config
::singleton();
579 return $config->userPermissionClass
->permissionEmails( $permissionName );
583 * Get all the contact emails for users that have a specific role
585 * @param string $roleName name of the role we are interested in
587 * @return string a comma separated list of email addresses
589 public static function roleEmails($roleName) {
590 $config = CRM_Core_Config
::singleton();
591 return $config->userRoleClass
->roleEmails( $roleName );
594 static function isMultisiteEnabled() {
595 return CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,