You are here

public function GroupRole::changePermissions in Group 7

Change permissions for a 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.

array(
  'administer users' => 0,
  // Revoke 'administer users'
  'administer group' => FALSE,
  // Revoke 'administer group'
  'create subgroups' => 1,
  // Grant 'create subgroups'
  'edit group' => TRUE,
  // Grant 'edit group'
  'view group' => 'view group',
);

Parameters

$permissions: 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. Existing permissions are not changed, unless specified in $permissions.

See also

GroupRole::grantPermissions()

GroupRole::revokePermissions()

File

classes/group_role.inc, line 114
Defines the Entity API class for group roles.

Class

GroupRole
Main class for group role entities.

Code

public function changePermissions(array $permissions) {

  // Grant new permissions for the role.
  $grant = array_filter($permissions);
  if (!empty($grant)) {
    $this
      ->grantPermissions(array_keys($grant));
  }

  // Revoke permissions for the role.
  $revoke = array_diff_assoc($permissions, $grant);
  if (!empty($revoke)) {
    $this
      ->revokePermissions(array_keys($revoke));
  }
}