You are here

public function TeamRoleStorage::changePermissions in Apigee Edge 8

Throws

\Drupal\apigee_edge_teams\Exception\InvalidArgumentException If team role does not exist.

\Drupal\Core\Entity\EntityStorageException If changes could not be saved.

Overrides TeamRoleStorageInterface::changePermissions

File

modules/apigee_edge_teams/src/Entity/Storage/TeamRoleStorage.php, line 39

Class

TeamRoleStorage
Team role entity storage.

Namespace

Drupal\apigee_edge_teams\Entity\Storage

Code

public function changePermissions(string $role_name, array $permissions) : void {

  /** @var \Drupal\apigee_edge_teams\Entity\TeamRoleInterface $role */
  $role = $this
    ->load($role_name);
  if ($role === NULL) {
    throw new InvalidArgumentException("Team role with name does not exist: {$role_name}");
  }

  // Grant new permissions for the role.
  $grant = array_filter($permissions);
  if (!empty($grant)) {
    foreach (array_keys($grant) as $permission) {
      $role
        ->grantPermission($permission);
    }
  }
  $revoke = array_diff_assoc($permissions, $grant);
  if (!empty($revoke)) {
    foreach (array_keys($revoke) as $permission) {
      $role
        ->revokePermission($permission);
    }
  }
  $role
    ->trustData()
    ->save();
}