From cf1cb686624c80f987540a51be2bcc57da20c930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sun, 19 Aug 2012 13:11:15 +0200 Subject: [PATCH] MDL-34901 fix user login times handling --- auth/email/auth.php | 4 +++- auth/ldap/auth.php | 4 +++- auth/manual/auth.php | 4 +++- lib/moodlelib.php | 23 ++++++++++++++++------- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/auth/email/auth.php b/auth/email/auth.php index 0051aafdefc..e50c09e44ee 100644 --- a/auth/email/auth.php +++ b/auth/email/auth.php @@ -132,7 +132,9 @@ class auth_plugin_email extends auth_plugin_base { } else if ($user->secret == $confirmsecret) { // They have provided the secret key to get in $DB->set_field("user", "confirmed", 1, array("id"=>$user->id)); - $DB->set_field("user", "firstaccess", time(), array("id"=>$user->id)); + if ($user->firstaccess == 0) { + $DB->set_field("user", "firstaccess", time(), array("id"=>$user->id)); + } return AUTH_CONFIRM_OK; } } else { diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php index bc90ebc10e0..88bb10d3f80 100644 --- a/auth/ldap/auth.php +++ b/auth/ldap/auth.php @@ -546,7 +546,9 @@ class auth_plugin_ldap extends auth_plugin_base { return AUTH_CONFIRM_FAIL; } $DB->set_field('user', 'confirmed', 1, array('id'=>$user->id)); - $DB->set_field('user', 'firstaccess', time(), array('id'=>$user->id)); + if ($user->firstaccess == 0) { + $DB->set_field('user', 'firstaccess', time(), array('id'=>$user->id)); + } return AUTH_CONFIRM_OK; } } else { diff --git a/auth/manual/auth.php b/auth/manual/auth.php index e3df78a3f0d..29cb59ae2ef 100644 --- a/auth/manual/auth.php +++ b/auth/manual/auth.php @@ -170,7 +170,9 @@ class auth_plugin_manual extends auth_plugin_base { return AUTH_CONFIRM_ALREADY; } else { $DB->set_field("user", "confirmed", 1, array("id"=>$user->id)); - $DB->set_field("user", "firstaccess", time(), array("id"=>$user->id)); + if ($user->firstaccess == 0) { + $DB->set_field("user", "firstaccess", time(), array("id"=>$user->id)); + } return AUTH_CONFIRM_OK; } } else { diff --git a/lib/moodlelib.php b/lib/moodlelib.php index f229b6b3ef2..104da29fa6b 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3283,12 +3283,25 @@ function get_user_key($script, $userid, $instance=null, $iprestriction=null, $va function update_user_login_times() { global $USER, $DB; - $user = new stdClass(); - $USER->lastlogin = $user->lastlogin = $USER->currentlogin; - $USER->currentlogin = $user->lastaccess = $user->currentlogin = time(); + $now = time(); + $user = new stdClass(); $user->id = $USER->id; + // Make sure all users that logged in have some firstaccess. + if ($USER->firstaccess == 0) { + $USER->firstaccess = $user->firstaccess = $now; + } + + // Store the previous current as lastlogin. + $USER->lastlogin = $user->lastlogin = $USER->currentlogin; + + $USER->currentlogin = $user->currentlogin = $now; + + // Function user_accesstime_log() may not update immediately, better do it here. + $USER->lastaccess = $user->lastaccess = $now; + $USER->lastip = $user->lastip = getremoteaddr(); + $DB->update_record('user', $user); return true; } @@ -4074,10 +4087,6 @@ function authenticate_user_login($username, $password) { $DB->set_field('user', 'auth', $auth, array('username'=>$username)); $user->auth = $auth; } - if (empty($user->firstaccess)) { //prevent firstaccess from remaining 0 for manual account that never required confirmation - $DB->set_field('user','firstaccess', $user->timemodified, array('id' => $user->id)); - $user->firstaccess = $user->timemodified; - } update_internal_user_password($user, $password); // just in case salt or encoding were changed (magic quotes too one day)