function login_history_user_login in Login History 7
Same name and namespace in other branches
- 8 login_history.module \login_history_user_login()
Implements hook_user_login().
File
- ./
login_history.module, line 21 - The login history module.
Code
function login_history_user_login(&$edit, $account) {
// Is this a one-time login?
$menu_item = menu_get_item();
if ('user/reset/%/%/%' == $menu_item['path']) {
$one_time = 1;
}
else {
$one_time = 0;
}
// Validate and parse the cookie.
module_load_include('inc', 'login_history', 'login_history');
try {
$old_device_id = login_history_get_device_id_from_cookie($_COOKIE, drupal_get_hash_salt());
} catch (Exception $e) {
$old_device_id = '';
watchdog_exception('login_history', $e, NULL, array(), WATCHDOG_NOTICE);
}
// Perform some default gathering of info about the login event.
$detection = array(
'user_agent' => empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'],
);
// Allow other modules to add more info to the detection.
drupal_alter('login_history_detect_device', $detection, $edit, $account);
// Have a consistent order for the hash.
asort($detection);
$device_id = hash('sha256', implode('', $detection));
// Limit user agent strings to 255 characters. If a module cares about more
// (or less) data, it should create it's own schema and store the agent there.
// Now save the user's current login timestamp to login_history.
$login_id = db_insert('login_history')
->fields(array(
'uid' => $account->uid,
'login' => $account->login,
'hostname' => ip_address(),
'one_time' => $one_time,
'user_agent' => substr($detection['user_agent'], 0, 255),
'device_id' => $device_id,
'old_device_id' => $old_device_id,
))
->execute();
// TODO: would be useful to load the prior login data here e.g. a change of IP address that is
// still in the same geo location is less risky than a change of IP address across the world.
$login_history_cookie = login_history_assemble_cookie($device_id, $login_id, drupal_get_hash_salt());
user_cookie_save(array(
'login_history' => $login_history_cookie,
));
module_invoke_all('login_history_detection_results', $login_id, $detection, $old_device_id, $device_id, $account);
if (variable_get('login_history_mail_on_new_login_device', FALSE)) {
login_history_send_mail_new_login_device($login_id, $detection, $old_device_id, $device_id, $account);
}
}