| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 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-2017 |
| 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 | * Various authentication sources. |
| 68 | * |
| 69 | * @var int |
| 70 | */ |
| 71 | const AUTH_SRC_UNKNOWN = 0, AUTH_SRC_CHECKSUM = 1, AUTH_SRC_SITEKEY = 2, AUTH_SRC_LOGIN = 4; |
| 72 | |
| 73 | /** |
| 74 | * Get the current permission of this user. |
| 75 | * |
| 76 | * @return string |
| 77 | * the permission of the user (edit or view or null) |
| 78 | */ |
| 79 | public static function getPermission() { |
| 80 | $config = CRM_Core_Config::singleton(); |
| 81 | return $config->userPermissionClass->getPermission(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Given a permission string or array, check for access requirements |
| 86 | * @param mixed $permissions |
| 87 | * The permission to check as an array or string -see examples. |
| 88 | * arrays |
| 89 | * |
| 90 | * Ex 1 |
| 91 | * |
| 92 | * Must have 'access CiviCRM' |
| 93 | * (string) 'access CiviCRM' |
| 94 | * |
| 95 | * |
| 96 | * Ex 2 Must have 'access CiviCRM' and 'access Ajax API' |
| 97 | * array('access CiviCRM', 'access Ajax API') |
| 98 | * |
| 99 | * Ex 3 Must have 'access CiviCRM' or 'access Ajax API' |
| 100 | * array( |
| 101 | * array('access CiviCRM', 'access Ajax API'), |
| 102 | * ), |
| 103 | * |
| 104 | * Ex 4 Must have 'access CiviCRM' or 'access Ajax API' AND 'access CiviEvent' |
| 105 | * array( |
| 106 | * array('access CiviCRM', 'access Ajax API'), |
| 107 | * 'access CiviEvent', |
| 108 | * ), |
| 109 | * |
| 110 | * Note that in permissions.php this is keyed by the action eg. |
| 111 | * (access Civi || access AJAX) && (access CiviEvent || access CiviContribute) |
| 112 | * 'myaction' => array( |
| 113 | * array('access CiviCRM', 'access Ajax API'), |
| 114 | * array('access CiviEvent', 'access CiviContribute') |
| 115 | * ), |
| 116 | * |
| 117 | * @return bool |
| 118 | * true if yes, else false |
| 119 | */ |
| 120 | public static function check($permissions) { |
| 121 | $permissions = (array) $permissions; |
| 122 | |
| 123 | $tempPerm = CRM_Core_Config::singleton()->userPermissionTemp; |
| 124 | |
| 125 | foreach ($permissions as $permission) { |
| 126 | if (is_array($permission)) { |
| 127 | foreach ($permission as $orPerm) { |
| 128 | if (self::check($orPerm)) { |
| 129 | //one of our 'or' permissions has succeeded - stop checking this permission |
| 130 | return TRUE;; |
| 131 | } |
| 132 | } |
| 133 | //none of our our conditions was met |
| 134 | return FALSE; |
| 135 | } |
| 136 | else { |
| 137 | // This is an individual permission |
| 138 | $granted = CRM_Core_Config::singleton()->userPermissionClass->check($permission); |
| 139 | // Call the permission_check hook to permit dynamic escalation (CRM-19256) |
| 140 | CRM_Utils_Hook::permission_check($permission, $granted); |
| 141 | if ( |
| 142 | !$granted |
| 143 | && !($tempPerm && $tempPerm->check($permission)) |
| 144 | ) { |
| 145 | //one of our 'and' conditions has not been met |
| 146 | return FALSE; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | return TRUE; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Determine if any one of the permissions strings applies to current user. |
| 155 | * |
| 156 | * @param array $perms |
| 157 | * @return bool |
| 158 | */ |
| 159 | public static function checkAnyPerm($perms) { |
| 160 | foreach ($perms as $perm) { |
| 161 | if (CRM_Core_Permission::check($perm)) { |
| 162 | return TRUE; |
| 163 | } |
| 164 | } |
| 165 | return FALSE; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Given a group/role array, check for access requirements |
| 170 | * |
| 171 | * @param array $array |
| 172 | * The group/role to check. |
| 173 | * |
| 174 | * @return bool |
| 175 | * true if yes, else false |
| 176 | */ |
| 177 | public static function checkGroupRole($array) { |
| 178 | $config = CRM_Core_Config::singleton(); |
| 179 | return $config->userPermissionClass->checkGroupRole($array); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Get the permissioned where clause for the user. |
| 184 | * |
| 185 | * @param int $type |
| 186 | * The type of permission needed. |
| 187 | * @param array $tables |
| 188 | * (reference ) add the tables that are needed for the select clause. |
| 189 | * @param array $whereTables |
| 190 | * (reference ) add the tables that are needed for the where clause. |
| 191 | * |
| 192 | * @return string |
| 193 | * the group where clause for this user |
| 194 | */ |
| 195 | public static function getPermissionedStaticGroupClause($type, &$tables, &$whereTables) { |
| 196 | $config = CRM_Core_Config::singleton(); |
| 197 | return $config->userPermissionClass->getPermissionedStaticGroupClause($type, $tables, $whereTables); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Get all groups from database, filtered by permissions |
| 202 | * for this user |
| 203 | * |
| 204 | * @param string $groupType |
| 205 | * Type of group(Access/Mailing). |
| 206 | * @param bool $excludeHidden |
| 207 | * exclude hidden groups. |
| 208 | * |
| 209 | * |
| 210 | * @return array |
| 211 | * array reference of all groups. |
| 212 | */ |
| 213 | public static function group($groupType, $excludeHidden = TRUE) { |
| 214 | $config = CRM_Core_Config::singleton(); |
| 215 | return $config->userPermissionClass->group($groupType, $excludeHidden); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @return bool |
| 220 | */ |
| 221 | public static function customGroupAdmin() { |
| 222 | $admin = FALSE; |
| 223 | |
| 224 | // check if user has all powerful permission |
| 225 | // or administer civicrm permission (CRM-1905) |
| 226 | if (self::check('access all custom data')) { |
| 227 | return TRUE; |
| 228 | } |
| 229 | |
| 230 | if ( |
| 231 | self::check('administer Multiple Organizations') && |
| 232 | self::isMultisiteEnabled() |
| 233 | ) { |
| 234 | return TRUE; |
| 235 | } |
| 236 | |
| 237 | if (self::check('administer CiviCRM')) { |
| 238 | return TRUE; |
| 239 | } |
| 240 | |
| 241 | return FALSE; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @param int $type |
| 246 | * @param bool $reset |
| 247 | * |
| 248 | * @return array |
| 249 | */ |
| 250 | public static function customGroup($type = CRM_Core_Permission::VIEW, $reset = FALSE) { |
| 251 | $customGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id', |
| 252 | array('fresh' => $reset)); |
| 253 | $defaultGroups = array(); |
| 254 | |
| 255 | // check if user has all powerful permission |
| 256 | // or administer civicrm permission (CRM-1905) |
| 257 | if (self::customGroupAdmin()) { |
| 258 | $defaultGroups = array_keys($customGroups); |
| 259 | } |
| 260 | |
| 261 | return CRM_ACL_API::group($type, NULL, 'civicrm_custom_group', $customGroups, $defaultGroups); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @param int $type |
| 266 | * @param null $prefix |
| 267 | * @param bool $reset |
| 268 | * |
| 269 | * @return string |
| 270 | */ |
| 271 | public static function customGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $reset = FALSE) { |
| 272 | if (self::customGroupAdmin()) { |
| 273 | return ' ( 1 ) '; |
| 274 | } |
| 275 | |
| 276 | $groups = self::customGroup($type, $reset); |
| 277 | if (empty($groups)) { |
| 278 | return ' ( 0 ) '; |
| 279 | } |
| 280 | else { |
| 281 | return "{$prefix}id IN ( " . implode(',', $groups) . ' ) '; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @param int $gid |
| 287 | * @param int $type |
| 288 | * |
| 289 | * @return bool |
| 290 | */ |
| 291 | public static function ufGroupValid($gid, $type = CRM_Core_Permission::VIEW) { |
| 292 | if (empty($gid)) { |
| 293 | return TRUE; |
| 294 | } |
| 295 | |
| 296 | $groups = self::ufGroup($type); |
| 297 | return !empty($groups) && in_array($gid, $groups) ? TRUE : FALSE; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * @param int $type |
| 302 | * |
| 303 | * @return array |
| 304 | */ |
| 305 | public static function ufGroup($type = CRM_Core_Permission::VIEW) { |
| 306 | $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id'); |
| 307 | |
| 308 | $allGroups = array_keys($ufGroups); |
| 309 | |
| 310 | // check if user has all powerful permission |
| 311 | if (self::check('profile listings and forms')) { |
| 312 | return $allGroups; |
| 313 | } |
| 314 | |
| 315 | switch ($type) { |
| 316 | case CRM_Core_Permission::VIEW: |
| 317 | if (self::check('profile view')) { |
| 318 | return $allGroups; |
| 319 | } |
| 320 | break; |
| 321 | |
| 322 | case CRM_Core_Permission::CREATE: |
| 323 | if (self::check('profile create')) { |
| 324 | return $allGroups; |
| 325 | } |
| 326 | break; |
| 327 | |
| 328 | case CRM_Core_Permission::EDIT: |
| 329 | if (self::check('profile edit')) { |
| 330 | return $allGroups; |
| 331 | } |
| 332 | break; |
| 333 | |
| 334 | case CRM_Core_Permission::SEARCH: |
| 335 | if (self::check('profile listings')) { |
| 336 | return $allGroups; |
| 337 | } |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | return CRM_ACL_API::group($type, NULL, 'civicrm_uf_group', $ufGroups); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * @param int $type |
| 346 | * @param null $prefix |
| 347 | * @param bool $returnUFGroupIds |
| 348 | * |
| 349 | * @return array|string |
| 350 | */ |
| 351 | public static function ufGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $returnUFGroupIds = FALSE) { |
| 352 | $groups = self::ufGroup($type); |
| 353 | if ($returnUFGroupIds) { |
| 354 | return $groups; |
| 355 | } |
| 356 | elseif (empty($groups)) { |
| 357 | return ' ( 0 ) '; |
| 358 | } |
| 359 | else { |
| 360 | return "{$prefix}id IN ( " . implode(',', $groups) . ' ) '; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * @param int $type |
| 366 | * @param int $eventID |
| 367 | * @param string $context |
| 368 | * |
| 369 | * @return array|null |
| 370 | */ |
| 371 | public static function event($type = CRM_Core_Permission::VIEW, $eventID = NULL, $context = '') { |
| 372 | if (!empty($context)) { |
| 373 | if (CRM_Core_Permission::check($context)) { |
| 374 | return TRUE; |
| 375 | } |
| 376 | } |
| 377 | $events = CRM_Event_PseudoConstant::event(NULL, TRUE); |
| 378 | $includeEvents = array(); |
| 379 | |
| 380 | // check if user has all powerful permission |
| 381 | if (self::check('register for events')) { |
| 382 | $includeEvents = array_keys($events); |
| 383 | } |
| 384 | |
| 385 | if ($type == CRM_Core_Permission::VIEW && |
| 386 | self::check('view event info') |
| 387 | ) { |
| 388 | $includeEvents = array_keys($events); |
| 389 | } |
| 390 | |
| 391 | $permissionedEvents = CRM_ACL_API::group($type, NULL, 'civicrm_event', $events, $includeEvents); |
| 392 | if (!$eventID) { |
| 393 | return $permissionedEvents; |
| 394 | } |
| 395 | if (!empty($permissionedEvents)) { |
| 396 | return array_search($eventID, $permissionedEvents) === FALSE ? NULL : $eventID; |
| 397 | } |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * @param int $type |
| 403 | * @param null $prefix |
| 404 | * |
| 405 | * @return string |
| 406 | */ |
| 407 | public static function eventClause($type = CRM_Core_Permission::VIEW, $prefix = NULL) { |
| 408 | $events = self::event($type); |
| 409 | if (empty($events)) { |
| 410 | return ' ( 0 ) '; |
| 411 | } |
| 412 | else { |
| 413 | return "{$prefix}id IN ( " . implode(',', $events) . ' ) '; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Checks that component is enabled and optionally that user has basic perm. |
| 419 | * |
| 420 | * @param string $module |
| 421 | * Specifies the name of the CiviCRM component. |
| 422 | * @param bool $checkPermission |
| 423 | * Check not only that module is enabled, but that user has necessary |
| 424 | * permission. |
| 425 | * @param bool $requireAllCasesPermOnCiviCase |
| 426 | * Significant only if $module == CiviCase |
| 427 | * Require "access all cases and activities", not just |
| 428 | * "access my cases and activities". |
| 429 | * |
| 430 | * @return bool |
| 431 | * Access to specified $module is granted. |
| 432 | */ |
| 433 | public static function access($module, $checkPermission = TRUE, $requireAllCasesPermOnCiviCase = FALSE) { |
| 434 | $config = CRM_Core_Config::singleton(); |
| 435 | |
| 436 | if (!in_array($module, $config->enableComponents)) { |
| 437 | return FALSE; |
| 438 | } |
| 439 | |
| 440 | if ($checkPermission) { |
| 441 | switch ($module) { |
| 442 | case 'CiviCase': |
| 443 | $access_all_cases = CRM_Core_Permission::check("access all cases and activities"); |
| 444 | $access_my_cases = CRM_Core_Permission::check("access my cases and activities"); |
| 445 | return $access_all_cases || (!$requireAllCasesPermOnCiviCase && $access_my_cases); |
| 446 | |
| 447 | case 'CiviCampaign': |
| 448 | return CRM_Core_Permission::check("administer $module"); |
| 449 | |
| 450 | default: |
| 451 | return CRM_Core_Permission::check("access $module"); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return TRUE; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Check permissions for delete and edit actions. |
| 460 | * |
| 461 | * @param string $module |
| 462 | * Component name. |
| 463 | * @param int $action |
| 464 | * Action to be check across component. |
| 465 | * |
| 466 | * |
| 467 | * @return bool |
| 468 | */ |
| 469 | public static function checkActionPermission($module, $action) { |
| 470 | //check delete related permissions. |
| 471 | if ($action & CRM_Core_Action::DELETE) { |
| 472 | $permissionName = "delete in $module"; |
| 473 | } |
| 474 | else { |
| 475 | $editPermissions = array( |
| 476 | 'CiviEvent' => 'edit event participants', |
| 477 | 'CiviMember' => 'edit memberships', |
| 478 | 'CiviPledge' => 'edit pledges', |
| 479 | 'CiviContribute' => 'edit contributions', |
| 480 | 'CiviGrant' => 'edit grants', |
| 481 | 'CiviMail' => 'access CiviMail', |
| 482 | 'CiviAuction' => 'add auction items', |
| 483 | ); |
| 484 | $permissionName = CRM_Utils_Array::value($module, $editPermissions); |
| 485 | } |
| 486 | |
| 487 | if ($module == 'CiviCase' && !$permissionName) { |
| 488 | return CRM_Case_BAO_Case::accessCiviCase(); |
| 489 | } |
| 490 | else { |
| 491 | //check for permission. |
| 492 | return CRM_Core_Permission::check($permissionName); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * @param $args |
| 498 | * @param string $op |
| 499 | * |
| 500 | * @return bool |
| 501 | */ |
| 502 | public static function checkMenu(&$args, $op = 'and') { |
| 503 | if (!is_array($args)) { |
| 504 | return $args; |
| 505 | } |
| 506 | foreach ($args as $str) { |
| 507 | $res = CRM_Core_Permission::check($str); |
| 508 | if ($op == 'or' && $res) { |
| 509 | return TRUE; |
| 510 | } |
| 511 | elseif ($op == 'and' && !$res) { |
| 512 | return FALSE; |
| 513 | } |
| 514 | } |
| 515 | return ($op == 'or') ? FALSE : TRUE; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * @param $item |
| 520 | * |
| 521 | * @return bool|mixed |
| 522 | * @throws Exception |
| 523 | */ |
| 524 | public static function checkMenuItem(&$item) { |
| 525 | if (!array_key_exists('access_callback', $item)) { |
| 526 | CRM_Core_Error::backtrace(); |
| 527 | CRM_Core_Error::fatal(); |
| 528 | } |
| 529 | |
| 530 | // if component_id is present, ensure it is enabled |
| 531 | if (isset($item['component_id']) && $item['component_id']) { |
| 532 | if (!isset(Civi::$statics[__CLASS__]['componentNameId'])) { |
| 533 | Civi::$statics[__CLASS__]['componentNameId'] = array_flip(CRM_Core_Component::getComponentIDs()); |
| 534 | } |
| 535 | $componentName = Civi::$statics[__CLASS__]['componentNameId'][$item['component_id']]; |
| 536 | |
| 537 | $config = CRM_Core_Config::singleton(); |
| 538 | if (is_array($config->enableComponents) && in_array($componentName, $config->enableComponents)) { |
| 539 | // continue with process |
| 540 | } |
| 541 | else { |
| 542 | return FALSE; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // the following is imitating drupal 6 code in includes/menu.inc |
| 547 | if (empty($item['access_callback']) || |
| 548 | is_numeric($item['access_callback']) |
| 549 | ) { |
| 550 | return (boolean ) $item['access_callback']; |
| 551 | } |
| 552 | |
| 553 | // check whether the following Ajax requests submitted the right key |
| 554 | // FIXME: this should be integrated into ACLs proper |
| 555 | if (CRM_Utils_Array::value('page_type', $item) == 3) { |
| 556 | if (!CRM_Core_Key::validate($_REQUEST['key'], $item['path'])) { |
| 557 | return FALSE; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | // check if callback is for checkMenu, if so optimize it |
| 562 | if (is_array($item['access_callback']) && |
| 563 | $item['access_callback'][0] == 'CRM_Core_Permission' && |
| 564 | $item['access_callback'][1] == 'checkMenu' |
| 565 | ) { |
| 566 | $op = CRM_Utils_Array::value(1, $item['access_arguments'], 'and'); |
| 567 | return self::checkMenu($item['access_arguments'][0], $op); |
| 568 | } |
| 569 | else { |
| 570 | return call_user_func_array($item['access_callback'], $item['access_arguments']); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * @param bool $all |
| 576 | * Include disabled components |
| 577 | * @param bool $descriptions |
| 578 | * Whether to return descriptions |
| 579 | * |
| 580 | * @return array |
| 581 | */ |
| 582 | public static function basicPermissions($all = FALSE, $descriptions = FALSE) { |
| 583 | $cacheKey = implode('-', array($all, $descriptions)); |
| 584 | if (empty(Civi::$statics[__CLASS__][__FUNCTION__][$cacheKey])) { |
| 585 | Civi::$statics[__CLASS__][__FUNCTION__][$cacheKey] = self::assembleBasicPermissions($all, $descriptions); |
| 586 | } |
| 587 | return Civi::$statics[__CLASS__][__FUNCTION__][$cacheKey]; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * @param bool $all |
| 592 | * @param bool $descriptions |
| 593 | * whether to return descriptions |
| 594 | * |
| 595 | * @return array |
| 596 | */ |
| 597 | public static function assembleBasicPermissions($all = FALSE, $descriptions = FALSE) { |
| 598 | $config = CRM_Core_Config::singleton(); |
| 599 | $prefix = ts('CiviCRM') . ': '; |
| 600 | $permissions = self::getCorePermissions($descriptions); |
| 601 | |
| 602 | if (self::isMultisiteEnabled()) { |
| 603 | $permissions['administer Multiple Organizations'] = array($prefix . ts('administer Multiple Organizations')); |
| 604 | } |
| 605 | |
| 606 | if (!$descriptions) { |
| 607 | foreach ($permissions as $name => $attr) { |
| 608 | $permissions[$name] = array_shift($attr); |
| 609 | } |
| 610 | } |
| 611 | if (!$all) { |
| 612 | $components = CRM_Core_Component::getEnabledComponents(); |
| 613 | } |
| 614 | else { |
| 615 | $components = CRM_Core_Component::getComponents(); |
| 616 | } |
| 617 | |
| 618 | foreach ($components as $comp) { |
| 619 | $perm = $comp->getPermissions(FALSE, $descriptions); |
| 620 | if ($perm) { |
| 621 | $info = $comp->getInfo(); |
| 622 | foreach ($perm as $p => $attr) { |
| 623 | |
| 624 | if (!is_array($attr)) { |
| 625 | $attr = array($attr); |
| 626 | } |
| 627 | |
| 628 | $attr[0] = $info['translatedName'] . ': ' . $attr[0]; |
| 629 | |
| 630 | if ($descriptions) { |
| 631 | $permissions[$p] = $attr; |
| 632 | } |
| 633 | else { |
| 634 | $permissions[$p] = $attr[0]; |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | // Add any permissions defined in hook_civicrm_permission implementations. |
| 641 | $module_permissions = $config->userPermissionClass->getAllModulePermissions($descriptions); |
| 642 | $permissions = array_merge($permissions, $module_permissions); |
| 643 | CRM_Financial_BAO_FinancialType::permissionedFinancialTypes($permissions, $descriptions); |
| 644 | return $permissions; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * @return array |
| 649 | */ |
| 650 | public static function getAnonymousPermissionsWarnings() { |
| 651 | static $permissions = array(); |
| 652 | if (empty($permissions)) { |
| 653 | $permissions = array( |
| 654 | 'administer CiviCRM', |
| 655 | ); |
| 656 | $components = CRM_Core_Component::getComponents(); |
| 657 | foreach ($components as $comp) { |
| 658 | if (!method_exists($comp, 'getAnonymousPermissionWarnings')) { |
| 659 | continue; |
| 660 | } |
| 661 | $permissions = array_merge($permissions, $comp->getAnonymousPermissionWarnings()); |
| 662 | } |
| 663 | } |
| 664 | return $permissions; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * @param $anonymous_perms |
| 669 | * |
| 670 | * @return array |
| 671 | */ |
| 672 | public static function validateForPermissionWarnings($anonymous_perms) { |
| 673 | return array_intersect($anonymous_perms, self::getAnonymousPermissionsWarnings()); |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Get core permissions. |
| 678 | * |
| 679 | * @return array |
| 680 | */ |
| 681 | public static function getCorePermissions() { |
| 682 | $prefix = ts('CiviCRM') . ': '; |
| 683 | $permissions = array( |
| 684 | 'add contacts' => array( |
| 685 | $prefix . ts('add contacts'), |
| 686 | ts('Create a new contact record in CiviCRM'), |
| 687 | ), |
| 688 | 'view all contacts' => array( |
| 689 | $prefix . ts('view all contacts'), |
| 690 | ts('View ANY CONTACT in the CiviCRM database, export contact info and perform activities such as Send Email, Phone Call, etc.'), |
| 691 | ), |
| 692 | 'edit all contacts' => array( |
| 693 | $prefix . ts('edit all contacts'), |
| 694 | ts('View, Edit and Delete ANY CONTACT in the CiviCRM database; Create and edit relationships, tags and other info about the contacts'), |
| 695 | ), |
| 696 | 'view my contact' => array( |
| 697 | $prefix . ts('view my contact'), |
| 698 | ), |
| 699 | 'edit my contact' => array( |
| 700 | $prefix . ts('edit my contact'), |
| 701 | ), |
| 702 | 'delete contacts' => array( |
| 703 | $prefix . ts('delete contacts'), |
| 704 | ), |
| 705 | 'access deleted contacts' => array( |
| 706 | $prefix . ts('access deleted contacts'), |
| 707 | ts('Access contacts in the trash'), |
| 708 | ), |
| 709 | 'import contacts' => array( |
| 710 | $prefix . ts('import contacts'), |
| 711 | ts('Import contacts and activities'), |
| 712 | ), |
| 713 | 'import SQL datasource' => array( |
| 714 | $prefix . ts('import SQL datasource'), |
| 715 | ts('When importing, consume data directly from a SQL datasource'), |
| 716 | ), |
| 717 | 'edit groups' => array( |
| 718 | $prefix . ts('edit groups'), |
| 719 | ts('Create new groups, edit group settings (e.g. group name, visibility...), delete groups'), |
| 720 | ), |
| 721 | 'administer CiviCRM' => array( |
| 722 | $prefix . ts('administer CiviCRM'), |
| 723 | ts('Perform all tasks in the Administer CiviCRM control panel and Import Contacts'), |
| 724 | ), |
| 725 | 'skip IDS check' => array( |
| 726 | $prefix . ts('skip IDS check'), |
| 727 | ts('IDS system is bypassed for users with this permission. Prevents false errors for admin users.'), |
| 728 | ), |
| 729 | 'access uploaded files' => array( |
| 730 | $prefix . ts('access uploaded files'), |
| 731 | ts('View / download files including images and photos'), |
| 732 | ), |
| 733 | 'profile listings and forms' => array( |
| 734 | $prefix . ts('profile listings and forms'), |
| 735 | ts('Access the profile Search form and listings'), |
| 736 | ), |
| 737 | 'profile listings' => array( |
| 738 | $prefix . ts('profile listings'), |
| 739 | ), |
| 740 | 'profile create' => array( |
| 741 | $prefix . ts('profile create'), |
| 742 | ts('Use profiles in Create mode'), |
| 743 | ), |
| 744 | 'profile edit' => array( |
| 745 | $prefix . ts('profile edit'), |
| 746 | ts('Use profiles in Edit mode'), |
| 747 | ), |
| 748 | 'profile view' => array( |
| 749 | $prefix . ts('profile view'), |
| 750 | ), |
| 751 | 'access all custom data' => array( |
| 752 | $prefix . ts('access all custom data'), |
| 753 | ts('View all custom fields regardless of ACL rules'), |
| 754 | ), |
| 755 | 'view all activities' => array( |
| 756 | $prefix . ts('view all activities'), |
| 757 | ts('View all activities (for visible contacts)'), |
| 758 | ), |
| 759 | 'delete activities' => array( |
| 760 | $prefix . ts('Delete activities'), |
| 761 | ), |
| 762 | 'access CiviCRM' => array( |
| 763 | $prefix . ts('access CiviCRM'), |
| 764 | ts('Master control for access to the main CiviCRM backend and API'), |
| 765 | ), |
| 766 | 'access Contact Dashboard' => array( |
| 767 | $prefix . ts('access Contact Dashboard'), |
| 768 | ts('View Contact Dashboard (for themselves and visible contacts)'), |
| 769 | ), |
| 770 | 'translate CiviCRM' => array( |
| 771 | $prefix . ts('translate CiviCRM'), |
| 772 | ts('Allow User to enable multilingual'), |
| 773 | ), |
| 774 | 'manage tags' => array( |
| 775 | $prefix . ts('manage tags'), |
| 776 | ts('Create and rename tags'), |
| 777 | ), |
| 778 | 'administer reserved groups' => array( |
| 779 | $prefix . ts('administer reserved groups'), |
| 780 | ts('Edit and disable Reserved Groups (Needs Edit Groups)'), |
| 781 | ), |
| 782 | 'administer Tagsets' => array( |
| 783 | $prefix . ts('administer Tagsets'), |
| 784 | ), |
| 785 | 'administer reserved tags' => array( |
| 786 | $prefix . ts('administer reserved tags'), |
| 787 | ), |
| 788 | 'administer dedupe rules' => array( |
| 789 | $prefix . ts('administer dedupe rules'), |
| 790 | ts('Create and edit rules, change the supervised and unsupervised rules'), |
| 791 | ), |
| 792 | 'merge duplicate contacts' => array( |
| 793 | $prefix . ts('merge duplicate contacts'), |
| 794 | ts('Delete Contacts must also be granted in order for this to work.'), |
| 795 | ), |
| 796 | 'force merge duplicate contacts' => array( |
| 797 | $prefix . ts('force merge duplicate contacts'), |
| 798 | ts('Delete Contacts must also be granted in order for this to work.'), |
| 799 | ), |
| 800 | 'view debug output' => array( |
| 801 | $prefix . ts('view debug output'), |
| 802 | ts('View results of debug and backtrace'), |
| 803 | ), |
| 804 | |
| 805 | 'view all notes' => array( |
| 806 | $prefix . ts('view all notes'), |
| 807 | ts("View notes (for visible contacts) even if they're marked admin only"), |
| 808 | ), |
| 809 | 'access AJAX API' => array( |
| 810 | $prefix . ts('access AJAX API'), |
| 811 | ts('Allow API access even if Access CiviCRM is not granted'), |
| 812 | ), |
| 813 | 'access contact reference fields' => array( |
| 814 | $prefix . ts('access contact reference fields'), |
| 815 | ts('Allow entering data into contact reference fields'), |
| 816 | ), |
| 817 | 'create manual batch' => array( |
| 818 | $prefix . ts('create manual batch'), |
| 819 | ts('Create an accounting batch (with Access to CiviContribute and View Own/All Manual Batches)'), |
| 820 | ), |
| 821 | 'edit own manual batches' => array( |
| 822 | $prefix . ts('edit own manual batches'), |
| 823 | ts('Edit accounting batches created by user'), |
| 824 | ), |
| 825 | 'edit all manual batches' => array( |
| 826 | $prefix . ts('edit all manual batches'), |
| 827 | ts('Edit all accounting batches'), |
| 828 | ), |
| 829 | 'view own manual batches' => array( |
| 830 | $prefix . ts('view own manual batches'), |
| 831 | ts('View accounting batches created by user (with Access to CiviContribute)'), |
| 832 | ), |
| 833 | 'view all manual batches' => array( |
| 834 | $prefix . ts('view all manual batches'), |
| 835 | ts('View all accounting batches (with Access to CiviContribute)'), |
| 836 | ), |
| 837 | 'delete own manual batches' => array( |
| 838 | $prefix . ts('delete own manual batches'), |
| 839 | ts('Delete accounting batches created by user'), |
| 840 | ), |
| 841 | 'delete all manual batches' => array( |
| 842 | $prefix . ts('delete all manual batches'), |
| 843 | ts('Delete all accounting batches'), |
| 844 | ), |
| 845 | 'export own manual batches' => array( |
| 846 | $prefix . ts('export own manual batches'), |
| 847 | ts('Export accounting batches created by user'), |
| 848 | ), |
| 849 | 'export all manual batches' => array( |
| 850 | $prefix . ts('export all manual batches'), |
| 851 | ts('Export all accounting batches'), |
| 852 | ), |
| 853 | 'administer payment processors' => array( |
| 854 | $prefix . ts('administer payment processors'), |
| 855 | ts('Add, Update, or Disable Payment Processors'), |
| 856 | ), |
| 857 | 'edit message templates' => array( |
| 858 | $prefix . ts('edit message templates'), |
| 859 | ), |
| 860 | 'view my invoices' => array( |
| 861 | $prefix . ts('view my invoices'), |
| 862 | ts('Allow users to view/ download their own invoices'), |
| 863 | ), |
| 864 | 'edit api keys' => array( |
| 865 | $prefix . ts('edit api keys'), |
| 866 | ts('Edit API keys'), |
| 867 | ), |
| 868 | 'edit own api keys' => array( |
| 869 | $prefix . ts('edit own api keys'), |
| 870 | ts('Edit user\'s own API keys'), |
| 871 | ), |
| 872 | ); |
| 873 | |
| 874 | return $permissions; |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * For each entity provides an array of permissions required for each action |
| 879 | * |
| 880 | * The action is the array key, possible values: |
| 881 | * * create: applies to create (with no id in params) |
| 882 | * * update: applies to update, setvalue, create (with id in params) |
| 883 | * * get: applies to getcount, getsingle, getvalue and other gets |
| 884 | * * delete: applies to delete, replace |
| 885 | * * meta: applies to getfields, getoptions, getspec |
| 886 | * * default: catch-all for anything not declared |
| 887 | * |
| 888 | * Note: some APIs declare other actions as well |
| 889 | * |
| 890 | * Permissions should use arrays for AND and arrays of arrays for OR |
| 891 | * @see CRM_Core_Permission::check |
| 892 | * |
| 893 | * @return array of permissions |
| 894 | */ |
| 895 | public static function getEntityActionPermissions() { |
| 896 | $permissions = array(); |
| 897 | // These are the default permissions - if any entity does not declare permissions for a given action, |
| 898 | // (or the entity does not declare permissions at all) - then the action will be used from here |
| 899 | $permissions['default'] = array( |
| 900 | // applies to getfields, getoptions, etc. |
| 901 | 'meta' => array('access CiviCRM'), |
| 902 | // catch-all, applies to create, get, delete, etc. |
| 903 | // If an entity declares it's own 'default' action it will override this one |
| 904 | 'default' => array('administer CiviCRM'), |
| 905 | ); |
| 906 | |
| 907 | // Note: Additional permissions in DynamicFKAuthorization |
| 908 | $permissions['attachment'] = array( |
| 909 | 'default' => array( |
| 910 | array('access CiviCRM', 'access AJAX API'), |
| 911 | ), |
| 912 | ); |
| 913 | |
| 914 | // Contact permissions |
| 915 | $permissions['contact'] = array( |
| 916 | 'create' => array( |
| 917 | 'access CiviCRM', |
| 918 | 'add contacts', |
| 919 | ), |
| 920 | 'delete' => array( |
| 921 | 'access CiviCRM', |
| 922 | 'delete contacts', |
| 923 | ), |
| 924 | // managed by query object |
| 925 | 'get' => array(), |
| 926 | // managed by _civicrm_api3_check_edit_permissions |
| 927 | 'update' => array(), |
| 928 | 'getquick' => array( |
| 929 | array('access CiviCRM', 'access AJAX API'), |
| 930 | ), |
| 931 | ); |
| 932 | |
| 933 | // CRM-16963 - Permissions for country. |
| 934 | $permissions['country'] = array( |
| 935 | 'get' => array( |
| 936 | 'access CiviCRM', |
| 937 | ), |
| 938 | 'default' => array( |
| 939 | 'administer CiviCRM', |
| 940 | ), |
| 941 | ); |
| 942 | |
| 943 | // Contact-related data permissions. |
| 944 | $permissions['address'] = array( |
| 945 | // get is managed by BAO::addSelectWhereClause |
| 946 | // create/delete are managed by _civicrm_api3_check_edit_permissions |
| 947 | 'default' => array(), |
| 948 | ); |
| 949 | $permissions['email'] = $permissions['address']; |
| 950 | $permissions['phone'] = $permissions['address']; |
| 951 | $permissions['website'] = $permissions['address']; |
| 952 | $permissions['im'] = $permissions['address']; |
| 953 | $permissions['open_i_d'] = $permissions['address']; |
| 954 | |
| 955 | // Also managed by ACLs - CRM-19448 |
| 956 | $permissions['entity_tag'] = array('default' => array()); |
| 957 | $permissions['note'] = $permissions['entity_tag']; |
| 958 | |
| 959 | // Allow non-admins to get and create tags to support tagset widget |
| 960 | // Delete is still reserved for admins |
| 961 | $permissions['tag'] = array( |
| 962 | 'get' => array('access CiviCRM'), |
| 963 | 'create' => array('access CiviCRM'), |
| 964 | 'update' => array('access CiviCRM'), |
| 965 | ); |
| 966 | |
| 967 | //relationship permissions |
| 968 | $permissions['relationship'] = array( |
| 969 | // get is managed by BAO::addSelectWhereClause |
| 970 | 'get' => array(), |
| 971 | 'delete' => array( |
| 972 | 'access CiviCRM', |
| 973 | 'edit all contacts', |
| 974 | ), |
| 975 | 'default' => array( |
| 976 | 'access CiviCRM', |
| 977 | 'edit all contacts', |
| 978 | ), |
| 979 | ); |
| 980 | |
| 981 | // CRM-17741 - Permissions for RelationshipType. |
| 982 | $permissions['relationship_type'] = array( |
| 983 | 'get' => array( |
| 984 | 'access CiviCRM', |
| 985 | ), |
| 986 | 'default' => array( |
| 987 | 'administer CiviCRM', |
| 988 | ), |
| 989 | ); |
| 990 | |
| 991 | // Activity permissions |
| 992 | $permissions['activity'] = array( |
| 993 | 'delete' => array( |
| 994 | 'access CiviCRM', |
| 995 | 'delete activities', |
| 996 | ), |
| 997 | 'get' => array( |
| 998 | 'access CiviCRM', |
| 999 | // Note that view all activities is also required within the api |
| 1000 | // if the id is not passed in. Where the id is passed in the activity |
| 1001 | // specific check functions are used and tested. |
| 1002 | ), |
| 1003 | 'default' => array( |
| 1004 | 'access CiviCRM', |
| 1005 | 'view all activities', |
| 1006 | ), |
| 1007 | ); |
| 1008 | |
| 1009 | // Case permissions |
| 1010 | $permissions['case'] = array( |
| 1011 | 'create' => array( |
| 1012 | 'access CiviCRM', |
| 1013 | 'add cases', |
| 1014 | ), |
| 1015 | 'delete' => array( |
| 1016 | 'access CiviCRM', |
| 1017 | 'delete in CiviCase', |
| 1018 | ), |
| 1019 | 'restore' => array( |
| 1020 | 'administer CiviCase', |
| 1021 | ), |
| 1022 | 'merge' => array( |
| 1023 | 'administer CiviCase', |
| 1024 | ), |
| 1025 | 'default' => array( |
| 1026 | // At minimum the user needs one of the following. Finer-grained access is controlled by CRM_Case_BAO_Case::addSelectWhereClause |
| 1027 | array('access my cases and activities', 'access all cases and activities'), |
| 1028 | ), |
| 1029 | ); |
| 1030 | $permissions['case_contact'] = $permissions['case']; |
| 1031 | |
| 1032 | $permissions['case_type'] = array( |
| 1033 | 'default' => array('administer CiviCase'), |
| 1034 | 'get' => array( |
| 1035 | // nested array = OR |
| 1036 | array('access my cases and activities', 'access all cases and activities'), |
| 1037 | ), |
| 1038 | ); |
| 1039 | |
| 1040 | // Campaign permissions |
| 1041 | $permissions['campaign'] = array( |
| 1042 | 'get' => array('access CiviCRM'), |
| 1043 | 'default' => array( |
| 1044 | // nested array = OR |
| 1045 | array('administer CiviCampaign', 'manage campaign'), |
| 1046 | ), |
| 1047 | ); |
| 1048 | $permissions['survey'] = $permissions['campaign']; |
| 1049 | |
| 1050 | // Financial permissions |
| 1051 | $permissions['contribution'] = array( |
| 1052 | 'get' => array( |
| 1053 | 'access CiviCRM', |
| 1054 | 'access CiviContribute', |
| 1055 | ), |
| 1056 | 'delete' => array( |
| 1057 | 'access CiviCRM', |
| 1058 | 'access CiviContribute', |
| 1059 | 'delete in CiviContribute', |
| 1060 | ), |
| 1061 | 'completetransaction' => array( |
| 1062 | 'edit contributions', |
| 1063 | ), |
| 1064 | 'default' => array( |
| 1065 | 'access CiviCRM', |
| 1066 | 'access CiviContribute', |
| 1067 | 'edit contributions', |
| 1068 | ), |
| 1069 | ); |
| 1070 | $permissions['line_item'] = $permissions['contribution']; |
| 1071 | |
| 1072 | // Payment permissions |
| 1073 | $permissions['payment'] = array( |
| 1074 | 'get' => array( |
| 1075 | 'access CiviCRM', |
| 1076 | 'access CiviContribute', |
| 1077 | ), |
| 1078 | 'delete' => array( |
| 1079 | 'access CiviCRM', |
| 1080 | 'access CiviContribute', |
| 1081 | 'delete in CiviContribute', |
| 1082 | ), |
| 1083 | 'cancel' => array( |
| 1084 | 'access CiviCRM', |
| 1085 | 'access CiviContribute', |
| 1086 | 'edit contributions', |
| 1087 | ), |
| 1088 | 'create' => array( |
| 1089 | 'access CiviCRM', |
| 1090 | 'access CiviContribute', |
| 1091 | 'edit contributions', |
| 1092 | ), |
| 1093 | 'default' => array( |
| 1094 | 'access CiviCRM', |
| 1095 | 'access CiviContribute', |
| 1096 | 'edit contributions', |
| 1097 | ), |
| 1098 | ); |
| 1099 | |
| 1100 | // Custom field permissions |
| 1101 | $permissions['custom_field'] = array( |
| 1102 | 'default' => array( |
| 1103 | 'administer CiviCRM', |
| 1104 | 'access all custom data', |
| 1105 | ), |
| 1106 | ); |
| 1107 | $permissions['custom_group'] = $permissions['custom_field']; |
| 1108 | |
| 1109 | // Event permissions |
| 1110 | $permissions['event'] = array( |
| 1111 | 'create' => array( |
| 1112 | 'access CiviCRM', |
| 1113 | 'access CiviEvent', |
| 1114 | 'edit all events', |
| 1115 | ), |
| 1116 | 'delete' => array( |
| 1117 | 'access CiviCRM', |
| 1118 | 'access CiviEvent', |
| 1119 | 'delete in CiviEvent', |
| 1120 | ), |
| 1121 | 'get' => array( |
| 1122 | 'access CiviCRM', |
| 1123 | 'access CiviEvent', |
| 1124 | 'view event info', |
| 1125 | ), |
| 1126 | 'update' => array( |
| 1127 | 'access CiviCRM', |
| 1128 | 'access CiviEvent', |
| 1129 | 'edit all events', |
| 1130 | ), |
| 1131 | ); |
| 1132 | // Loc block is only used for events |
| 1133 | $permissions['loc_block'] = $permissions['event']; |
| 1134 | |
| 1135 | // Price sets are shared by several components, user needs access to at least one of them |
| 1136 | $permissions['price_set'] = array( |
| 1137 | 'default' => array( |
| 1138 | array('access CiviEvent', 'access CiviContribute', 'access CiviMember'), |
| 1139 | ), |
| 1140 | 'get' => array( |
| 1141 | array('access CiviCRM', 'view event info', 'make online contributions'), |
| 1142 | ), |
| 1143 | ); |
| 1144 | |
| 1145 | // File permissions |
| 1146 | $permissions['file'] = array( |
| 1147 | 'default' => array( |
| 1148 | 'access CiviCRM', |
| 1149 | 'access uploaded files', |
| 1150 | ), |
| 1151 | ); |
| 1152 | $permissions['files_by_entity'] = $permissions['file']; |
| 1153 | |
| 1154 | // Group permissions |
| 1155 | $permissions['group'] = array( |
| 1156 | 'get' => array( |
| 1157 | 'access CiviCRM', |
| 1158 | ), |
| 1159 | 'default' => array( |
| 1160 | 'access CiviCRM', |
| 1161 | 'edit groups', |
| 1162 | ), |
| 1163 | ); |
| 1164 | |
| 1165 | $permissions['group_nesting'] = $permissions['group']; |
| 1166 | $permissions['group_organization'] = $permissions['group']; |
| 1167 | |
| 1168 | //Group Contact permission |
| 1169 | $permissions['group_contact'] = array( |
| 1170 | 'get' => array( |
| 1171 | 'access CiviCRM', |
| 1172 | ), |
| 1173 | 'default' => array( |
| 1174 | 'access CiviCRM', |
| 1175 | 'edit all contacts', |
| 1176 | ), |
| 1177 | ); |
| 1178 | |
| 1179 | // CiviMail Permissions |
| 1180 | $civiMailBasePerms = array( |
| 1181 | // To get/preview/update, one must have least one of these perms: |
| 1182 | // Mailing API implementations enforce nuances of create/approve/schedule permissions. |
| 1183 | 'access CiviMail', |
| 1184 | 'create mailings', |
| 1185 | 'schedule mailings', |
| 1186 | 'approve mailings', |
| 1187 | ); |
| 1188 | $permissions['mailing'] = array( |
| 1189 | 'get' => array( |
| 1190 | 'access CiviCRM', |
| 1191 | $civiMailBasePerms, |
| 1192 | ), |
| 1193 | 'delete' => array( |
| 1194 | 'access CiviCRM', |
| 1195 | $civiMailBasePerms, |
| 1196 | 'delete in CiviMail', |
| 1197 | ), |
| 1198 | 'submit' => array( |
| 1199 | 'access CiviCRM', |
| 1200 | array('access CiviMail', 'schedule mailings'), |
| 1201 | ), |
| 1202 | 'default' => array( |
| 1203 | 'access CiviCRM', |
| 1204 | $civiMailBasePerms, |
| 1205 | ), |
| 1206 | ); |
| 1207 | $permissions['mailing_group'] = $permissions['mailing']; |
| 1208 | $permissions['mailing_job'] = $permissions['mailing']; |
| 1209 | $permissions['mailing_recipients'] = $permissions['mailing']; |
| 1210 | |
| 1211 | $permissions['mailing_a_b'] = array( |
| 1212 | 'get' => array( |
| 1213 | 'access CiviCRM', |
| 1214 | 'access CiviMail', |
| 1215 | ), |
| 1216 | 'delete' => array( |
| 1217 | 'access CiviCRM', |
| 1218 | 'access CiviMail', |
| 1219 | 'delete in CiviMail', |
| 1220 | ), |
| 1221 | 'submit' => array( |
| 1222 | 'access CiviCRM', |
| 1223 | array('access CiviMail', 'schedule mailings'), |
| 1224 | ), |
| 1225 | 'default' => array( |
| 1226 | 'access CiviCRM', |
| 1227 | 'access CiviMail', |
| 1228 | ), |
| 1229 | ); |
| 1230 | |
| 1231 | // Membership permissions |
| 1232 | $permissions['membership'] = array( |
| 1233 | 'get' => array( |
| 1234 | 'access CiviCRM', |
| 1235 | 'access CiviMember', |
| 1236 | ), |
| 1237 | 'delete' => array( |
| 1238 | 'access CiviCRM', |
| 1239 | 'access CiviMember', |
| 1240 | 'delete in CiviMember', |
| 1241 | ), |
| 1242 | 'default' => array( |
| 1243 | 'access CiviCRM', |
| 1244 | 'access CiviMember', |
| 1245 | 'edit memberships', |
| 1246 | ), |
| 1247 | ); |
| 1248 | $permissions['membership_status'] = $permissions['membership']; |
| 1249 | $permissions['membership_type'] = $permissions['membership']; |
| 1250 | $permissions['membership_payment'] = array( |
| 1251 | 'create' => array( |
| 1252 | 'access CiviCRM', |
| 1253 | 'access CiviMember', |
| 1254 | 'edit memberships', |
| 1255 | 'access CiviContribute', |
| 1256 | 'edit contributions', |
| 1257 | ), |
| 1258 | 'delete' => array( |
| 1259 | 'access CiviCRM', |
| 1260 | 'access CiviMember', |
| 1261 | 'delete in CiviMember', |
| 1262 | 'access CiviContribute', |
| 1263 | 'delete in CiviContribute', |
| 1264 | ), |
| 1265 | 'get' => array( |
| 1266 | 'access CiviCRM', |
| 1267 | 'access CiviMember', |
| 1268 | 'access CiviContribute', |
| 1269 | ), |
| 1270 | 'update' => array( |
| 1271 | 'access CiviCRM', |
| 1272 | 'access CiviMember', |
| 1273 | 'edit memberships', |
| 1274 | 'access CiviContribute', |
| 1275 | 'edit contributions', |
| 1276 | ), |
| 1277 | ); |
| 1278 | |
| 1279 | // Participant permissions |
| 1280 | $permissions['participant'] = array( |
| 1281 | 'create' => array( |
| 1282 | 'access CiviCRM', |
| 1283 | 'access CiviEvent', |
| 1284 | 'register for events', |
| 1285 | ), |
| 1286 | 'delete' => array( |
| 1287 | 'access CiviCRM', |
| 1288 | 'access CiviEvent', |
| 1289 | 'edit event participants', |
| 1290 | ), |
| 1291 | 'get' => array( |
| 1292 | 'access CiviCRM', |
| 1293 | 'access CiviEvent', |
| 1294 | 'view event participants', |
| 1295 | ), |
| 1296 | 'update' => array( |
| 1297 | 'access CiviCRM', |
| 1298 | 'access CiviEvent', |
| 1299 | 'edit event participants', |
| 1300 | ), |
| 1301 | ); |
| 1302 | $permissions['participant_payment'] = array( |
| 1303 | 'create' => array( |
| 1304 | 'access CiviCRM', |
| 1305 | 'access CiviEvent', |
| 1306 | 'register for events', |
| 1307 | 'access CiviContribute', |
| 1308 | 'edit contributions', |
| 1309 | ), |
| 1310 | 'delete' => array( |
| 1311 | 'access CiviCRM', |
| 1312 | 'access CiviEvent', |
| 1313 | 'edit event participants', |
| 1314 | 'access CiviContribute', |
| 1315 | 'delete in CiviContribute', |
| 1316 | ), |
| 1317 | 'get' => array( |
| 1318 | 'access CiviCRM', |
| 1319 | 'access CiviEvent', |
| 1320 | 'view event participants', |
| 1321 | 'access CiviContribute', |
| 1322 | ), |
| 1323 | 'update' => array( |
| 1324 | 'access CiviCRM', |
| 1325 | 'access CiviEvent', |
| 1326 | 'edit event participants', |
| 1327 | 'access CiviContribute', |
| 1328 | 'edit contributions', |
| 1329 | ), |
| 1330 | ); |
| 1331 | |
| 1332 | // Pledge permissions |
| 1333 | $permissions['pledge'] = array( |
| 1334 | 'create' => array( |
| 1335 | 'access CiviCRM', |
| 1336 | 'access CiviPledge', |
| 1337 | 'edit pledges', |
| 1338 | ), |
| 1339 | 'delete' => array( |
| 1340 | 'access CiviCRM', |
| 1341 | 'access CiviPledge', |
| 1342 | 'delete in CiviPledge', |
| 1343 | ), |
| 1344 | 'get' => array( |
| 1345 | 'access CiviCRM', |
| 1346 | 'access CiviPledge', |
| 1347 | ), |
| 1348 | 'update' => array( |
| 1349 | 'access CiviCRM', |
| 1350 | 'access CiviPledge', |
| 1351 | 'edit pledges', |
| 1352 | ), |
| 1353 | ); |
| 1354 | |
| 1355 | //CRM-16777: Disable schedule reminder for user that have 'edit all events' and 'administer CiviCRM' permission. |
| 1356 | $permissions['action_schedule'] = array( |
| 1357 | 'update' => array( |
| 1358 | array( |
| 1359 | 'access CiviCRM', |
| 1360 | 'edit all events', |
| 1361 | ), |
| 1362 | ), |
| 1363 | ); |
| 1364 | |
| 1365 | $permissions['pledge_payment'] = array( |
| 1366 | 'create' => array( |
| 1367 | 'access CiviCRM', |
| 1368 | 'access CiviPledge', |
| 1369 | 'edit pledges', |
| 1370 | 'access CiviContribute', |
| 1371 | 'edit contributions', |
| 1372 | ), |
| 1373 | 'delete' => array( |
| 1374 | 'access CiviCRM', |
| 1375 | 'access CiviPledge', |
| 1376 | 'delete in CiviPledge', |
| 1377 | 'access CiviContribute', |
| 1378 | 'delete in CiviContribute', |
| 1379 | ), |
| 1380 | 'get' => array( |
| 1381 | 'access CiviCRM', |
| 1382 | 'access CiviPledge', |
| 1383 | 'access CiviContribute', |
| 1384 | ), |
| 1385 | 'update' => array( |
| 1386 | 'access CiviCRM', |
| 1387 | 'access CiviPledge', |
| 1388 | 'edit pledges', |
| 1389 | 'access CiviContribute', |
| 1390 | 'edit contributions', |
| 1391 | ), |
| 1392 | ); |
| 1393 | |
| 1394 | // Profile permissions |
| 1395 | $permissions['profile'] = array( |
| 1396 | 'get' => array(), // the profile will take care of this |
| 1397 | ); |
| 1398 | |
| 1399 | $permissions['uf_group'] = array( |
| 1400 | 'create' => array( |
| 1401 | 'access CiviCRM', |
| 1402 | array( |
| 1403 | 'administer CiviCRM', |
| 1404 | 'manage event profiles', |
| 1405 | ), |
| 1406 | ), |
| 1407 | 'get' => array( |
| 1408 | 'access CiviCRM', |
| 1409 | ), |
| 1410 | 'update' => array( |
| 1411 | 'access CiviCRM', |
| 1412 | array( |
| 1413 | 'administer CiviCRM', |
| 1414 | 'manage event profiles', |
| 1415 | ), |
| 1416 | ), |
| 1417 | ); |
| 1418 | $permissions['uf_field'] = $permissions['uf_join'] = $permissions['uf_group']; |
| 1419 | $permissions['uf_field']['delete'] = array( |
| 1420 | 'access CiviCRM', |
| 1421 | array( |
| 1422 | 'administer CiviCRM', |
| 1423 | 'manage event profiles', |
| 1424 | ), |
| 1425 | ); |
| 1426 | $permissions['option_value'] = $permissions['uf_group']; |
| 1427 | $permissions['option_group'] = $permissions['option_value']; |
| 1428 | |
| 1429 | $permissions['message_template'] = array( |
| 1430 | 'get' => array('access CiviCRM'), |
| 1431 | 'create' => array('edit message templates'), |
| 1432 | 'update' => array('edit message templates'), |
| 1433 | ); |
| 1434 | return $permissions; |
| 1435 | } |
| 1436 | |
| 1437 | /** |
| 1438 | * Translate an unknown action to a canonical form. |
| 1439 | * |
| 1440 | * @param string $action |
| 1441 | * |
| 1442 | * @return string |
| 1443 | * the standardised action name |
| 1444 | */ |
| 1445 | public static function getGenericAction($action) { |
| 1446 | $snippet = substr($action, 0, 3); |
| 1447 | if ($action == 'replace' || $snippet == 'del') { |
| 1448 | // 'Replace' is a combination of get+create+update+delete; however, the permissions |
| 1449 | // on each of those will be tested separately at runtime. This is just a sniff-test |
| 1450 | // based on the heuristic that 'delete' tends to be the most closely guarded |
| 1451 | // of the necessary permissions. |
| 1452 | $action = 'delete'; |
| 1453 | } |
| 1454 | elseif ($action == 'setvalue' || $snippet == 'upd') { |
| 1455 | $action = 'update'; |
| 1456 | } |
| 1457 | elseif ($action == 'getfields' || $action == 'getfield' || $action == 'getspec' || $action == 'getoptions') { |
| 1458 | $action = 'meta'; |
| 1459 | } |
| 1460 | elseif ($snippet == 'get') { |
| 1461 | $action = 'get'; |
| 1462 | } |
| 1463 | return $action; |
| 1464 | } |
| 1465 | |
| 1466 | /** |
| 1467 | * Validate user permission across. |
| 1468 | * edit or view or with supportable acls. |
| 1469 | * |
| 1470 | * @return bool |
| 1471 | */ |
| 1472 | public static function giveMeAllACLs() { |
| 1473 | if (CRM_Core_Permission::check('view all contacts') || |
| 1474 | CRM_Core_Permission::check('edit all contacts') |
| 1475 | ) { |
| 1476 | return TRUE; |
| 1477 | } |
| 1478 | |
| 1479 | $session = CRM_Core_Session::singleton(); |
| 1480 | $contactID = $session->get('userID'); |
| 1481 | |
| 1482 | //check for acl. |
| 1483 | $aclPermission = self::getPermission(); |
| 1484 | if (in_array($aclPermission, array( |
| 1485 | CRM_Core_Permission::EDIT, |
| 1486 | CRM_Core_Permission::VIEW, |
| 1487 | )) |
| 1488 | ) { |
| 1489 | return TRUE; |
| 1490 | } |
| 1491 | |
| 1492 | // run acl where hook and see if the user is supplying an ACL clause |
| 1493 | // that is not false |
| 1494 | $tables = $whereTables = array(); |
| 1495 | $where = NULL; |
| 1496 | |
| 1497 | CRM_Utils_Hook::aclWhereClause(CRM_Core_Permission::VIEW, |
| 1498 | $tables, $whereTables, |
| 1499 | $contactID, $where |
| 1500 | ); |
| 1501 | return empty($whereTables) ? FALSE : TRUE; |
| 1502 | } |
| 1503 | |
| 1504 | /** |
| 1505 | * Get component name from given permission. |
| 1506 | * |
| 1507 | * @param string $permission |
| 1508 | * |
| 1509 | * @return null|string |
| 1510 | * the name of component. |
| 1511 | */ |
| 1512 | public static function getComponentName($permission) { |
| 1513 | $componentName = NULL; |
| 1514 | $permission = trim($permission); |
| 1515 | if (empty($permission)) { |
| 1516 | return $componentName; |
| 1517 | } |
| 1518 | |
| 1519 | static $allCompPermissions = array(); |
| 1520 | if (empty($allCompPermissions)) { |
| 1521 | $components = CRM_Core_Component::getComponents(); |
| 1522 | foreach ($components as $name => $comp) { |
| 1523 | //get all permissions of each components unconditionally |
| 1524 | $allCompPermissions[$name] = $comp->getPermissions(TRUE); |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | if (is_array($allCompPermissions)) { |
| 1529 | foreach ($allCompPermissions as $name => $permissions) { |
| 1530 | if (array_key_exists($permission, $permissions)) { |
| 1531 | $componentName = $name; |
| 1532 | break; |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | return $componentName; |
| 1538 | } |
| 1539 | |
| 1540 | /** |
| 1541 | * Get all the contact emails for users that have a specific permission. |
| 1542 | * |
| 1543 | * @param string $permissionName |
| 1544 | * Name of the permission we are interested in. |
| 1545 | * |
| 1546 | * @return string |
| 1547 | * a comma separated list of email addresses |
| 1548 | */ |
| 1549 | public static function permissionEmails($permissionName) { |
| 1550 | $config = CRM_Core_Config::singleton(); |
| 1551 | return $config->userPermissionClass->permissionEmails($permissionName); |
| 1552 | } |
| 1553 | |
| 1554 | /** |
| 1555 | * Get all the contact emails for users that have a specific role. |
| 1556 | * |
| 1557 | * @param string $roleName |
| 1558 | * Name of the role we are interested in. |
| 1559 | * |
| 1560 | * @return string |
| 1561 | * a comma separated list of email addresses |
| 1562 | */ |
| 1563 | public static function roleEmails($roleName) { |
| 1564 | $config = CRM_Core_Config::singleton(); |
| 1565 | return $config->userRoleClass->roleEmails($roleName); |
| 1566 | } |
| 1567 | |
| 1568 | /** |
| 1569 | * @return bool |
| 1570 | */ |
| 1571 | public static function isMultisiteEnabled() { |
| 1572 | return Civi::settings()->get('is_enabled') ? TRUE : FALSE; |
| 1573 | } |
| 1574 | |
| 1575 | /** |
| 1576 | * Verify if the user has permission to get the invoice. |
| 1577 | * |
| 1578 | * @return bool |
| 1579 | * TRUE if the user has download all invoices permission or download my |
| 1580 | * invoices permission and the invoice author is the current user. |
| 1581 | */ |
| 1582 | public static function checkDownloadInvoice() { |
| 1583 | global $user; |
| 1584 | $cid = CRM_Core_BAO_UFMatch::getContactId($user->uid); |
| 1585 | if (CRM_Core_Permission::check('access CiviContribute') || |
| 1586 | (CRM_Core_Permission::check('view my invoices') && $_GET['cid'] == $cid) |
| 1587 | ) { |
| 1588 | return TRUE; |
| 1589 | } |
| 1590 | return FALSE; |
| 1591 | } |
| 1592 | |
| 1593 | } |