You are here

function login_security_set_login_timestamp in Login Security 2.x

Same name and namespace in other branches
  1. 8 login_security.module \login_security_set_login_timestamp()
  2. 6 login_security.module \login_security_set_login_timestamp()
  3. 7 login_security.module \login_security_set_login_timestamp()

Save login attempt and save login/access timestamps.

Previous incarnations of this code put it in hook_submit or hook_user, but since Drupal core validation updates the login timestamp, we have to set the message before it gets updated with the current login instance.

1 string reference to 'login_security_set_login_timestamp'
login_security_form_alter in ./login_security.module
Implements hook_form_alter().

File

./login_security.module, line 72
Login Security module hooks.

Code

function login_security_set_login_timestamp(array $form, FormStateInterface $form_state) {
  $account = \Drupal::database()
    ->select('users_field_data', 'u')
    ->fields('u', [
    'login',
    'access',
  ])
    ->condition('name', $form_state
    ->getValue('name'))
    ->condition('status', 1)
    ->execute()
    ->fetchAssoc();
  if (empty($account)) {
    return;
  }
  _login_security_login_timestamp($account['login']);
  _login_security_access_timestamp($account['access']);

  // Save entry in security log, Username and IP Address.
  $ip_address = \Drupal::request()
    ->getClientIp();
  _login_security_add_event($form_state
    ->getValue('name'), $ip_address);
}