You are here

public function OgRoleManager::getRolesByPermissions in Organic groups 8

Returns all the roles that have a specific permission.

Optionally filter the roles by entity type ID and bundle.

Parameters

array $permissions: An array of permissions that the roles must have.

string $entity_type_id: (optional) The entity type ID of the group.

string $bundle: (optional) The bundle of the group.

bool $require_all: (optional) Whether all given permissions are required. When set to FALSE all roles that include one or more of the given permissions will be returned. Defaults to TRUE.

Return value

\Drupal\og\OgRoleInterface[] An array of roles indexed by their IDs.

Overrides OgRoleManagerInterface::getRolesByPermissions

File

src/OgRoleManager.php, line 140

Class

OgRoleManager
Defines a manager of an OG role.

Namespace

Drupal\og

Code

public function getRolesByPermissions(array $permissions, $entity_type_id = NULL, $bundle = NULL, $require_all = TRUE) : array {
  $role_storage = $this
    ->ogRoleStorage();
  $query = $role_storage
    ->getQuery();
  if ($require_all) {

    // If all permissions are requested, we need to add an AND condition for
    // each permission because there is not an easy way to explicitly request
    // a subset of an array.
    foreach ($permissions as $permission) {
      $query
        ->condition('permissions.*', $permission);
    }
  }
  else {
    $query
      ->condition('permissions.*', $permissions, 'IN');
  }
  if (!empty($entity_type_id)) {
    $query
      ->condition('group_type', $entity_type_id);
  }
  if (!empty($bundle)) {
    $query
      ->condition('group_bundle', $bundle);
  }
  $role_ids = $query
    ->execute();
  return $role_storage
    ->loadMultiple($role_ids);
}