Merge pull request #1964 from dlobo/CRM-13746
[civicrm-core.git] / CRM / Core / Permission.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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 */
39class 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
085823c1
TO
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
6dd18b98
DS
66 /**
67 * Various authentication sources
68 *
69 * @var int
70 */
e8f14831 71 CONST AUTH_SRC_UNKNOWN = 0, AUTH_SRC_CHECKSUM = 1, AUTH_SRC_SITEKEY = 2, AUTH_SRC_LOGIN = 4;
6dd18b98 72
6a488035
TO
73 /**
74 * get the current permission of this user
75 *
76 * @return string the permission of the user (edit or view or null)
77 */
78 public static function getPermission() {
79 $config = CRM_Core_Config::singleton();
41f314b6 80 return $config->userPermissionClass->getPermission();
6a488035
TO
81 }
82
83 /**
84 * given a permission string, check for access requirements
85 *
86 * @param string $str the permission to check
87 *
88 * @return boolean true if yes, else false
89 * @static
90 * @access public
91 */
92 static function check($str) {
93 $config = CRM_Core_Config::singleton();
41f314b6 94 return $config->userPermissionClass->check($str);
6a488035
TO
95 }
96
dc92f2f8
TO
97 /**
98 * Determine if any one of the permissions strings applies to current user
99 *
100 * @param array $perms
101 * @return bool
102 */
103 public static function checkAnyPerm($perms) {
104 foreach ($perms as $perm) {
105 if (CRM_Core_Permission::check($perm)) {
106 return TRUE;
107 }
108 }
109 return FALSE;
110 }
111
6a488035
TO
112 /**
113 * Given a group/role array, check for access requirements
114 *
115 * @param array $array the group/role to check
116 *
117 * @return boolean true if yes, else false
118 * @static
119 * @access public
120 */
121 static function checkGroupRole($array) {
122 $config = CRM_Core_Config::singleton();
41f314b6 123 return $config->userPermissionClass->checkGroupRole($array);
6a488035
TO
124 }
125
126 /**
127 * Get the permissioned where clause for the user
128 *
129 * @param int $type the type of permission needed
130 * @param array $tables (reference ) add the tables that are needed for the select clause
131 * @param array $whereTables (reference ) add the tables that are needed for the where clause
132 *
133 * @return string the group where clause for this user
134 * @access public
135 */
136 public static function getPermissionedStaticGroupClause($type, &$tables, &$whereTables) {
137 $config = CRM_Core_Config::singleton();
41f314b6 138 return $config->userPermissionClass->getPermissionedStaticGroupClause($type, $tables, $whereTables);
6a488035
TO
139 }
140
141 /**
142 * Get all groups from database, filtered by permissions
143 * for this user
144 *
145 * @param string $groupType type of group(Access/Mailing)
146 * @param boolen $excludeHidden exclude hidden groups.
147 *
148 * @access public
149 * @static
150 *
151 * @return array - array reference of all groups.
152 *
153 */
154 public static function group($groupType, $excludeHidden = TRUE) {
155 $config = CRM_Core_Config::singleton();
41f314b6 156 return $config->userPermissionClass->group($groupType, $excludeHidden);
6a488035
TO
157 }
158
159 public static function customGroupAdmin() {
160 $admin = FALSE;
161
162 // check if user has all powerful permission
163 // or administer civicrm permission (CRM-1905)
164 if (self::check('access all custom data')) {
165 return TRUE;
166 }
167
634e1a1a
DL
168 if (
169 self::check('administer Multiple Organizations') &&
6a488035
TO
170 self::isMultisiteEnabled()
171 ) {
172 return TRUE;
173 }
174
175 if (self::check('administer CiviCRM')) {
176 return TRUE;
177 }
178
179 return FALSE;
180 }
181
182 public static function customGroup($type = CRM_Core_Permission::VIEW, $reset = FALSE) {
41f314b6 183 $customGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id',
184 array('fresh' => $reset));
6a488035
TO
185 $defaultGroups = array();
186
187 // check if user has all powerful permission
188 // or administer civicrm permission (CRM-1905)
189 if (self::customGroupAdmin()) {
190 $defaultGroups = array_keys($customGroups);
191 }
192
193 return CRM_ACL_API::group($type, NULL, 'civicrm_custom_group', $customGroups, $defaultGroups);
194 }
195
196 static function customGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $reset = FALSE) {
197 if (self::customGroupAdmin()) {
198 return ' ( 1 ) ';
199 }
200
201 $groups = self::customGroup($type, $reset);
202 if (empty($groups)) {
203 return ' ( 0 ) ';
204 }
205 else {
206 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
207 }
208 }
209
210 public static function ufGroupValid($gid, $type = CRM_Core_Permission::VIEW) {
211 if (empty($gid)) {
212 return TRUE;
213 }
214
215 $groups = self::ufGroup($type);
216 return in_array($gid, $groups) ? TRUE : FALSE;
217 }
218
219 public static function ufGroup($type = CRM_Core_Permission::VIEW) {
ff4f7744 220 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
6a488035
TO
221
222 $allGroups = array_keys($ufGroups);
223
224 // check if user has all powerful permission
225 if (self::check('profile listings and forms')) {
226 return $allGroups;
227 }
228
229 switch ($type) {
230 case CRM_Core_Permission::VIEW:
231 if (self::check('profile view')) {
232 return $allGroups;
233 }
234 break;
235
236 case CRM_Core_Permission::CREATE:
237 if (self::check('profile create')) {
238 return $allGroups;
239 }
240 break;
241
242 case CRM_Core_Permission::EDIT:
243 if (self::check('profile edit')) {
244 return $allGroups;
245 }
246 break;
247
248 case CRM_Core_Permission::SEARCH:
249 if (self::check('profile listings')) {
250 return $allGroups;
251 }
252 break;
253 }
254
255 return CRM_ACL_API::group($type, NULL, 'civicrm_uf_group', $ufGroups);
256 }
257
258 static function ufGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $returnUFGroupIds = FALSE) {
259 $groups = self::ufGroup($type);
260 if ($returnUFGroupIds) {
261 return $groups;
262 }
263 elseif (empty($groups)) {
264 return ' ( 0 ) ';
265 }
266 else {
267 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
268 }
269 }
270
271 public static function event($type = CRM_Core_Permission::VIEW, $eventID = NULL) {
272 $events = CRM_Event_PseudoConstant::event(NULL, TRUE);
273 $includeEvents = array();
274
275 // check if user has all powerful permission
276 if (self::check('register for events')) {
277 $includeEvents = array_keys($events);
278 }
279
280 if ($type == CRM_Core_Permission::VIEW &&
281 self::check('view event info')
282 ) {
283 $includeEvents = array_keys($events);
284 }
285
286 $permissionedEvents = CRM_ACL_API::group($type, NULL, 'civicrm_event', $events, $includeEvents);
287 if (!$eventID) {
288 return $permissionedEvents;
289 }
41f314b6 290 if (!empty($permissionedEvents)) {
2efcf0c2 291 return array_search($eventID, $permissionedEvents) === FALSE ? NULL : $eventID;
41f314b6 292 }
293 else {
d551c85d
DG
294 return $eventID;
295 }
6a488035
TO
296 }
297
298 static function eventClause($type = CRM_Core_Permission::VIEW, $prefix = NULL) {
299 $events = self::event($type);
300 if (empty($events)) {
301 return ' ( 0 ) ';
302 }
303 else {
304 return "{$prefix}id IN ( " . implode(',', $events) . ' ) ';
305 }
306 }
307
308 static function access($module, $checkPermission = TRUE) {
309 $config = CRM_Core_Config::singleton();
310
311 if (!in_array($module, $config->enableComponents)) {
312 return FALSE;
313 }
314
315 if ($checkPermission) {
316 if ($module == 'CiviCase') {
317 return CRM_Case_BAO_Case::accessCiviCase();
318 }
319 else {
320 return CRM_Core_Permission::check("access $module");
321 }
322 }
323
324 return TRUE;
325 }
326
327 /**
328 * check permissions for delete and edit actions
329 *
41f314b6 330 * @param string $module component name.
6a488035
TO
331 * @param $action action to be check across component
332 *
333 **/
334 static function checkActionPermission($module, $action) {
335 //check delete related permissions.
336 if ($action & CRM_Core_Action::DELETE) {
337 $permissionName = "delete in $module";
338 }
339 else {
340 $editPermissions = array(
341 'CiviEvent' => 'edit event participants',
342 'CiviMember' => 'edit memberships',
343 'CiviPledge' => 'edit pledges',
344 'CiviContribute' => 'edit contributions',
345 'CiviGrant' => 'edit grants',
346 'CiviMail' => 'access CiviMail',
347 'CiviAuction' => 'add auction items',
348 );
349 $permissionName = CRM_Utils_Array::value($module, $editPermissions);
350 }
351
352 if ($module == 'CiviCase' && !$permissionName) {
353 return CRM_Case_BAO_Case::accessCiviCase();
354 }
355 else {
356 //check for permission.
357 return CRM_Core_Permission::check($permissionName);
358 }
359 }
360
361 static function checkMenu(&$args, $op = 'and') {
362 if (!is_array($args)) {
363 return $args;
364 }
365 foreach ($args as $str) {
366 $res = CRM_Core_Permission::check($str);
367 if ($op == 'or' && $res) {
368 return TRUE;
369 }
370 elseif ($op == 'and' && !$res) {
371 return FALSE;
372 }
373 }
374 return ($op == 'or') ? FALSE : TRUE;
375 }
376
377 static function checkMenuItem(&$item) {
378 if (!array_key_exists('access_callback', $item)) {
379 CRM_Core_Error::backtrace();
380 CRM_Core_Error::fatal();
381 }
382
383 // if component_id is present, ensure it is enabled
384 if (isset($item['component_id']) &&
385 $item['component_id']
386 ) {
387 $config = CRM_Core_Config::singleton();
388 if (is_array($config->enableComponentIDs) &&
389 in_array($item['component_id'], $config->enableComponentIDs)
390 ) {
391 // continue with process
392 }
393 else {
394 return FALSE;
395 }
396 }
397
398 // the following is imitating drupal 6 code in includes/menu.inc
399 if (empty($item['access_callback']) ||
400 is_numeric($item['access_callback'])
401 ) {
402 return (boolean ) $item['access_callback'];
403 }
404
405 // check whether the following Ajax requests submitted the right key
406 // FIXME: this should be integrated into ACLs proper
407 if (CRM_Utils_Array::value('page_type', $item) == 3) {
408 if (!CRM_Core_Key::validate($_REQUEST['key'], $item['path'])) {
409 return FALSE;
410 }
411 }
412
413 // check if callback is for checkMenu, if so optimize it
414 if (is_array($item['access_callback']) &&
415 $item['access_callback'][0] == 'CRM_Core_Permission' &&
416 $item['access_callback'][1] == 'checkMenu'
417 ) {
418 $op = CRM_Utils_Array::value(1, $item['access_arguments'], 'and');
419 return self::checkMenu($item['access_arguments'][0], $op);
420 }
421 else {
422 return call_user_func_array($item['access_callback'], $item['access_arguments']);
423 }
424 }
425
426 static function &basicPermissions($all = FALSE) {
427 static $permissions = NULL;
428
429 if (!$permissions) {
430 $prefix = ts('CiviCRM') . ': ';
431 $permissions = self::getCorePermissions();
432
433 if (self::isMultisiteEnabled()) {
434 $permissions['administer Multiple Organizations'] = $prefix . ts('administer Multiple Organizations');
435 }
436
437 $config = CRM_Core_Config::singleton();
438
439 if (!$all) {
440 $components = CRM_Core_Component::getEnabledComponents();
441 }
442 else {
443 $components = CRM_Core_Component::getComponents();
444 }
445
446 foreach ($components as $comp) {
447 $perm = $comp->getPermissions();
448 if ($perm) {
449 $info = $comp->getInfo();
450 foreach ($perm as $p) {
451 $permissions[$p] = $info['translatedName'] . ': ' . $p;
452 }
453 }
454 }
455
456 // Add any permissions defined in hook_civicrm_permission implementations.
457 $config = CRM_Core_Config::singleton();
458 $module_permissions = $config->userPermissionClass->getAllModulePermissions();
459 $permissions = array_merge($permissions, $module_permissions);
460 }
461
462 return $permissions;
463 }
464
465 static function getCorePermissions() {
466 $prefix = ts('CiviCRM') . ': ';
467 $permissions = array(
468 'add contacts' => $prefix . ts('add contacts'),
469 'view all contacts' => $prefix . ts('view all contacts'),
470 'edit all contacts' => $prefix . ts('edit all contacts'),
aafd773a
DL
471 'view my contact' => $prefix . ts('view my contact'),
472 'edit my contact' => $prefix . ts('edit my contact'),
6a488035
TO
473 'delete contacts' => $prefix . ts('delete contacts'),
474 'access deleted contacts' => $prefix . ts('access deleted contacts'),
475 'import contacts' => $prefix . ts('import contacts'),
476 'edit groups' => $prefix . ts('edit groups'),
477 'administer CiviCRM' => $prefix . ts('administer CiviCRM'),
634e1a1a 478 'skip IDS check' => $prefix . ts('skip IDS check'),
6a488035
TO
479 'access uploaded files' => $prefix . ts('access uploaded files'),
480 'profile listings and forms' => $prefix . ts('profile listings and forms'),
481 'profile listings' => $prefix . ts('profile listings'),
482 'profile create' => $prefix . ts('profile create'),
483 'profile edit' => $prefix . ts('profile edit'),
484 'profile view' => $prefix . ts('profile view'),
485 'access all custom data' => $prefix . ts('access all custom data'),
486 'view all activities' => $prefix . ts('view all activities'),
487 'delete activities' => $prefix . ts('delete activities'),
488 'access CiviCRM' => $prefix . ts('access CiviCRM'),
489 'access Contact Dashboard' => $prefix . ts('access Contact Dashboard'),
490 'translate CiviCRM' => $prefix . ts('translate CiviCRM'),
491 'administer reserved groups' => $prefix . ts('administer reserved groups'),
492 'administer Tagsets' => $prefix . ts('administer Tagsets'),
493 'administer reserved tags' => $prefix . ts('administer reserved tags'),
494 'administer dedupe rules' => $prefix . ts('administer dedupe rules'),
495 'merge duplicate contacts' => $prefix . ts('merge duplicate contacts'),
496 'view debug output' => $prefix . ts('view debug output'),
497 'view all notes' => $prefix . ts('view all notes'),
498 'access AJAX API' => $prefix . ts('access AJAX API'),
499 'access contact reference fields' => $prefix . ts('access contact reference fields'),
500 'create manual batch' => $prefix . ts('create manual batch'),
501 'edit own manual batches' => $prefix . ts('edit own manual batches'),
502 'edit all manual batches' => $prefix . ts('edit all manual batches'),
503 'view own manual batches' => $prefix . ts('view own manual batches'),
504 'view all manual batches' => $prefix . ts('view all manual batches'),
505 'delete own manual batches' => $prefix . ts('delete own manual batches'),
506 'delete all manual batches' => $prefix . ts('delete all manual batches'),
507 'export own manual batches' => $prefix . ts('export own manual batches'),
508 'export all manual batches' => $prefix . ts('export all manual batches'),
509 );
510
511 return $permissions;
512 }
513
514 /**
515 * Validate user permission across
516 * edit or view or with supportable acls.
517 *
518 * return boolean true/false.
519 **/
520 static function giveMeAllACLs() {
521 if (CRM_Core_Permission::check('view all contacts') ||
522 CRM_Core_Permission::check('edit all contacts')
523 ) {
524 return TRUE;
525 }
526
527 $session = CRM_Core_Session::singleton();
528 $contactID = $session->get('userID');
529
6a488035
TO
530 //check for acl.
531 $aclPermission = self::getPermission();
532 if (in_array($aclPermission, array(
533 CRM_Core_Permission::EDIT,
41f314b6 534 CRM_Core_Permission::VIEW,
535 ))
536 ) {
6a488035
TO
537 return TRUE;
538 }
539
540 // run acl where hook and see if the user is supplying an ACL clause
541 // that is not false
542 $tables = $whereTables = array();
543 $where = NULL;
544
545 CRM_Utils_Hook::aclWhereClause(CRM_Core_Permission::VIEW,
546 $tables, $whereTables,
547 $contactID, $where
548 );
549 return empty($whereTables) ? FALSE : TRUE;
550 }
551
552 /**
553 * Function to get component name from given permission.
554 *
41f314b6 555 * @param string $permission
6a488035
TO
556 *
557 * return string $componentName the name of component.
558 * @static
559 */
560 static function getComponentName($permission) {
561 $componentName = NULL;
562 $permission = trim($permission);
563 if (empty($permission)) {
564 return $componentName;
565 }
566
567 static $allCompPermissions = array();
568 if (empty($allCompPermissions)) {
569 $components = CRM_Core_Component::getComponents();
570 foreach ($components as $name => $comp) {
33777e4a
PJ
571 //get all permissions of each components unconditionally
572 $allCompPermissions[$name] = $comp->getPermissions(TRUE);
6a488035
TO
573 }
574 }
575
576 if (is_array($allCompPermissions)) {
577 foreach ($allCompPermissions as $name => $permissions) {
578 if (in_array($permission, $permissions)) {
579 $componentName = $name;
580 break;
581 }
582 }
583 }
584
585 return $componentName;
586 }
587
588 /**
589 * Get all the contact emails for users that have a specific permission
590 *
591 * @param string $permissionName name of the permission we are interested in
592 *
593 * @return string a comma separated list of email addresses
594 */
595 public static function permissionEmails($permissionName) {
596 $config = CRM_Core_Config::singleton();
41f314b6 597 return $config->userPermissionClass->permissionEmails($permissionName);
6a488035
TO
598 }
599
600 /**
601 * Get all the contact emails for users that have a specific role
602 *
603 * @param string $roleName name of the role we are interested in
604 *
605 * @return string a comma separated list of email addresses
606 */
607 public static function roleEmails($roleName) {
608 $config = CRM_Core_Config::singleton();
41f314b6 609 return $config->userRoleClass->roleEmails($roleName);
6a488035
TO
610 }
611
612 static function isMultisiteEnabled() {
613 return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
614 'is_enabled'
615 ) ? TRUE : FALSE;
616 }
617}