function user_role_change_permissions

Same name in other branches
  1. 7.x modules/user/user.module \user_role_change_permissions()
  2. 9 core/modules/user/user.module \user_role_change_permissions()
  3. 10 core/modules/user/user.module \user_role_change_permissions()
  4. 11.x core/modules/user/user.module \user_role_change_permissions()

Change permissions for a user role.

This function may be used to grant and revoke multiple permissions at once. For example, when a form exposes checkboxes to configure permissions for a role, the form submit handler may directly pass the submitted values for the checkboxes form element to this function.

Parameters

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

array $permissions: (optional) An associative array, where the key holds the permission name and the value determines whether to grant or revoke that permission. Any value that evaluates to TRUE will cause the permission to be granted. Any value that evaluates to FALSE will cause the permission to be revoked.

array(
    'administer nodes' => 0,
    // Revoke 'administer nodes'
'administer blocks' => FALSE,
    // Revoke 'administer blocks'
'access user profiles' => 1,
    // Grant 'access user profiles'
'access content' => TRUE,
    // Grant 'access content'
'access comments' => 'access comments',
);

Existing permissions are not changed, unless specified in $permissions.

See also

user_role_grant_permissions()

user_role_revoke_permissions()

22 calls to user_role_change_permissions()
CommentAdminTest::testApprovalAdminInterface in core/modules/comment/tests/src/Functional/CommentAdminTest.php
Test comment approval functionality through admin/content/comment.
CommentAdminTest::testApprovalAdminInterface in core/modules/comment/tests/src/Functional/Views/CommentAdminTest.php
Test comment approval functionality through admin/content/comment.
CommentAdminTest::testApprovalNodeInterface in core/modules/comment/tests/src/Functional/CommentAdminTest.php
Tests comment approval functionality through the node interface.
CommentAnonymousTest::testAnonymous in core/modules/comment/tests/src/Functional/CommentAnonymousTest.php
Tests anonymous comment functionality.
CommentAttributesTest::setUp in core/modules/rdf/tests/src/Functional/CommentAttributesTest.php

... See full list

File

core/modules/user/user.module, line 1204

Code

function user_role_change_permissions($rid, array $permissions = []) {
    // Grant new permissions for the role.
    $grant = array_filter($permissions);
    if (!empty($grant)) {
        user_role_grant_permissions($rid, array_keys($grant));
    }
    // Revoke permissions for the role.
    $revoke = array_diff_assoc($permissions, $grant);
    if (!empty($revoke)) {
        user_role_revoke_permissions($rid, array_keys($revoke));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.