class ExpiredCollector in Simple OAuth (OAuth2) & OpenID Connect 8.4
Same name and namespace in other branches
- 8.2 src/ExpiredCollector.php \Drupal\simple_oauth\ExpiredCollector
- 8.3 src/ExpiredCollector.php \Drupal\simple_oauth\ExpiredCollector
- 5.x src/ExpiredCollector.php \Drupal\simple_oauth\ExpiredCollector
Service in charge of deleting or expiring tokens that cannot be used anymore.
Hierarchy
- class \Drupal\simple_oauth\ExpiredCollector
Expanded class hierarchy of ExpiredCollector
1 file declares its use of ExpiredCollector
- EntityCollectorTest.php in tests/
src/ Unit/ EntityCollectorTest.php
1 string reference to 'ExpiredCollector'
1 service uses ExpiredCollector
File
- src/
ExpiredCollector.php, line 14
Namespace
Drupal\simple_oauthView source
class ExpiredCollector {
/**
* The token storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $tokenStorage;
/**
* The client storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $clientStorage;
/**
* The date time to collect tokens.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $dateTime;
/**
* ExpiredCollector constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Component\Datetime\TimeInterface $date_time
* The date time service.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, TimeInterface $date_time) {
$this->clientStorage = $entity_type_manager
->getStorage('consumer');
$this->tokenStorage = $entity_type_manager
->getStorage('oauth2_token');
$this->dateTime = $date_time;
}
/**
* Collect all expired token ids.
*
* @param int $limit
* Number of tokens to fetch.
*
* @return \Drupal\simple_oauth\Entity\Oauth2TokenInterface[]
* The expired tokens.
*/
public function collect($limit = 0) {
$query = $this->tokenStorage
->getQuery();
$query
->condition('expire', $this->dateTime
->getRequestTime(), '<');
// If limit available.
if (!empty($limit)) {
$query
->range(0, $limit);
}
if (!($results = $query
->execute())) {
return [];
}
return array_values($this->tokenStorage
->loadMultiple(array_values($results)));
}
/**
* Collect all the tokens associated with the provided account.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The account.
*
* @return \Drupal\simple_oauth\Entity\Oauth2TokenInterface[]
* The tokens.
*/
public function collectForAccount(AccountInterface $account) {
$query = $this->tokenStorage
->getQuery();
$query
->condition('auth_user_id', $account
->id());
$query
->condition('bundle', 'refresh_token', '!=');
$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) {
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);
}
/**
* Collect all the tokens associated a particular client.
*
* @param \Drupal\consumers\Entity\Consumer $client
* The account.
*
* @return \Drupal\simple_oauth\Entity\Oauth2TokenInterface[]
* The tokens.
*/
public function collectForClient(Consumer $client) {
$query = $this->tokenStorage
->getQuery();
$query
->condition('client', $client
->id());
if (!($entity_ids = $query
->execute())) {
return [];
}
/** @var \Drupal\simple_oauth\Entity\Oauth2TokenInterface[] $results */
$results = $this->tokenStorage
->loadMultiple(array_values($entity_ids));
return array_values($results);
}
/**
* Deletes multiple tokens based on ID.
*
* @param \Drupal\simple_oauth\Entity\Oauth2TokenInterface[] $tokens
* The token entity IDs.
*/
public function deleteMultipleTokens(array $tokens = []) {
$this->tokenStorage
->delete($tokens);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExpiredCollector:: |
protected | property | The client storage. | |
ExpiredCollector:: |
protected | property | The date time to collect tokens. | |
ExpiredCollector:: |
protected | property | The token storage. | |
ExpiredCollector:: |
public | function | Collect all expired token ids. | |
ExpiredCollector:: |
public | function | Collect all the tokens associated with the provided account. | |
ExpiredCollector:: |
public | function | Collect all the tokens associated a particular client. | |
ExpiredCollector:: |
public | function | Deletes multiple tokens based on ID. | |
ExpiredCollector:: |
public | function | ExpiredCollector constructor. |