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')) {
162 self
::check('administer Multiple Organizations') &&
163 self
::isMultisiteEnabled()
168 if (self
::check('administer CiviCRM')) {
175 public static function customGroup($type = CRM_Core_Permission
::VIEW
, $reset = FALSE) {
176 $customGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_CustomField', 'custom_group_id', array('fresh' => $reset));
177 $defaultGroups = array();
179 // check if user has all powerful permission
180 // or administer civicrm permission (CRM-1905)
181 if (self
::customGroupAdmin()) {
182 $defaultGroups = array_keys($customGroups);
185 return CRM_ACL_API
::group($type, NULL, 'civicrm_custom_group', $customGroups, $defaultGroups);
188 static function customGroupClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL, $reset = FALSE) {
189 if (self
::customGroupAdmin()) {
193 $groups = self
::customGroup($type, $reset);
194 if (empty($groups)) {
198 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
202 public static function ufGroupValid($gid, $type = CRM_Core_Permission
::VIEW
) {
207 $groups = self
::ufGroup($type);
208 return in_array($gid, $groups) ?
TRUE : FALSE;
211 public static function ufGroup($type = CRM_Core_Permission
::VIEW
) {
212 $ufGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_UFField', 'uf_group_id');
214 $allGroups = array_keys($ufGroups);
216 // check if user has all powerful permission
217 if (self
::check('profile listings and forms')) {
222 case CRM_Core_Permission
::VIEW
:
223 if (self
::check('profile view')) {
228 case CRM_Core_Permission
::CREATE
:
229 if (self
::check('profile create')) {
234 case CRM_Core_Permission
::EDIT
:
235 if (self
::check('profile edit')) {
240 case CRM_Core_Permission
::SEARCH
:
241 if (self
::check('profile listings')) {
247 return CRM_ACL_API
::group($type, NULL, 'civicrm_uf_group', $ufGroups);
250 static function ufGroupClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL, $returnUFGroupIds = FALSE) {
251 $groups = self
::ufGroup($type);
252 if ($returnUFGroupIds) {
255 elseif (empty($groups)) {
259 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
263 public static function event($type = CRM_Core_Permission
::VIEW
, $eventID = NULL) {
264 $events = CRM_Event_PseudoConstant
::event(NULL, TRUE);
265 $includeEvents = array();
267 // check if user has all powerful permission
268 if (self
::check('register for events')) {
269 $includeEvents = array_keys($events);
272 if ($type == CRM_Core_Permission
::VIEW
&&
273 self
::check('view event info')
275 $includeEvents = array_keys($events);
278 $permissionedEvents = CRM_ACL_API
::group($type, NULL, 'civicrm_event', $events, $includeEvents);
280 return $permissionedEvents;
282 return array_search($eventID, $permissionedEvents) === FALSE ?
NULL : $eventID;
285 static function eventClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL) {
286 $events = self
::event($type);
287 if (empty($events)) {
291 return "{$prefix}id IN ( " . implode(',', $events) . ' ) ';
295 static function access($module, $checkPermission = TRUE) {
296 $config = CRM_Core_Config
::singleton();
298 if (!in_array($module, $config->enableComponents
)) {
302 if ($checkPermission) {
303 if ($module == 'CiviCase') {
304 return CRM_Case_BAO_Case
::accessCiviCase();
307 return CRM_Core_Permission
::check("access $module");
315 * check permissions for delete and edit actions
317 * @param string $module component name.
318 * @param $action action to be check across component
321 static function checkActionPermission($module, $action) {
322 //check delete related permissions.
323 if ($action & CRM_Core_Action
::DELETE
) {
324 $permissionName = "delete in $module";
327 $editPermissions = array(
328 'CiviEvent' => 'edit event participants',
329 'CiviMember' => 'edit memberships',
330 'CiviPledge' => 'edit pledges',
331 'CiviContribute' => 'edit contributions',
332 'CiviGrant' => 'edit grants',
333 'CiviMail' => 'access CiviMail',
334 'CiviAuction' => 'add auction items',
336 $permissionName = CRM_Utils_Array
::value($module, $editPermissions);
339 if ($module == 'CiviCase' && !$permissionName) {
340 return CRM_Case_BAO_Case
::accessCiviCase();
343 //check for permission.
344 return CRM_Core_Permission
::check($permissionName);
348 static function checkMenu(&$args, $op = 'and') {
349 if (!is_array($args)) {
352 foreach ($args as $str) {
353 $res = CRM_Core_Permission
::check($str);
354 if ($op == 'or' && $res) {
357 elseif ($op == 'and' && !$res) {
361 return ($op == 'or') ?
FALSE : TRUE;
364 static function checkMenuItem(&$item) {
365 if (!array_key_exists('access_callback', $item)) {
366 CRM_Core_Error
::backtrace();
367 CRM_Core_Error
::fatal();
370 // if component_id is present, ensure it is enabled
371 if (isset($item['component_id']) &&
372 $item['component_id']
374 $config = CRM_Core_Config
::singleton();
375 if (is_array($config->enableComponentIDs
) &&
376 in_array($item['component_id'], $config->enableComponentIDs
)
378 // continue with process
385 // the following is imitating drupal 6 code in includes/menu.inc
386 if (empty($item['access_callback']) ||
387 is_numeric($item['access_callback'])
389 return (boolean
) $item['access_callback'];
392 // check whether the following Ajax requests submitted the right key
393 // FIXME: this should be integrated into ACLs proper
394 if (CRM_Utils_Array
::value('page_type', $item) == 3) {
395 if (!CRM_Core_Key
::validate($_REQUEST['key'], $item['path'])) {
400 // check if callback is for checkMenu, if so optimize it
401 if (is_array($item['access_callback']) &&
402 $item['access_callback'][0] == 'CRM_Core_Permission' &&
403 $item['access_callback'][1] == 'checkMenu'
405 $op = CRM_Utils_Array
::value(1, $item['access_arguments'], 'and');
406 return self
::checkMenu($item['access_arguments'][0], $op);
409 return call_user_func_array($item['access_callback'], $item['access_arguments']);
413 static function &basicPermissions($all = FALSE) {
414 static $permissions = NULL;
417 $prefix = ts('CiviCRM') . ': ';
418 $permissions = self
::getCorePermissions();
420 if (self
::isMultisiteEnabled()) {
421 $permissions['administer Multiple Organizations'] = $prefix . ts('administer Multiple Organizations');
424 $config = CRM_Core_Config
::singleton();
427 $components = CRM_Core_Component
::getEnabledComponents();
430 $components = CRM_Core_Component
::getComponents();
433 foreach ($components as $comp) {
434 $perm = $comp->getPermissions();
436 $info = $comp->getInfo();
437 foreach ($perm as $p) {
438 $permissions[$p] = $info['translatedName'] . ': ' . $p;
443 // Add any permissions defined in hook_civicrm_permission implementations.
444 $config = CRM_Core_Config
::singleton();
445 $module_permissions = $config->userPermissionClass
->getAllModulePermissions();
446 $permissions = array_merge($permissions, $module_permissions);
452 static function getCorePermissions() {
453 $prefix = ts('CiviCRM') . ': ';
454 $permissions = array(
455 'add contacts' => $prefix . ts('add contacts'),
456 'view all contacts' => $prefix . ts('view all contacts'),
457 'edit all contacts' => $prefix . ts('edit all contacts'),
458 'view my contact' => $prefix . ts('view my contact'),
459 'edit my contact' => $prefix . ts('edit my contact'),
460 'delete contacts' => $prefix . ts('delete contacts'),
461 'access deleted contacts' => $prefix . ts('access deleted contacts'),
462 'import contacts' => $prefix . ts('import contacts'),
463 'edit groups' => $prefix . ts('edit groups'),
464 'administer CiviCRM' => $prefix . ts('administer CiviCRM'),
465 'skip IDS check' => $prefix . ts('skip IDS check'),
466 'access uploaded files' => $prefix . ts('access uploaded files'),
467 'profile listings and forms' => $prefix . ts('profile listings and forms'),
468 'profile listings' => $prefix . ts('profile listings'),
469 'profile create' => $prefix . ts('profile create'),
470 'profile edit' => $prefix . ts('profile edit'),
471 'profile view' => $prefix . ts('profile view'),
472 'access all custom data' => $prefix . ts('access all custom data'),
473 'view all activities' => $prefix . ts('view all activities'),
474 'delete activities' => $prefix . ts('delete activities'),
475 'access CiviCRM' => $prefix . ts('access CiviCRM'),
476 'access Contact Dashboard' => $prefix . ts('access Contact Dashboard'),
477 'translate CiviCRM' => $prefix . ts('translate CiviCRM'),
478 'administer reserved groups' => $prefix . ts('administer reserved groups'),
479 'administer Tagsets' => $prefix . ts('administer Tagsets'),
480 'administer reserved tags' => $prefix . ts('administer reserved tags'),
481 'administer dedupe rules' => $prefix . ts('administer dedupe rules'),
482 'merge duplicate contacts' => $prefix . ts('merge duplicate contacts'),
483 'view debug output' => $prefix . ts('view debug output'),
484 'view all notes' => $prefix . ts('view all notes'),
485 'access AJAX API' => $prefix . ts('access AJAX API'),
486 'access contact reference fields' => $prefix . ts('access contact reference fields'),
487 'create manual batch' => $prefix . ts('create manual batch'),
488 'edit own manual batches' => $prefix . ts('edit own manual batches'),
489 'edit all manual batches' => $prefix . ts('edit all manual batches'),
490 'view own manual batches' => $prefix . ts('view own manual batches'),
491 'view all manual batches' => $prefix . ts('view all manual batches'),
492 'delete own manual batches' => $prefix . ts('delete own manual batches'),
493 'delete all manual batches' => $prefix . ts('delete all manual batches'),
494 'export own manual batches' => $prefix . ts('export own manual batches'),
495 'export all manual batches' => $prefix . ts('export all manual batches'),
502 * Validate user permission across
503 * edit or view or with supportable acls.
505 * return boolean true/false.
507 static function giveMeAllACLs() {
508 if (CRM_Core_Permission
::check('view all contacts') ||
509 CRM_Core_Permission
::check('edit all contacts')
514 $session = CRM_Core_Session
::singleton();
515 $contactID = $session->get('userID');
518 $aclPermission = self
::getPermission();
519 if (in_array($aclPermission, array(
520 CRM_Core_Permission
::EDIT
,
521 CRM_Core_Permission
::VIEW
,
526 // run acl where hook and see if the user is supplying an ACL clause
528 $tables = $whereTables = array();
531 CRM_Utils_Hook
::aclWhereClause(CRM_Core_Permission
::VIEW
,
532 $tables, $whereTables,
535 return empty($whereTables) ?
FALSE : TRUE;
539 * Function to get component name from given permission.
541 * @param string $permission
543 * return string $componentName the name of component.
546 static function getComponentName($permission) {
547 $componentName = NULL;
548 $permission = trim($permission);
549 if (empty($permission)) {
550 return $componentName;
553 static $allCompPermissions = array();
554 if (empty($allCompPermissions)) {
555 $components = CRM_Core_Component
::getComponents();
556 foreach ($components as $name => $comp) {
557 //get all permissions of each components unconditionally
558 $allCompPermissions[$name] = $comp->getPermissions(TRUE);
562 if (is_array($allCompPermissions)) {
563 foreach ($allCompPermissions as $name => $permissions) {
564 if (in_array($permission, $permissions)) {
565 $componentName = $name;
571 return $componentName;
575 * Get all the contact emails for users that have a specific permission
577 * @param string $permissionName name of the permission we are interested in
579 * @return string a comma separated list of email addresses
581 public static function permissionEmails($permissionName) {
582 $config = CRM_Core_Config
::singleton();
583 return $config->userPermissionClass
->permissionEmails( $permissionName );
587 * Get all the contact emails for users that have a specific role
589 * @param string $roleName name of the role we are interested in
591 * @return string a comma separated list of email addresses
593 public static function roleEmails($roleName) {
594 $config = CRM_Core_Config
::singleton();
595 return $config->userRoleClass
->roleEmails( $roleName );
598 static function isMultisiteEnabled() {
599 return CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,