You are here

public function ScopeUtility::getDefaultScope in OAuth2 Server 8

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

Get default scope.

Parameters

string|null $client_id: The client id string.

Return value

string The scope string.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/ScopeUtility.php, line 123

Class

ScopeUtility
Provides a scope-checking utility to the library.

Namespace

Drupal\oauth2_server

Code

public function getDefaultScope($client_id = NULL) {

  // Allow any hook_oauth2_server_default_scope() implementations to supply
  // the default scope. The first one to return a scope wins.
  foreach (\Drupal::moduleHandler()
    ->getImplementations('oauth2_server_default_scope') as $module) {
    $function = $module . '_' . 'oauth2_server_default_scope';
    $args = [
      $this->server,
    ];
    $result = call_user_func_array($function, $args);
    if (is_array($result)) {
      sort($result);
      return implode(' ', $result);
    }
  }

  // If there's a valid default scope set in server settings, return it.
  $default_scope = $this->server->settings['default_scope'];
  if (!empty($default_scope)) {

    /** @var \Drupal\oauth2_server\ScopeInterface[] $loaded_scope */
    $loaded_scope = \Drupal::entityTypeManager()
      ->getStorage('oauth2_server_scope')
      ->load($default_scope);
    if ($loaded_scope) {
      return $loaded_scope->scope_id;
    }
  }
  return FALSE;
}