Merge pull request #999 from lcdservices/master
[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 (
162 self::check('administer Multiple Organizations') &&
163 self::isMultisiteEnabled()
164 ) {
165 return TRUE;
166 }
167
168 if (self::check('administer CiviCRM')) {
169 return TRUE;
170 }
171
172 return FALSE;
173 }
174
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();
178
179 // check if user has all powerful permission
180 // or administer civicrm permission (CRM-1905)
181 if (self::customGroupAdmin()) {
182 $defaultGroups = array_keys($customGroups);
183 }
184
185 return CRM_ACL_API::group($type, NULL, 'civicrm_custom_group', $customGroups, $defaultGroups);
186 }
187
188 static function customGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $reset = FALSE) {
189 if (self::customGroupAdmin()) {
190 return ' ( 1 ) ';
191 }
192
193 $groups = self::customGroup($type, $reset);
194 if (empty($groups)) {
195 return ' ( 0 ) ';
196 }
197 else {
198 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
199 }
200 }
201
202 public static function ufGroupValid($gid, $type = CRM_Core_Permission::VIEW) {
203 if (empty($gid)) {
204 return TRUE;
205 }
206
207 $groups = self::ufGroup($type);
208 return in_array($gid, $groups) ? TRUE : FALSE;
209 }
210
211 public static function ufGroup($type = CRM_Core_Permission::VIEW) {
212 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
213
214 $allGroups = array_keys($ufGroups);
215
216 // check if user has all powerful permission
217 if (self::check('profile listings and forms')) {
218 return $allGroups;
219 }
220
221 switch ($type) {
222 case CRM_Core_Permission::VIEW:
223 if (self::check('profile view')) {
224 return $allGroups;
225 }
226 break;
227
228 case CRM_Core_Permission::CREATE:
229 if (self::check('profile create')) {
230 return $allGroups;
231 }
232 break;
233
234 case CRM_Core_Permission::EDIT:
235 if (self::check('profile edit')) {
236 return $allGroups;
237 }
238 break;
239
240 case CRM_Core_Permission::SEARCH:
241 if (self::check('profile listings')) {
242 return $allGroups;
243 }
244 break;
245 }
246
247 return CRM_ACL_API::group($type, NULL, 'civicrm_uf_group', $ufGroups);
248 }
249
250 static function ufGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $returnUFGroupIds = FALSE) {
251 $groups = self::ufGroup($type);
252 if ($returnUFGroupIds) {
253 return $groups;
254 }
255 elseif (empty($groups)) {
256 return ' ( 0 ) ';
257 }
258 else {
259 return "{$prefix}id IN ( " . implode(',', $groups) . ' ) ';
260 }
261 }
262
263 public static function event($type = CRM_Core_Permission::VIEW, $eventID = NULL) {
264 $events = CRM_Event_PseudoConstant::event(NULL, TRUE);
265 $includeEvents = array();
266
267 // check if user has all powerful permission
268 if (self::check('register for events')) {
269 $includeEvents = array_keys($events);
270 }
271
272 if ($type == CRM_Core_Permission::VIEW &&
273 self::check('view event info')
274 ) {
275 $includeEvents = array_keys($events);
276 }
277
278 $permissionedEvents = CRM_ACL_API::group($type, NULL, 'civicrm_event', $events, $includeEvents);
279 if (!$eventID) {
280 return $permissionedEvents;
281 }
282 return array_search($eventID, $permissionedEvents) === FALSE ? NULL : $eventID;
283 }
284
285 static function eventClause($type = CRM_Core_Permission::VIEW, $prefix = NULL) {
286 $events = self::event($type);
287 if (empty($events)) {
288 return ' ( 0 ) ';
289 }
290 else {
291 return "{$prefix}id IN ( " . implode(',', $events) . ' ) ';
292 }
293 }
294
295 static function access($module, $checkPermission = TRUE) {
296 $config = CRM_Core_Config::singleton();
297
298 if (!in_array($module, $config->enableComponents)) {
299 return FALSE;
300 }
301
302 if ($checkPermission) {
303 if ($module == 'CiviCase') {
304 return CRM_Case_BAO_Case::accessCiviCase();
305 }
306 else {
307 return CRM_Core_Permission::check("access $module");
308 }
309 }
310
311 return TRUE;
312 }
313
314 /**
315 * check permissions for delete and edit actions
316 *
317 * @param string $module component name.
318 * @param $action action to be check across component
319 *
320 **/
321 static function checkActionPermission($module, $action) {
322 //check delete related permissions.
323 if ($action & CRM_Core_Action::DELETE) {
324 $permissionName = "delete in $module";
325 }
326 else {
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',
335 );
336 $permissionName = CRM_Utils_Array::value($module, $editPermissions);
337 }
338
339 if ($module == 'CiviCase' && !$permissionName) {
340 return CRM_Case_BAO_Case::accessCiviCase();
341 }
342 else {
343 //check for permission.
344 return CRM_Core_Permission::check($permissionName);
345 }
346 }
347
348 static function checkMenu(&$args, $op = 'and') {
349 if (!is_array($args)) {
350 return $args;
351 }
352 foreach ($args as $str) {
353 $res = CRM_Core_Permission::check($str);
354 if ($op == 'or' && $res) {
355 return TRUE;
356 }
357 elseif ($op == 'and' && !$res) {
358 return FALSE;
359 }
360 }
361 return ($op == 'or') ? FALSE : TRUE;
362 }
363
364 static function checkMenuItem(&$item) {
365 if (!array_key_exists('access_callback', $item)) {
366 CRM_Core_Error::backtrace();
367 CRM_Core_Error::fatal();
368 }
369
370 // if component_id is present, ensure it is enabled
371 if (isset($item['component_id']) &&
372 $item['component_id']
373 ) {
374 $config = CRM_Core_Config::singleton();
375 if (is_array($config->enableComponentIDs) &&
376 in_array($item['component_id'], $config->enableComponentIDs)
377 ) {
378 // continue with process
379 }
380 else {
381 return FALSE;
382 }
383 }
384
385 // the following is imitating drupal 6 code in includes/menu.inc
386 if (empty($item['access_callback']) ||
387 is_numeric($item['access_callback'])
388 ) {
389 return (boolean ) $item['access_callback'];
390 }
391
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'])) {
396 return FALSE;
397 }
398 }
399
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'
404 ) {
405 $op = CRM_Utils_Array::value(1, $item['access_arguments'], 'and');
406 return self::checkMenu($item['access_arguments'][0], $op);
407 }
408 else {
409 return call_user_func_array($item['access_callback'], $item['access_arguments']);
410 }
411 }
412
413 static function &basicPermissions($all = FALSE) {
414 static $permissions = NULL;
415
416 if (!$permissions) {
417 $prefix = ts('CiviCRM') . ': ';
418 $permissions = self::getCorePermissions();
419
420 if (self::isMultisiteEnabled()) {
421 $permissions['administer Multiple Organizations'] = $prefix . ts('administer Multiple Organizations');
422 }
423
424 $config = CRM_Core_Config::singleton();
425
426 if (!$all) {
427 $components = CRM_Core_Component::getEnabledComponents();
428 }
429 else {
430 $components = CRM_Core_Component::getComponents();
431 }
432
433 foreach ($components as $comp) {
434 $perm = $comp->getPermissions();
435 if ($perm) {
436 $info = $comp->getInfo();
437 foreach ($perm as $p) {
438 $permissions[$p] = $info['translatedName'] . ': ' . $p;
439 }
440 }
441 }
442
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);
447 }
448
449 return $permissions;
450 }
451
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'),
496 );
497
498 return $permissions;
499 }
500
501 /**
502 * Validate user permission across
503 * edit or view or with supportable acls.
504 *
505 * return boolean true/false.
506 **/
507 static function giveMeAllACLs() {
508 if (CRM_Core_Permission::check('view all contacts') ||
509 CRM_Core_Permission::check('edit all contacts')
510 ) {
511 return TRUE;
512 }
513
514 $session = CRM_Core_Session::singleton();
515 $contactID = $session->get('userID');
516
517 //check for acl.
518 $aclPermission = self::getPermission();
519 if (in_array($aclPermission, array(
520 CRM_Core_Permission::EDIT,
521 CRM_Core_Permission::VIEW,
522 ))) {
523 return TRUE;
524 }
525
526 // run acl where hook and see if the user is supplying an ACL clause
527 // that is not false
528 $tables = $whereTables = array();
529 $where = NULL;
530
531 CRM_Utils_Hook::aclWhereClause(CRM_Core_Permission::VIEW,
532 $tables, $whereTables,
533 $contactID, $where
534 );
535 return empty($whereTables) ? FALSE : TRUE;
536 }
537
538 /**
539 * Function to get component name from given permission.
540 *
541 * @param string $permission
542 *
543 * return string $componentName the name of component.
544 * @static
545 */
546 static function getComponentName($permission) {
547 $componentName = NULL;
548 $permission = trim($permission);
549 if (empty($permission)) {
550 return $componentName;
551 }
552
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);
559 }
560 }
561
562 if (is_array($allCompPermissions)) {
563 foreach ($allCompPermissions as $name => $permissions) {
564 if (in_array($permission, $permissions)) {
565 $componentName = $name;
566 break;
567 }
568 }
569 }
570
571 return $componentName;
572 }
573
574 /**
575 * Get all the contact emails for users that have a specific permission
576 *
577 * @param string $permissionName name of the permission we are interested in
578 *
579 * @return string a comma separated list of email addresses
580 */
581 public static function permissionEmails($permissionName) {
582 $config = CRM_Core_Config::singleton();
583 return $config->userPermissionClass->permissionEmails( $permissionName );
584 }
585
586 /**
587 * Get all the contact emails for users that have a specific role
588 *
589 * @param string $roleName name of the role we are interested in
590 *
591 * @return string a comma separated list of email addresses
592 */
593 public static function roleEmails($roleName) {
594 $config = CRM_Core_Config::singleton();
595 return $config->userRoleClass->roleEmails( $roleName );
596 }
597
598 static function isMultisiteEnabled() {
599 return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
600 'is_enabled'
601 ) ? TRUE : FALSE;
602 }
603 }
604