You are here

protected function ScopeRepository::addRoleToScopes in Simple OAuth (OAuth2) & OpenID Connect 5.x

Same name and namespace in other branches
  1. 8.4 src/Repositories/ScopeRepository.php \Drupal\simple_oauth\Repositories\ScopeRepository::addRoleToScopes()
  2. 8.2 src/Repositories/ScopeRepository.php \Drupal\simple_oauth\Repositories\ScopeRepository::addRoleToScopes()
  3. 8.3 src/Repositories/ScopeRepository.php \Drupal\simple_oauth\Repositories\ScopeRepository::addRoleToScopes()

Add an additional scope if it's not present.

Parameters

\League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes: The list of scopes.

string $additional_role_id: The role ID to add as a scope.

Return value

\League\OAuth2\Server\Entities\ScopeEntityInterface[] The modified list of scopes.

1 call to ScopeRepository::addRoleToScopes()
ScopeRepository::finalizeScopes in src/Repositories/ScopeRepository.php
This will remove any role that is not associated to the identified user and add all the roles configured in the client.

File

src/Repositories/ScopeRepository.php, line 113

Class

ScopeRepository
The repository for scopes.

Namespace

Drupal\simple_oauth\Repositories

Code

protected function addRoleToScopes(array $scopes, $additional_role_id) {
  $role_storage = $this->entityTypeManager
    ->getStorage('user_role');

  // Only add the role if it's not already in the list.
  $found = array_filter($scopes, function (ScopeEntityInterface $scope) use ($additional_role_id) {
    return $scope
      ->getIdentifier() == $additional_role_id;
  });
  if (empty($found)) {

    // If it's not there, then add the authenticated role.
    $additional_role = $role_storage
      ->load($additional_role_id);
    array_push($scopes, $this
      ->scopeFactory($additional_role));
  }
  return $scopes;
}