Skip to content
Snippets Groups Projects
Commit 5dee4a74 authored by Sean Madsen's avatar Sean Madsen
Browse files

hook_civicrm_alterAPIPermissions - clean up after content import

Fixes #79
parent 765bd9e4
No related branches found
No related tags found
No related merge requests found
...@@ -3,28 +3,31 @@ ...@@ -3,28 +3,31 @@
## Description ## Description
This hook is called when API 3 permissions are checked and can alter the This hook is called when API 3 permissions are checked and can alter the
$permissions structure from CRM/Core/DAO/.permissions.php (as well as `$permissions` structure from `CRM/Core/DAO/permissions.php` (as well as
the API $params array) based on the $entity and $action (or the API `$params` array) based on the `$entity` and `$action` (or
unconditionally). unconditionally).
Note that if a given entity/action permissions are unset, the default !!! Note
‘access CiviCRM’ permission is enforced. If a given entity/action permissions are unset, the default
"access CiviCRM" permission is enforced.
Note also that the entity in $permissions array use the camel case
syntax (e.g. $permissions['option_group']['get'] = ... and not
$permissions['OptionGroup']['get'] = ...)
## Definition ## Definition
hook_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) ```php
hook_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions)
```
## Parameters ## Parameters
- string $entity the API entity (like contact) - string `$entity` - the API entity (like contact)
- string $action the API action (like get) - string `$action` - the API action (like get)
- array &$params the API parameters - array `&$params` - the API parameters
- array &$permisisons the associative permissions array (probably to - array `&$permisisons` - the associative permissions array (probably to
be altered by this hook) be altered by this hook)
- Note: the entity in `$permissions` array use the camel case
syntax (e.g. `$permissions['option_group']['get'] = ...` and not
`$permissions['OptionGroup']['get'] = ...`)
## Returns ## Returns
...@@ -36,39 +39,31 @@ $permissions['OptionGroup']['get'] = ...) ...@@ -36,39 +39,31 @@ $permissions['OptionGroup']['get'] = ...)
## Example ## Example
/** ```php
* alterAPIPermissions() hook allows you to change the permissions checked when doing API 3 calls. function civitest_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions)
*/ {
function civitest_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) // skip permission checks for contact/create calls
{ // (but keep the ones for email, address, etc./create calls)
// skip permission checks for contact/create calls // note: unsetting the below would require the default ‘access CiviCRM’ permission
// (but keep the ones for email, address, etc./create calls) $permissions['contact']['create'] = array();
// note: unsetting the below would require the default ‘access CiviCRM’ permission
$permissions['contact']['create'] = array();
// enforce ‘view all contacts’ check for contact/get, but do not test ‘access CiviCRM’ // enforce ‘view all contacts’ check for contact/get, but do not test ‘access CiviCRM’
$permissions['contact']['get'] = array('view all contacts'); $permissions['contact']['get'] = array('view all contacts');
// add a new permission requirement for your own custom API call // add a new permission requirement for your own custom API call
// (if all you want to enforce is ‘access CiviCRM’ you can skip the below altogether) // (if all you want to enforce is ‘access CiviCRM’ you can skip the below altogether)
$permissions['foo']['get'] = array('access CiviCRM', 'get all foos'); $permissions['foo']['get'] = array('access CiviCRM', 'get all foos');
// allow everyone to get info for a given event; also – another way to skip permissions // allow everyone to get info for a given event; also – another way to skip permissions
if ($entity == 'event' and $action == 'get' and $params['title'] == 'CiviCon 2038') { if ($entity == 'event' and $action == 'get' and $params['title'] == 'CiviCon 2038') {
$params['check_permissions'] = false; $params['check_permissions'] = false;
} }
} }
```
## Notes on Example
When developing an extension with custom API, this code is placed When developing an extension with custom API, this code is placed
directly in the API php file that you have created. In this case the directly in the API php file that you have created. In this case the
extension would be named CiviTest. The API function for the GET would be extension would be named CiviTest. The API function for the GET would be
: function civicrm_api3_civi_test_get(); The alterAPIPermissions `function civicrm_api3_civi_test_get();`. The `alterAPIPermissions`
function is prefixed with the full extension name, all lowercase, function is prefixed with the full extension name, all lowercase,
followed by "_civicrm_alterAPIPermissions". followed by `_civicrm_alterAPIPermissions`.
There seems to be a bit of inconsistency between civiCRM 4.2.6 and
civiCRM 4.2.13. See attached screen.\
![](https://wiki.civicrm.org/confluence/download/attachments/86213391/IMG_26112013_110442.png?version=1&modificationDate=1385467332000&api=v2)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment