Commit e79ac4ff authored by Nigel McNie's avatar Nigel McNie
Browse files

[sfd08] Add 'lastaccess' field to users.

There is a configuration parameter that sets how often this field is updated. This prevents DB writes every page load, though this behaviour can be achieved by setting the parameter to 0 if necessary.
parent dfe7170d
...@@ -63,6 +63,7 @@ class User { ...@@ -63,6 +63,7 @@ class User {
'expirymailsent' => 0, 'expirymailsent' => 0,
'lastlogin' => null, 'lastlogin' => null,
'lastlastlogin' => null, 'lastlastlogin' => null,
'lastaccess' => null, /* Is not necessarily updated every request, see accesstimeupdatefrequency config variable */
'lastauthinstance' => null, 'lastauthinstance' => null,
'inactivemailsent' => 0, 'inactivemailsent' => 0,
'staff' => 0, 'staff' => 0,
...@@ -108,7 +109,8 @@ class User { ...@@ -108,7 +109,8 @@ class User {
*, *,
' . db_format_tsfield('expiry') . ', ' . db_format_tsfield('expiry') . ',
' . db_format_tsfield('lastlogin') . ', ' . db_format_tsfield('lastlogin') . ',
' . db_format_tsfield('lastlastlogin') . ', ' . db_format_tsfield('lastlastlogin') . ',
' . db_format_tsfield('lastaccess') . ',
' . db_format_tsfield('suspendedctime') . ' ' . db_format_tsfield('suspendedctime') . '
FROM FROM
{usr} {usr}
...@@ -145,6 +147,7 @@ class User { ...@@ -145,6 +147,7 @@ class User {
' . db_format_tsfield('expiry') . ', ' . db_format_tsfield('expiry') . ',
' . db_format_tsfield('lastlogin') . ', ' . db_format_tsfield('lastlogin') . ',
' . db_format_tsfield('lastlastlogin') . ', ' . db_format_tsfield('lastlastlogin') . ',
' . db_format_tsfield('lastaccess') . ',
' . db_format_tsfield('suspendedctime') . ' ' . db_format_tsfield('suspendedctime') . '
FROM FROM
{usr} {usr}
...@@ -206,9 +209,10 @@ class User { ...@@ -206,9 +209,10 @@ class User {
$sql = 'SELECT $sql = 'SELECT
u.*, u.*,
' . db_format_tsfield('u.expiry', 'expiry') . ', ' . db_format_tsfield('u.expiry', 'expiry') . ',
' . db_format_tsfield('u.lastlogin', 'lastlogin') . ', ' . db_format_tsfield('u.lastlogin', 'lastlogin') . ',
' . db_format_tsfield('u.lastlastlogin', 'lastlastlogin') . ', ' . db_format_tsfield('u.lastlastlogin', 'lastlastlogin') . ',
' . db_format_tsfield('u.lastaccess', 'lastaccess') . ',
' . db_format_tsfield('u.suspendedctime', 'suspendedctime') . ' ' . db_format_tsfield('u.suspendedctime', 'suspendedctime') . '
FROM {usr} u FROM {usr} u
LEFT JOIN {auth_remote_user} r ON u.id = r.localusr LEFT JOIN {auth_remote_user} r ON u.id = r.localusr
...@@ -226,9 +230,10 @@ class User { ...@@ -226,9 +230,10 @@ class User {
else { else {
$sql = 'SELECT $sql = 'SELECT
*, *,
' . db_format_tsfield('expiry') . ', ' . db_format_tsfield('expiry') . ',
' . db_format_tsfield('lastlogin') . ', ' . db_format_tsfield('lastlogin') . ',
' . db_format_tsfield('lastlastlogin') . ', ' . db_format_tsfield('lastlastlogin') . ',
' . db_format_tsfield('lastaccess') . ',
' . db_format_tsfield('suspendedctime') . ' ' . db_format_tsfield('suspendedctime') . '
FROM FROM
{usr} {usr}
...@@ -399,7 +404,7 @@ class User { ...@@ -399,7 +404,7 @@ class User {
$this->stdclass = new StdClass; $this->stdclass = new StdClass;
reset($this->defaults); reset($this->defaults);
foreach (array_keys($this->defaults) as $k) { foreach (array_keys($this->defaults) as $k) {
if ($k == 'expiry' || $k == 'lastlogin' || $k == 'lastlastlogin' || $k == 'suspendedctime') { if ($k == 'expiry' || $k == 'lastlogin' || $k == 'lastlastlogin' || $k == 'lastaccess' || $k == 'suspendedctime') {
$this->stdclass->{$k} = db_format_timestamp($this->get($k)); $this->stdclass->{$k} = db_format_timestamp($this->get($k));
} else { } else {
$this->stdclass->{$k} = $this->get($k);//(is_null($this->get($k))? 'NULL' : $this->get($k)); $this->stdclass->{$k} = $this->get($k);//(is_null($this->get($k))? 'NULL' : $this->get($k));
...@@ -663,9 +668,10 @@ class LiveUser extends User { ...@@ -663,9 +668,10 @@ class LiveUser extends User {
public function login($username, $password) { public function login($username, $password) {
$sql = 'SELECT $sql = 'SELECT
*, *,
' . db_format_tsfield('expiry') . ', ' . db_format_tsfield('expiry') . ',
' . db_format_tsfield('lastlogin') . ', ' . db_format_tsfield('lastlogin') . ',
' . db_format_tsfield('lastlastlogin') . ', ' . db_format_tsfield('lastlastlogin') . ',
' . db_format_tsfield('lastaccess') . ',
' . db_format_tsfield('suspendedctime') . ' ' . db_format_tsfield('suspendedctime') . '
FROM FROM
{usr} {usr}
...@@ -726,14 +732,25 @@ class LiveUser extends User { ...@@ -726,14 +732,25 @@ class LiveUser extends User {
} }
/** /**
* Assuming that a session is already active for a user, this method * Updates information in a users' session once we know their session is
* retrieves the information from the session and creates a user object * continuing
* that the script can use
*
* @return object
*/ */
public function renew() { public function renew() {
$this->set('logout_time', time() + get_config('session_timeout')); $time = time();
$this->set('logout_time', $time + get_config('session_timeout'));
$oldlastaccess = $this->get('lastaccess');
// If there is an access time update frequency, we use a cookie to
// prevent updating before this time has expired.
// If it is set to zero, we always update the accesstime.
$accesstimeupdatefrequency = get_config('accesstimeupdatefrequency');
if ($accesstimeupdatefrequency == 0) {
$this->set('lastaccess', $time);
$this->commit();
}
else if ($oldlastaccess + $accesstimeupdatefrequency < $time) {
$this->set('lastaccess', $time);
$this->commit();
}
} }
/** /**
...@@ -761,6 +778,7 @@ class LiveUser extends User { ...@@ -761,6 +778,7 @@ class LiveUser extends User {
session_regenerate_id(true); session_regenerate_id(true);
$this->lastlastlogin = $this->lastlogin; $this->lastlastlogin = $this->lastlogin;
$this->lastlogin = time(); $this->lastlogin = time();
$this->lastaccess = time();
$this->sessionid = session_id(); $this->sessionid = session_id();
$this->logout_time = time() + get_config('session_timeout'); $this->logout_time = time() + get_config('session_timeout');
$this->sesskey = get_random_key(); $this->sesskey = get_random_key();
......
...@@ -138,4 +138,11 @@ $cfg->imagemaxheight = 1024; ...@@ -138,4 +138,11 @@ $cfg->imagemaxheight = 1024;
// paths and arguments for various system commands // paths and arguments for various system commands
$cfg->pathtounzip = '/usr/bin/unzip'; $cfg->pathtounzip = '/usr/bin/unzip';
$cfg->unzipdirarg = '-d'; $cfg->unzipdirarg = '-d';
// How often Mahara should update the last access time for users. Setting this
// lower means the field will be updated more regularly, but means a database
// write will be required for more requests.
// Setting it to zero means the access time will be updated every request
$cfg->accesstimeupdatefrequency = 300;
?> ?>
...@@ -82,6 +82,7 @@ ...@@ -82,6 +82,7 @@
<FIELD NAME="expirymailsent" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0"/> <FIELD NAME="expirymailsent" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0"/>
<FIELD NAME="lastlogin" TYPE="datetime" NOTNULL="false"/> <FIELD NAME="lastlogin" TYPE="datetime" NOTNULL="false"/>
<FIELD NAME="lastlastlogin" TYPE="datetime" NOTNULL="false"/> <FIELD NAME="lastlastlogin" TYPE="datetime" NOTNULL="false"/>
<FIELD NAME="lastaccess" TYPE="datetime" NOTNULL="false"/>
<FIELD NAME="inactivemailsent" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0"/> <FIELD NAME="inactivemailsent" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0"/>
<FIELD NAME="staff" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0"/> <FIELD NAME="staff" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0"/>
<FIELD NAME="admin" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0"/> <FIELD NAME="admin" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0"/>
......
...@@ -1408,6 +1408,13 @@ function xmldb_core_upgrade($oldversion=0) { ...@@ -1408,6 +1408,13 @@ function xmldb_core_upgrade($oldversion=0) {
add_field($table, $field); add_field($table, $field);
} }
if ($oldversion < 2008092000) {
$table = new XMLDBTable('usr');
$field = new XMLDBField('lastaccess');
$field->setAttributes(XMLDB_TYPE_DATETIME, null, null);
add_field($table, $field);
}
return $status; return $status;
} }
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
defined('INTERNAL') || die(); defined('INTERNAL') || die();
$config = new StdClass; $config = new StdClass;
$config->version = 2008091604; $config->version = 2008092000;
$config->release = '1.1.0alpha2dev'; $config->release = '1.1.0alpha2dev';
$config->minupgradefrom = 2007080700; $config->minupgradefrom = 2007080700;
$config->minupgraderelease = '0.8.0 (release tag 0.8.0_RELEASE)'; $config->minupgraderelease = '0.8.0 (release tag 0.8.0_RELEASE)';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment