You are here

public function ScopeUtility::scopeExists in OAuth2 Server 8

Same name and namespace in other branches
  1. 2.0.x src/ScopeUtility.php \Drupal\oauth2_server\ScopeUtility::scopeExists()

Check if the provided scope exists in storage.

Parameters

string $scope: A space-separated string of scopes.

string|null $client_id: The requesting client.

Return value

bool TRUE if it exists, FALSE otherwise.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/ScopeUtility.php, line 68

Class

ScopeUtility
Provides a scope-checking utility to the library.

Namespace

Drupal\oauth2_server

Code

public function scopeExists($scope, $client_id = NULL) {
  $scope = explode(' ', trim($scope));

  // Get all scope entities that match the provided scope.
  // Compare the difference.
  $query = \Drupal::entityQuery('oauth2_server_scope');
  $query
    ->condition('server_id', $this->server
    ->id());
  $query
    ->condition('scope_id', $scope);
  $results = $query
    ->execute();
  $scope_ids = array_keys($results);

  /** @var \Drupal\oauth2_server\ScopeInterface[] $loaded_scopes */
  $loaded_scopes = \Drupal::entityTypeManager()
    ->getStorage('oauth2_server_scope')
    ->loadMultiple($scope_ids);

  // Previously $query->addTag('oauth2_server_scope_access') was used but in
  // the config entities the query alter does not run. Use an alter.
  $context = [
    'scopes' => &$loaded_scopes,
    'server' => $this->server,
  ];
  \Drupal::moduleHandler()
    ->alter('oauth2_server_scope_access', $context);
  if ($loaded_scopes) {
    $found_scope = [];
    foreach ($loaded_scopes as $loaded_scope) {
      $found_scope[] = $loaded_scope
        ->label();
    }
    return count(array_diff($scope, $found_scope)) == 0;
  }
  return FALSE;
}