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 if (!empty($permissionedEvents)){
283 return array_search($eventID, $permissionedEvents) === FALSE ?
NULL : $eventID;
289 static function eventClause($type = CRM_Core_Permission
::VIEW
, $prefix = NULL) {
290 $events = self
::event($type);
291 if (empty($events)) {
295 return "{$prefix}id IN ( " . implode(',', $events) . ' ) ';
299 static function access($module, $checkPermission = TRUE) {
300 $config = CRM_Core_Config
::singleton();
302 if (!in_array($module, $config->enableComponents
)) {
306 if ($checkPermission) {
307 if ($module == 'CiviCase') {
308 return CRM_Case_BAO_Case
::accessCiviCase();
311 return CRM_Core_Permission
::check("access $module");
319 * check permissions for delete and edit actions
321 * @param string $module component name.
322 * @param $action action to be check across component
325 static function checkActionPermission($module, $action) {
326 //check delete related permissions.
327 if ($action & CRM_Core_Action
::DELETE
) {
328 $permissionName = "delete in $module";
331 $editPermissions = array(
332 'CiviEvent' => 'edit event participants',
333 'CiviMember' => 'edit memberships',
334 'CiviPledge' => 'edit pledges',
335 'CiviContribute' => 'edit contributions',
336 'CiviGrant' => 'edit grants',
337 'CiviMail' => 'access CiviMail',
338 'CiviAuction' => 'add auction items',
340 $permissionName = CRM_Utils_Array
::value($module, $editPermissions);
343 if ($module == 'CiviCase' && !$permissionName) {
344 return CRM_Case_BAO_Case
::accessCiviCase();
347 //check for permission.
348 return CRM_Core_Permission
::check($permissionName);
352 static function checkMenu(&$args, $op = 'and') {
353 if (!is_array($args)) {
356 foreach ($args as $str) {
357 $res = CRM_Core_Permission
::check($str);
358 if ($op == 'or' && $res) {
361 elseif ($op == 'and' && !$res) {
365 return ($op == 'or') ?
FALSE : TRUE;
368 static function checkMenuItem(&$item) {
369 if (!array_key_exists('access_callback', $item)) {
370 CRM_Core_Error
::backtrace();
371 CRM_Core_Error
::fatal();
374 // if component_id is present, ensure it is enabled
375 if (isset($item['component_id']) &&
376 $item['component_id']
378 $config = CRM_Core_Config
::singleton();
379 if (is_array($config->enableComponentIDs
) &&
380 in_array($item['component_id'], $config->enableComponentIDs
)
382 // continue with process
389 // the following is imitating drupal 6 code in includes/menu.inc
390 if (empty($item['access_callback']) ||
391 is_numeric($item['access_callback'])
393 return (boolean
) $item['access_callback'];
396 // check whether the following Ajax requests submitted the right key
397 // FIXME: this should be integrated into ACLs proper
398 if (CRM_Utils_Array
::value('page_type', $item) == 3) {
399 if (!CRM_Core_Key
::validate($_REQUEST['key'], $item['path'])) {
404 // check if callback is for checkMenu, if so optimize it
405 if (is_array($item['access_callback']) &&
406 $item['access_callback'][0] == 'CRM_Core_Permission' &&
407 $item['access_callback'][1] == 'checkMenu'
409 $op = CRM_Utils_Array
::value(1, $item['access_arguments'], 'and');
410 return self
::checkMenu($item['access_arguments'][0], $op);
413 return call_user_func_array($item['access_callback'], $item['access_arguments']);
417 static function &basicPermissions($all = FALSE) {
418 static $permissions = NULL;
421 $prefix = ts('CiviCRM') . ': ';
422 $permissions = self
::getCorePermissions();
424 if (self
::isMultisiteEnabled()) {
425 $permissions['administer Multiple Organizations'] = $prefix . ts('administer Multiple Organizations');
428 $config = CRM_Core_Config
::singleton();
431 $components = CRM_Core_Component
::getEnabledComponents();
434 $components = CRM_Core_Component
::getComponents();
437 foreach ($components as $comp) {
438 $perm = $comp->getPermissions();
440 $info = $comp->getInfo();
441 foreach ($perm as $p) {
442 $permissions[$p] = $info['translatedName'] . ': ' . $p;
447 // Add any permissions defined in hook_civicrm_permission implementations.
448 $config = CRM_Core_Config
::singleton();
449 $module_permissions = $config->userPermissionClass
->getAllModulePermissions();
450 $permissions = array_merge($permissions, $module_permissions);
456 static function getCorePermissions() {
457 $prefix = ts('CiviCRM') . ': ';
458 $permissions = array(
459 'add contacts' => $prefix . ts('add contacts'),
460 'view all contacts' => $prefix . ts('view all contacts'),
461 'edit all contacts' => $prefix . ts('edit all contacts'),
462 'view my contact' => $prefix . ts('view my contact'),
463 'edit my contact' => $prefix . ts('edit my contact'),
464 'delete contacts' => $prefix . ts('delete contacts'),
465 'access deleted contacts' => $prefix . ts('access deleted contacts'),
466 'import contacts' => $prefix . ts('import contacts'),
467 'edit groups' => $prefix . ts('edit groups'),
468 'administer CiviCRM' => $prefix . ts('administer CiviCRM'),
469 'skip IDS check' => $prefix . ts('skip IDS check'),
470 'access uploaded files' => $prefix . ts('access uploaded files'),
471 'profile listings and forms' => $prefix . ts('profile listings and forms'),
472 'profile listings' => $prefix . ts('profile listings'),
473 'profile create' => $prefix . ts('profile create'),
474 'profile edit' => $prefix . ts('profile edit'),
475 'profile view' => $prefix . ts('profile view'),
476 'access all custom data' => $prefix . ts('access all custom data'),
477 'view all activities' => $prefix . ts('view all activities'),
478 'delete activities' => $prefix . ts('delete activities'),
479 'access CiviCRM' => $prefix . ts('access CiviCRM'),
480 'access Contact Dashboard' => $prefix . ts('access Contact Dashboard'),
481 'translate CiviCRM' => $prefix . ts('translate CiviCRM'),
482 'administer reserved groups' => $prefix . ts('administer reserved groups'),
483 'administer Tagsets' => $prefix . ts('administer Tagsets'),
484 'administer reserved tags' => $prefix . ts('administer reserved tags'),
485 'administer dedupe rules' => $prefix . ts('administer dedupe rules'),
486 'merge duplicate contacts' => $prefix . ts('merge duplicate contacts'),
487 'view debug output' => $prefix . ts('view debug output'),
488 'view all notes' => $prefix . ts('view all notes'),
489 'access AJAX API' => $prefix . ts('access AJAX API'),
490 'access contact reference fields' => $prefix . ts('access contact reference fields'),
491 'create manual batch' => $prefix . ts('create manual batch'),
492 'edit own manual batches' => $prefix . ts('edit own manual batches'),
493 'edit all manual batches' => $prefix . ts('edit all manual batches'),
494 'view own manual batches' => $prefix . ts('view own manual batches'),
495 'view all manual batches' => $prefix . ts('view all manual batches'),
496 'delete own manual batches' => $prefix . ts('delete own manual batches'),
497 'delete all manual batches' => $prefix . ts('delete all manual batches'),
498 'export own manual batches' => $prefix . ts('export own manual batches'),
499 'export all manual batches' => $prefix . ts('export all manual batches'),
506 * Validate user permission across
507 * edit or view or with supportable acls.
509 * return boolean true/false.
511 static function giveMeAllACLs() {
512 if (CRM_Core_Permission
::check('view all contacts') ||
513 CRM_Core_Permission
::check('edit all contacts')
518 $session = CRM_Core_Session
::singleton();
519 $contactID = $session->get('userID');
522 $aclPermission = self
::getPermission();
523 if (in_array($aclPermission, array(
524 CRM_Core_Permission
::EDIT
,
525 CRM_Core_Permission
::VIEW
,
530 // run acl where hook and see if the user is supplying an ACL clause
532 $tables = $whereTables = array();
535 CRM_Utils_Hook
::aclWhereClause(CRM_Core_Permission
::VIEW
,
536 $tables, $whereTables,
539 return empty($whereTables) ?
FALSE : TRUE;
543 * Function to get component name from given permission.
545 * @param string $permission
547 * return string $componentName the name of component.
550 static function getComponentName($permission) {
551 $componentName = NULL;
552 $permission = trim($permission);
553 if (empty($permission)) {
554 return $componentName;
557 static $allCompPermissions = array();
558 if (empty($allCompPermissions)) {
559 $components = CRM_Core_Component
::getComponents();
560 foreach ($components as $name => $comp) {
561 //get all permissions of each components unconditionally
562 $allCompPermissions[$name] = $comp->getPermissions(TRUE);
566 if (is_array($allCompPermissions)) {
567 foreach ($allCompPermissions as $name => $permissions) {
568 if (in_array($permission, $permissions)) {
569 $componentName = $name;
575 return $componentName;
579 * Get all the contact emails for users that have a specific permission
581 * @param string $permissionName name of the permission we are interested in
583 * @return string a comma separated list of email addresses
585 public static function permissionEmails($permissionName) {
586 $config = CRM_Core_Config
::singleton();
587 return $config->userPermissionClass
->permissionEmails( $permissionName );
591 * Get all the contact emails for users that have a specific role
593 * @param string $roleName name of the role we are interested in
595 * @return string a comma separated list of email addresses
597 public static function roleEmails($roleName) {
598 $config = CRM_Core_Config
::singleton();
599 return $config->userRoleClass
->roleEmails( $roleName );
602 static function isMultisiteEnabled() {
603 return CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,