Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
082d771a | 13 | * Decide what permissions to check for an api call |
6a488035 | 14 | * |
353ffa53 TO |
15 | * @param $entity : (str) api entity |
16 | * @param $action : (str) api action | |
17 | * @param $params : (array) api params | |
6a488035 | 18 | * |
a6c01b45 | 19 | * @return array |
16b10e64 | 20 | * Array of permissions to check for this entity-action combo |
6a488035 | 21 | */ |
6a488035 | 22 | function _civicrm_api3_permissions($entity, $action, &$params) { |
4846df91 CW |
23 | // FIXME: Lowercase entity_names are nonstandard but difficult to fix here |
24 | // because this function invokes hook_civicrm_alterAPIPermissions | |
47e6af81 | 25 | $entity = _civicrm_api_get_entity_name_from_camel($entity); |
bf9a7c0f | 26 | $permissions = CRM_Core_Permission::getEntityActionPermissions(); |
2e27d447 | 27 | |
79089019 CW |
28 | // Translate 'create' action to 'update' if id is set |
29 | if ($action == 'create' && (!empty($params['id']) || !empty($params[$entity . '_id']))) { | |
30 | $action = 'update'; | |
31 | } | |
32 | ||
6a488035 TO |
33 | // let third parties modify the permissions |
34 | CRM_Utils_Hook::alterAPIPermissions($entity, $action, $params, $permissions); | |
35 | ||
79089019 | 36 | // Merge permissions for this entity with the defaults |
be2fb01f | 37 | $perm = CRM_Utils_Array::value($entity, $permissions, []) + $permissions['default']; |
79089019 CW |
38 | |
39 | // Return exact match if permission for this action has been declared | |
40 | if (isset($perm[$action])) { | |
41 | return $perm[$action]; | |
42 | } | |
43 | ||
44 | // Translate specific actions into their generic equivalents | |
bf9a7c0f ES |
45 | $action = CRM_Core_Permission::getGenericAction($action); |
46 | ||
2e1f50d6 | 47 | return $perm[$action] ?? $perm['default']; |
6a488035 | 48 | } |