event_log_track_auth.module in Events Log Track 8
Same filename and directory in other branches
Logs user authentication in the event_log_track module.
File
event_log_track_auth/event_log_track_auth.moduleView source
<?php
/**
* @file
* Logs user authentication in the event_log_track module.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_event_log_track_handlers().
*/
function event_log_track_auth_event_log_track_handlers() {
// User Authentication event log handler.
$handlers = array();
$handlers['authentication'] = array(
'title' => t('User authentication'),
'form_ids' => array(
'user_login_form',
'user_pass',
),
'form_submit_callback' => 'event_log_track_auth_form_submit',
);
return $handlers;
}
/**
* Event log callback for the user authentication event log.
*
* @return array
* Return an associative array of data to insert in database.
*/
function event_log_track_auth_form_submit($form, $form_state, $form_id) {
$account = \Drupal::currentUser();
$log = NULL;
switch ($form_id) {
case 'user_login_form':
$log = array(
'operation' => 'login',
'description' => t('%user (uid %uid)', array(
'%user' => $account
->getUsername(),
'%uid' => $account
->id(),
)),
'ref_numeric' => $account
->id(),
'ref_char' => $account
->getUsername(),
);
break;
case 'user_pass':
$log = array(
'operation' => 'request password',
'description' => t('%user (uid %uid)', array(
'%user' => $form_state
->getValue('name'),
'%uid' => $form_state
->getValue('account')
->id(),
)),
'ref_numeric' => $form_state
->getValue('account')
->id(),
'ref_char' => $form_state
->getValue('name'),
);
break;
}
return $log;
}
/**
* Implements hook_user_logout().
*/
function event_log_track_auth_user_logout($account) {
$log = array(
'type' => 'authentication',
'operation' => 'logout',
'description' => t('%user (uid %uid)', array(
'%user' => $account
->getUsername(),
'%uid' => $account
->id(),
)),
'ref_numeric' => $account
->id(),
'ref_char' => $account
->getUsername(),
);
event_log_track_insert($log);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function event_log_track_auth_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#validate'][] = 'event_log_track_auth_user_login_validate';
}
/**
* Implements hook_form_FORM_ID_validate().
*/
function event_log_track_auth_user_login_validate($form, &$form_state) {
// Check for erros and log them.
$errors = $form_state
->getErrors();
if (!empty($errors)) {
$log = array(
'type' => 'authentication',
'operation' => 'fail',
'description' => t('%user (pass %pass)', array(
'%user' => $form_state
->getValue('name'),
'%pass' => $form_state
->getValue('pass'),
)),
'ref_char' => $form_state
->getValue('name'),
);
event_log_track_insert($log);
}
}
Functions
Name | Description |
---|---|
event_log_track_auth_event_log_track_handlers | Implements hook_event_log_track_handlers(). |
event_log_track_auth_form_submit | Event log callback for the user authentication event log. |
event_log_track_auth_form_user_login_form_alter | Implements hook_form_FORM_ID_alter(). |
event_log_track_auth_user_login_validate | Implements hook_form_FORM_ID_validate(). |
event_log_track_auth_user_logout | Implements hook_user_logout(). |