function logintoboggan_cron in LoginToboggan 8
Same name and namespace in other branches
- 6 logintoboggan.module \logintoboggan_cron()
- 7 logintoboggan.module \logintoboggan_cron()
Implements hook_cron().
If set password enabled, and a purge interval, purge untrusted users.
File
- ./
logintoboggan.module, line 28 - LoginToboggan module.
Code
function logintoboggan_cron() {
if (($purge_interval = \Drupal::config('logintoboggan.settings')
->get('purge_unvalidated_user_interval')) && Drupal::config('logintoboggan.settings')
->get('user_email_verification')) {
// As a safety check, check we have a non-core role as the trusted role.
$anon_role = User::ANONYMOUS_ROLE;
$auth_role = User::AUTHENTICATED_ROLE;
$trusted_role = LogintobogganUtility::trustedRole();
if (!in_array($trusted_role, [
$anon_role,
$auth_role,
])) {
$request_time = \Drupal::time()
->getRequestTime();
$purge_time = $request_time - $purge_interval;
$connection = Database::getConnection();
$no_role = [];
$query = $connection
->select('users_field_data', 'u');
$query
->leftJoin('user__roles', 'r', 'u.uid = r.entity_id');
$query
->fields('u', [
'uid',
]);
$query
->fields('r', [
'entity_id',
]);
$query
->isNull('entity_id');
$query
->condition('uid', '0', '<>');
$query
->condition('created', $purge_time, '<');
$res_query = $query
->execute()
->fetchAll();
foreach ($res_query as $k => $v) {
$no_role[] = $v->uid;
}
if (!empty($no_role)) {
$purged_users = [];
$users = User::loadMultiple($no_role);
foreach ($users as $u) {
$purged_users[] = $u
->getAccountName();
}
$purged_users_list = implode(', ', $purged_users);
user_delete_multiple($no_role);
// Log the purged users.
if (!empty($purged_users)) {
\Drupal::logger('logintoboggan')
->notice('Following unvalidated users removed: %purged', [
'%purged' => $purged_users_list,
]);
}
}
}
}
}