639308bf87289e71b82929def8433e6c820d7405
[civicrm-core.git] / CRM / Core / Permission.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This is the basic permission class wrapper
38 */
39 class CRM_Core_Permission {
40
41 /**
42 * Static strings used to compose permissions
43 *
44 * @const
45 * @var string
46 */
47 CONST EDIT_GROUPS = 'edit contacts in ', VIEW_GROUPS = 'view contacts in ';
48
49 /**
50 * The various type of permissions
51 *
52 * @var int
53 */
54 CONST EDIT = 1, VIEW = 2, DELETE = 3, CREATE = 4, SEARCH = 5, ALL = 6, ADMIN = 7;
55
56 /**
57 * A placeholder permission which always fails
58 */
59 const ALWAYS_DENY_PERMISSION = "*always deny*";
60
61 /**
62 * A placeholder permission which always fails
63 */
64 const ALWAYS_ALLOW_PERMISSION = "*always allow*";
65
66 /**
67 * get the current permission of this user
68 *
69 * @return string the permission of the user (edit or view or null)
70 */
71 public static function getPermission() {
72 $config = CRM_Core_Config::singleton();
73 return $config->userPermissionClass->getPermission( );
74 }
75
76 /**
77 * given a permission string, check for access requirements
78 *
79 * @param string $str the permission to check
80 *
81 * @return boolean true if yes, else false
82 * @static
83 * @access public
84 */
85 static function check($str) {
86 $config = CRM_Core_Config::singleton();
87 return $config->userPermissionClass->check( $str );
88 }
89
90 /**
91 * Determine if any one of the permissions strings applies to current user
92 *
93 * @param array $perms
94 * @return bool
95 */
96 public static function checkAnyPerm($perms) {
97 foreach ($perms as $perm) {
98 if (CRM_Core_Permission::check($perm)) {
99 return TRUE;
100 }
101 }
102 return FALSE;
103 }
104
105 /**
106 * Given a group/role array, check for access requirements
107 *
108 * @param array $array the group/role to check
109 *
110 * @return boolean true if yes, else false
111 * @static
112 * @access public
113 */
114 static function checkGroupRole($array) {
115 $config = CRM_Core_Config::singleton();
116 return $config->userPermissionClass->checkGroupRole( $array );
117 }
118
119 /**
120 * Get the permissioned where clause for the user
121 *
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
125 *
126 * @return string the group where clause for this user
127 * @access public
128 */
129 public static function getPermissionedStaticGroupClause($type, &$tables, &$whereTables) {
130 $config = CRM_Core_Config::singleton();
131 return $config->userPermissionClass->getPermissionedStaticGroupClause( $type, $tables, $whereTables );
132 }
133
134 /**
135 * Get all groups from database, filtered by permissions
136 * for this user
137 *
138 * @param string $groupType type of group(Access/Mailing)
139 * @param boolen $excludeHidden exclude hidden groups.
140 *
141 * @access public
142 * @static
143 *
144 * @return array - array reference of all groups.
145 *
146 */
147 public static function group($groupType, $excludeHidden = TRUE) {
148 $config = CRM_Core_Config::singleton();
149 return $config->userPermissionClass->group( $groupType, $excludeHidden );
150 }
151
152 public static function customGroupAdmin() {
153 $admin = FALSE;
154
155 // check if user has all powerful permission
156 // or administer civicrm permission (CRM-1905)
157 if (self::check('access all custom data')) {
158 return TRUE;
159 }
160
161 if (self::check('administer Multiple Organizations') &&
162 self::isMultisiteEnabled()
163 ) {
164 return TRUE;
165 }
166
167 if (self::check('administer CiviCRM')) {
168 return TRUE;
169 }
170
171 return FALSE;
172 }
173
174 public static function customGroup($type = CRM_Core_Permission::VIEW, $reset = FALSE) {
175 $customGroups = CRM_Core_PseudoConstant::customGroup($reset);
176 $defaultGroups = array();
177
178 // check if user has all powerful permission
179 // or administer civicrm permission (CRM-1905)
180 if (self::customGroupAdmin()) {
181 $defaultGroups = array_keys($customGroups);
182 }
183
184 return CRM_ACL_API::group($type, NULL, 'civicrm_custom_group', $customGroups, $defaultGroups);
185 }
186
187 static function customGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $reset = FALSE) {
188 if (self::customGroupAdmin()) {
189 return ' ( 1 ) ';
190 }
191
192 $groups = self::customGroup($type, $reset);
193 if (empty($groups)) {
194 return ' ( 0 ) ';
195 }
196 else {
197 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
198 }
199 }
200
201 public static function ufGroupValid($gid, $type = CRM_Core_Permission::VIEW) {
202 if (empty($gid)) {
203 return TRUE;
204 }
205
206 $groups = self::ufGroup($type);
207 return in_array($gid, $groups) ? TRUE : FALSE;
208 }
209
210 public static function ufGroup($type = CRM_Core_Permission::VIEW) {
211 $ufGroups = CRM_Core_PseudoConstant::ufGroup();
212
213 $allGroups = array_keys($ufGroups);
214
215 // check if user has all powerful permission
216 if (self::check('profile listings and forms')) {
217 return $allGroups;
218 }
219
220 switch ($type) {
221 case CRM_Core_Permission::VIEW:
222 if (self::check('profile view')) {
223 return $allGroups;
224 }
225 break;
226
227 case CRM_Core_Permission::CREATE:
228 if (self::check('profile create')) {
229 return $allGroups;
230 }
231 break;
232
233 case CRM_Core_Permission::EDIT:
234 if (self::check('profile edit')) {
235 return $allGroups;
236 }
237 break;
238
239 case CRM_Core_Permission::SEARCH:
240 if (self::check('profile listings')) {
241 return $allGroups;
242 }
243 break;
244 }
245
246 return CRM_ACL_API::group($type, NULL, 'civicrm_uf_group', $ufGroups);
247 }
248
249 static function ufGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $returnUFGroupIds = FALSE) {
250 $groups = self::ufGroup($type);
251 if ($returnUFGroupIds) {
252 return $groups;
253 }
254 elseif (empty($groups)) {
255 return ' ( 0 ) ';
256 }
257 else {
258 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
259 }
260 }
261
262 public static function event($type = CRM_Core_Permission::VIEW, $eventID = NULL) {
263 $events = CRM_Event_PseudoConstant::event(NULL, TRUE);
264 $includeEvents = array();
265
266 // check if user has all powerful permission
267 if (self::check('register for events')) {
268 $includeEvents = array_keys($events);
269 }
270
271 if ($type == CRM_Core_Permission::VIEW &&
272 self::check('view event info')
273 ) {
274 $includeEvents = array_keys($events);
275 }
276
277 $permissionedEvents = CRM_ACL_API::group($type, NULL, 'civicrm_event', $events, $includeEvents);
278 if (!$eventID) {
279 return $permissionedEvents;
280 }
281 return array_search($eventID, $permissionedEvents) === FALSE ? NULL : $eventID;
282 }
283
284 static function eventClause($type = CRM_Core_Permission::VIEW, $prefix = NULL) {
285 $events = self::event($type);
286 if (empty($events)) {
287 return ' ( 0 ) ';
288 }
289 else {
290 return "{$prefix}id IN ( " . implode(',', $events) . ' ) ';
291 }
292 }
293
294 static function access($module, $checkPermission = TRUE) {
295 $config = CRM_Core_Config::singleton();
296
297 if (!in_array($module, $config->enableComponents)) {
298 return FALSE;
299 }
300
301 if ($checkPermission) {
302 if ($module == 'CiviCase') {
303 return CRM_Case_BAO_Case::accessCiviCase();
304 }
305 else {
306 return CRM_Core_Permission::check("access $module");
307 }
308 }
309
310 return TRUE;
311 }
312
313 /**
314 * check permissions for delete and edit actions
315 *
316 * @param string $module component name.
317 * @param $action action to be check across component
318 *
319 **/
320 static function checkActionPermission($module, $action) {
321 //check delete related permissions.
322 if ($action & CRM_Core_Action::DELETE) {
323 $permissionName = "delete in $module";
324 }
325 else {
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',
334 );
335 $permissionName = CRM_Utils_Array::value($module, $editPermissions);
336 }
337
338 if ($module == 'CiviCase' && !$permissionName) {
339 return CRM_Case_BAO_Case::accessCiviCase();
340 }
341 else {
342 //check for permission.
343 return CRM_Core_Permission::check($permissionName);
344 }
345 }
346
347 static function checkMenu(&$args, $op = 'and') {
348 if (!is_array($args)) {
349 return $args;
350 }
351 foreach ($args as $str) {
352 $res = CRM_Core_Permission::check($str);
353 if ($op == 'or' && $res) {
354 return TRUE;
355 }
356 elseif ($op == 'and' && !$res) {
357 return FALSE;
358 }
359 }
360 return ($op == 'or') ? FALSE : TRUE;
361 }
362
363 static function checkMenuItem(&$item) {
364 if (!array_key_exists('access_callback', $item)) {
365 CRM_Core_Error::backtrace();
366 CRM_Core_Error::fatal();
367 }
368
369 // if component_id is present, ensure it is enabled
370 if (isset($item['component_id']) &&
371 $item['component_id']
372 ) {
373 $config = CRM_Core_Config::singleton();
374 if (is_array($config->enableComponentIDs) &&
375 in_array($item['component_id'], $config->enableComponentIDs)
376 ) {
377 // continue with process
378 }
379 else {
380 return FALSE;
381 }
382 }
383
384 // the following is imitating drupal 6 code in includes/menu.inc
385 if (empty($item['access_callback']) ||
386 is_numeric($item['access_callback'])
387 ) {
388 return (boolean ) $item['access_callback'];
389 }
390
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'])) {
395 return FALSE;
396 }
397 }
398
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'
403 ) {
404 $op = CRM_Utils_Array::value(1, $item['access_arguments'], 'and');
405 return self::checkMenu($item['access_arguments'][0], $op);
406 }
407 else {
408 return call_user_func_array($item['access_callback'], $item['access_arguments']);
409 }
410 }
411
412 static function &basicPermissions($all = FALSE) {
413 static $permissions = NULL;
414
415 if (!$permissions) {
416 $prefix = ts('CiviCRM') . ': ';
417 $permissions = self::getCorePermissions();
418
419 if (self::isMultisiteEnabled()) {
420 $permissions['administer Multiple Organizations'] = $prefix . ts('administer Multiple Organizations');
421 }
422
423 $config = CRM_Core_Config::singleton();
424
425 if (!$all) {
426 $components = CRM_Core_Component::getEnabledComponents();
427 }
428 else {
429 $components = CRM_Core_Component::getComponents();
430 }
431
432 foreach ($components as $comp) {
433 $perm = $comp->getPermissions();
434 if ($perm) {
435 $info = $comp->getInfo();
436 foreach ($perm as $p) {
437 $permissions[$p] = $info['translatedName'] . ': ' . $p;
438 }
439 }
440 }
441
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);
446 }
447
448 return $permissions;
449 }
450
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'),
492 );
493
494 return $permissions;
495 }
496
497 /**
498 * Validate user permission across
499 * edit or view or with supportable acls.
500 *
501 * return boolean true/false.
502 **/
503 static function giveMeAllACLs() {
504 if (CRM_Core_Permission::check('view all contacts') ||
505 CRM_Core_Permission::check('edit all contacts')
506 ) {
507 return TRUE;
508 }
509
510 $session = CRM_Core_Session::singleton();
511 $contactID = $session->get('userID');
512
513 //check for acl.
514 $aclPermission = self::getPermission();
515 if (in_array($aclPermission, array(
516 CRM_Core_Permission::EDIT,
517 CRM_Core_Permission::VIEW,
518 ))) {
519 return TRUE;
520 }
521
522 // run acl where hook and see if the user is supplying an ACL clause
523 // that is not false
524 $tables = $whereTables = array();
525 $where = NULL;
526
527 CRM_Utils_Hook::aclWhereClause(CRM_Core_Permission::VIEW,
528 $tables, $whereTables,
529 $contactID, $where
530 );
531 return empty($whereTables) ? FALSE : TRUE;
532 }
533
534 /**
535 * Function to get component name from given permission.
536 *
537 * @param string $permission
538 *
539 * return string $componentName the name of component.
540 * @static
541 */
542 static function getComponentName($permission) {
543 $componentName = NULL;
544 $permission = trim($permission);
545 if (empty($permission)) {
546 return $componentName;
547 }
548
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);
555 }
556 }
557
558 if (is_array($allCompPermissions)) {
559 foreach ($allCompPermissions as $name => $permissions) {
560 if (in_array($permission, $permissions)) {
561 $componentName = $name;
562 break;
563 }
564 }
565 }
566
567 return $componentName;
568 }
569
570 /**
571 * Get all the contact emails for users that have a specific permission
572 *
573 * @param string $permissionName name of the permission we are interested in
574 *
575 * @return string a comma separated list of email addresses
576 */
577 public static function permissionEmails($permissionName) {
578 $config = CRM_Core_Config::singleton();
579 return $config->userPermissionClass->permissionEmails( $permissionName );
580 }
581
582 /**
583 * Get all the contact emails for users that have a specific role
584 *
585 * @param string $roleName name of the role we are interested in
586 *
587 * @return string a comma separated list of email addresses
588 */
589 public static function roleEmails($roleName) {
590 $config = CRM_Core_Config::singleton();
591 return $config->userRoleClass->roleEmails( $roleName );
592 }
593
594 static function isMultisiteEnabled() {
595 return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
596 'is_enabled'
597 ) ? TRUE : FALSE;
598 }
599 }
600