Files
materio-d9/web/modules/custom/login_tracker/login_tracker.module
T
2026-09-26 01:10:59 +02:00

76 lines
2.2 KiB
PHP

<?php
/**
* @file
* The main module file.
*
* The main module file contains hooks to create the permissions used by the
* module, to log the login data, and to expose the module to views.
*/
/**
* Implements hook_user_login().
*
* Stores the fact that the user has logged in. 3rd party modules may override
* whether a particular login is tracked by using
* hook_login_tracker_track_login_alter(). Modules may also provide additional
* data to be logged by using
* hook_login_tracker_login_data_alter().
*/
function login_tracker_user_login($account) {
// Do not track the login by default if the user's role is excluded from
// tracking.
$track_login = !$account->hasPermission('excluded_from_login_tracking');
// Allow other modules to override whether or not we're tracking this
// login.
\Drupal::moduleHandler()->alter('login_tracker_track_login', $track_login, $account);
if (!$track_login) {
return;
}
$data = [];
\Drupal::moduleHandler()->alter('login_tracker_login_data', $data, $account);
$data = serialize($data);
$fields = [
'uid' => $account->id(),
'login_timestamp' => \Drupal::time()->getRequestTime(),
'data' => $data,
];
// Core 11 narrowed Merge::key() to a string field, so the historical
// merge->key(['record_id' => NULL]) trick no longer works. A plain insert
// is exactly what it did (the NULL key never matched, forcing an insert
// per login).
\Drupal::database()->insert('login_tracker')
->fields($fields)
->execute();
}
/**
* Implements hook_cron().
*/
function login_tracker_cron() {
$settings = \Drupal::state()->get('login_tracker_settings', []);
if (!empty($settings['purge_interval'])) {
$query = \Drupal::database()->delete('login_tracker');
$query->condition('login_timestamp', time() - $settings['purge_interval'], '<=');
$results = $query->execute();
if ($results) {
\Drupal::logger('login_tracker')->notice(
t(
'@logs records have been purged with success from table @table',
['@logs' => $results, '@table' => 'login_tracker']
)
);
}
else {
\Drupal::logger('login_tracker')->notice(t('Nothing to purge from table @table', ['@table' => 'login_tracker']));
}
}
}