9/6/2015 - 9:57 AM


permissions drupal

/* If your module needs to define permissions in code, Drupal 7 provides some new hooks to make the task easier. Let's take a common example. Your module creates
a page callback that should be visible by 'authenticated' but not 'anonymous' users. To activate this feature when the module is enabled, you can use hook_enable()
as follows: */
function example_enable() {
$permissions = array('view example page');
This function goes into your module's .install file. When the module is enabled, Drupal will add the view example page permission to the authenticated user role.
You can (and normally should) do the reverse when the module is disabled: */ 
  function example_disable() {
  $permissions = array('view example page');
  $roles = user_roles();
// Since permissions can be set per role, remove our permission from
// each role.
  foreach ($roles as $rid => $name) {
    user_role_revoke_permissions($rid, $permissions);


It is also possible to add/remove multiple permissions at the same time. To do so, we must build an array of permissions to be passed to user_role_change_permissions(). Suppose that our module wants to remove the default access content permission from the anonymous user role, while adding our new view example page permission. To do so, we build an array in the format 'permission name' => TRUE or FALSE, for each role.

function example_enable() {
$permissions = array(
       'access content' => FALSE,
       'view example page' => TRUE,
  user_role_change_permissions(DRUPAL_ANONYMOUS_USER, $permissions);