8.5.x user.module user_role_grant_permissions($rid, array $permissions = [])
8.0.x user.module user_role_grant_permissions($rid, array $permissions = array())
8.1.x user.module user_role_grant_permissions($rid, array $permissions = array())
8.2.x user.module user_role_grant_permissions($rid, array $permissions = array())
8.3.x user.module user_role_grant_permissions($rid, array $permissions = [])
8.4.x user.module user_role_grant_permissions($rid, array $permissions = [])
8.6.x user.module user_role_grant_permissions($rid, array $permissions = [])
7.x user.module user_role_grant_permissions($rid, array $permissions = array())

Grant permissions to a user role.


$rid: The ID of a user role to alter.

$permissions: A list of permission names to grant.

See also



14 calls to user_role_grant_permissions()
BookTestCase::testBookExport in modules/book/book.test
Tests book export ("printer-friendly version") functionality.
CommentBlockFunctionalTest::testRecentCommentBlock in modules/comment/comment.test
Test the recent comments block.
ContactPersonalTestCase::testPersonalContactAccess in modules/contact/contact.test
Tests access to the personal contact form.
ContactSitewideTestCase::testSiteWideContact in modules/contact/contact.test
Tests configuration options and the site-wide contact form.
DrupalWebTestCase::drupalCreateRole in modules/simpletest/drupal_web_test_case.php
Creates a role with specified permissions.

... See full list


modules/user/user.module, line 3162
Enables the user registration and login system.


function user_role_grant_permissions($rid, array $permissions = array()) {
  $modules = user_permission_get_modules();

  // Grant new permissions for the role.
  foreach ($permissions as $name) {
      'rid' => $rid,
      'permission' => $name,
      'module' => $modules[$name],

  // Clear the user access cache.


abel_osorio’s picture

If the permission doesn't exists, then the db_merge() fails. Should the user_role_grant_permissions() check this?

I had to do this:

$permissions_modules = user_permission_get_modules();

foreach ($laridae_roles as $name => $default_permissions) {
    $permissions = array();
    $role = user_role_load_by_name($name);
    if (!is_object($role)) {
      $role = new stdClass();
      $role->name = $name;


    foreach ($default_permissions as $key => $perm) {

      /** THIS **/
      if (empty($permissions_modules[$perm])) {
        drupal_set_message(t('El permiso :perm no existe, se ignora.',
                             array(':perm' => $perm)), 'warning');

        /** **/

    user_role_grant_permissions($role->rid, $default_permissions);


Thanks in advance.

PrineShazar’s picture

I wanted to check before i executed user_role_grant_permissions(), heres my steps.

  // List of  permissions from lots of modules. This is an example
  $permission_for_role = array(
    'Workbench' => array(
        'access workbench',
      'Workbench Access' => array(
        'access workbench access by role',
        'view workbench access information',
     'Node' => array(
      'access content overview',

  // All permissions implemented in system.
  $system_module_perms = array_keys(user_permission_get_modules());
  // Permissions we cant use as modules havent implemented them. eg workbench not installed.
  $perms_not_available = array_diff($permission_for_role, $system_module_perms);
  // Use perms allowed to be implemented .
  $perms_final = array_diff($permission_for_role, $perms_not_available);

  user_role_grant_permissions($drupal_role->rid, $perms_final);
bsandor’s picture

Important to know that this function does not work during install or enable hooks.

rustyy’s picture

Can't confirm that. Just used it in hook_install and it worked

delaBruyne’s picture

I don't agree :) I'm using this hook and it's working very well :

 * Add permissions.
function lth_configuration_update_7010() {
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('permission name')); // Anonymous users
  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('permission name')); // Authentificated users
  user_role_grant_permissions(3, array('permission name')); // Role ID