You are here

public function ExpiredCollector::collectForAccount in Simple OAuth (OAuth2) & OpenID Connect 8.2

Same name and namespace in other branches
  1. 8.4 src/ExpiredCollector.php \Drupal\simple_oauth\ExpiredCollector::collectForAccount()
  2. 8.3 src/ExpiredCollector.php \Drupal\simple_oauth\ExpiredCollector::collectForAccount()
  3. 5.x src/ExpiredCollector.php \Drupal\simple_oauth\ExpiredCollector::collectForAccount()

Collect all the tokens associated with the provided account.

Parameters

\Drupal\Core\Session\AccountInterface $account: The account.

Return value

\Drupal\simple_oauth\Entity\Oauth2TokenInterface[] The tokens.

File

src/ExpiredCollector.php, line 69

Class

ExpiredCollector
Service in charge of deleting or expiring tokens that cannot be used anymore.

Namespace

Drupal\simple_oauth

Code

public function collectForAccount(AccountInterface $account) {
  $query = $this->tokenStorage
    ->getQuery();
  $query
    ->condition('auth_user_id', $account
    ->id());
  $entity_ids = $query
    ->execute();
  $output = $entity_ids ? array_values($this->tokenStorage
    ->loadMultiple(array_values($entity_ids))) : [];

  // Also collect the tokens of the clients that have this account as the
  // default user.
  try {
    $clients = array_values($this->clientStorage
      ->loadByProperties([
      'user_id' => $account
        ->id(),
    ]));
  } catch (QueryException $exception) {

    // This happens when simple_oauth_extras is not enabled because the
    // 'user_id' field is not available.
    return $output;
  }

  // Append all the tokens for each of the clients having this account as the
  // default.
  $tokens = array_reduce($clients, function ($carry, $client) {
    return array_merge($carry, $this
      ->collectForClient($client));
  }, $output);

  // Return a unique list.
  $existing = [];
  foreach ($tokens as $token) {
    $existing[$token
      ->id()] = $token;
  }
  return array_values($existing);
}