Last active
March 16, 2016 22:47
-
-
Save lucasconstantino/18acbc5f24d8bcc467bf to your computer and use it in GitHub Desktop.
Permission managment helper for Drupal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Helper to grant/revoke permissions. | |
* | |
* @param | |
* A associative array map of permissions to grant/remove. | |
* Example: | |
* array( | |
* 'all' => array( | |
* 'use Search Autocomplete' => TRUE, // Sample for granting to all user roles. | |
* ), | |
* 'role name' => array( | |
* 'granting permission' => TRUE, // Sample for granting a permission. | |
* 'removing permission' => FALSE, // Sample for removing a permission. | |
* ), | |
* ); | |
*/ | |
function set_permissions($permission_map = array(), $severity = WATCHDOG_WARNING) { | |
$all_permissions = user_permission_get_modules(); | |
$user_roles = user_roles(); | |
foreach ($user_roles as $rid => $role_name) { | |
$assigning_permissions = empty($permission_map[$role_name]) ? array() : $permission_map[$role_name]; | |
$assigning_permissions += empty($permission_map['all']) ? array() : $permission_map['all']; | |
// Clean-up non-existing permissions. | |
foreach ($assigning_permissions as $permission_name => $change) { | |
if (empty($all_permissions[$permission_name])) { | |
watchdog('Set permission', t('Permission "%permission" does not exist and could not be %action to %role.', array( | |
'%permission' => $permission_name, | |
'%role' => $role_name, | |
'%action' => $assigning_permissions[$permission_name] ? t('granted') : t('revoked'), | |
)), NULL, $severity); | |
unset($assigning_permissions[$permission_name]); | |
} | |
} | |
user_role_change_permissions($rid, $assigning_permissions); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment