function media_acquiadam_purge_expired_tokens in Media: Acquia DAM 8
Purge Acquia DAM authentication tokens for inactive Drupal users.
1 call to media_acquiadam_purge_expired_tokens()
- media_acquiadam_cron in ./
media_acquiadam.module - Implements hook_cron().
File
- ./
media_acquiadam.module, line 334 - Integrates Drupal with Acquia DAM.
Code
function media_acquiadam_purge_expired_tokens() {
$cookie_params = session_get_cookie_params();
// 2000000 is the Drupal default value for cookie lifetime (approx 23 days).
$lifetime = $cookie_params['lifetime'] ?: 2000000;
// The user data keys associated with DAM authentication.
$purgable_keys = [
'acquiadam_access_token',
'acquiadam_refresh_token',
'acquiadam_access_token_expiration',
];
try {
$db = Drupal::database();
$expired_tokens_query = $db
->select('users_data', 'ud_expired')
->fields('ud_expired', [
'uid',
])
->condition('ud_expired.module', 'media_acquiadam')
->condition('ud_expired.name', 'acquiadam_access_token_expiration')
->condition('ud_expired.value', \Drupal::time()
->getRequestTime() - $lifetime, '<=');
// Select our UID list into a temporary table so we can delete without
// involving PHP. queryTemporary requires a string-based query at this time.
$temporary_table = $db
->queryTemporary((string) $expired_tokens_query, $expired_tokens_query
->getArguments());
$temporary_query = $db
->select($temporary_table, 'tt')
->fields('tt', [
'uid',
]);
$deleted_count = $db
->delete('users_data')
->condition('module', 'media_acquiadam')
->condition('name', $purgable_keys, 'IN')
->condition('uid', $temporary_query, 'IN')
->execute();
if ($deleted_count > 0) {
Drupal::logger('media_acquiadam')
->info('Deleted @count records (approx. @estimate inactive users).', [
'@count' => $deleted_count,
'@estimate' => intval($deleted_count / count($purgable_keys)),
]);
}
} catch (Exception $x) {
Drupal::logger('media_acquiadam')
->error('Unable to purge old authentication tokens.');
watchdog_exception('media_acquiadam', $x);
}
}