You are here

function Scope::scopeExists in OAuth2 Server 7

Check if the provided scope exists in storage.

Parameters

$scope: A space-separated string of scopes.

$client_id: The requesting client.

Return value

bool TRUE if it exists, FALSE otherwise.

File

lib/Drupal/oauth2_server/Scope.php, line 50

Class

Scope
Provides a scope-checking utility to the library.

Namespace

Drupal\oauth2_server

Code

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

  // Get all scope entities that match the provided scope.
  // Compare the difference.
  $query = new \EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'oauth2_server_scope');
  $query
    ->propertyCondition('server', $this->server->name);
  $query
    ->propertyCondition('name', $scope);
  $query
    ->addTag('oauth2_server_scope_access');
  $query
    ->addMetaData('oauth2_server', $this->server);
  $results = $query
    ->execute();
  if ($results) {
    $scope_ids = array_keys($results['oauth2_server_scope']);
    $loaded_scopes = entity_load('oauth2_server_scope', $scope_ids);
    $found_scope = array();
    foreach ($loaded_scopes as $loaded_scope) {
      $found_scope[] = $loaded_scope->name;
    }
    return count(array_diff($scope, $found_scope)) == 0;
  }
  return FALSE;
}