Commit 8d255c29 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Move account expiry from institution to site options; add theme, membership...

Move account expiry from institution to site options; add theme, membership period to institution settings
parent a84e8a36
......@@ -125,6 +125,27 @@ $siteoptionform = pieform(array(
'defaultvalue' => get_config('artefactviewinactivitytime'),
'help' => true,
),
'defaultaccountlifetime' => array(
'type' => 'expiry',
'title' => get_string('defaultaccountlifetime', 'admin'),
'description' => get_string('defaultaccountlifetimedescription', 'admin'),
'defaultvalue' => get_config('defaultaccountlifetime'),
'help' => true,
),
'defaultaccountinactiveexpire' => array(
'type' => 'expiry',
'title' => get_string('defaultaccountinactiveexpire', 'admin'),
'description' => get_string('defaultaccountinactiveexpiredescription', 'admin'),
'defaultvalue' => get_config('defaultaccountinactiveexpire'),
'help' => true,
),
'defaultaccountinactivewarn' => array(
'type' => 'expiry',
'title' => get_string('defaultaccountinactivewarn', 'admin'),
'description' => get_string('defaultaccountinactivewarndescription', 'admin'),
'defaultvalue' => get_config('defaultaccountinactivewarn'),
'help' => true,
),
'submit' => array(
'type' => 'submit',
'value' => get_string('updatesiteoptions','admin')
......@@ -138,6 +159,7 @@ function siteoptions_fail(Pieform $form, $field) {
function siteoptions_submit(Pieform $form, $values) {
$fields = array('sitename','lang','theme','pathtofile', 'pathtoclam',
'defaultaccountlifetime', 'defaultaccountinactiveexpire', 'defaultaccountinactivewarn',
'allowpublicviews','artefactviewinactivitytime', 'searchplugin');
foreach ($fields as $field) {
if (!set_config($field, $values[$field])) {
......
......@@ -126,14 +126,14 @@ if ($institution || $add) {
$data = new StdClass;
$data->displayname = '';
$data->registerallowed = 1;
$data->defaultaccountlifetime = null;
$data->defaultaccountinactiveexpire = null;
$data->defaultaccountinactivewarn = 604800; // 1 week
$data->theme = 'default';
$data->defaultmembershipperiod = null;
$lockedprofilefields = array();
$smarty->assign('add', true);
$authtypes = auth_get_available_auth_types();
}
$themeoptions = get_themes();
safe_require('artefact', 'internal');
$elements = array(
......@@ -192,26 +192,21 @@ if ($institution || $add) {
'defaultvalue' => $data->registerallowed,
'help' => true,
),
'defaultaccountlifetime' => array(
'defaultmembershipperiod' => array(
'type' => 'expiry',
'title' => get_string('defaultaccountlifetime', 'admin'),
'description' => get_string('defaultaccountlifetimedescription', 'admin'),
'defaultvalue' => $data->defaultaccountlifetime,
'title' => get_string('defaultmembershipperiod', 'admin'),
'description' => get_string('defaultmembershipperioddescription', 'admin'),
'defaultvalue' => $data->defaultmembershipperiod,
'help' => true,
),
'defaultaccountinactiveexpire' => array(
'type' => 'expiry',
'title' => get_string('defaultaccountinactiveexpire', 'admin'),
'description' => get_string('defaultaccountinactiveexpiredescription', 'admin'),
'defaultvalue' => $data->defaultaccountinactiveexpire,
'help' => true,
),
'defaultaccountinactivewarn' => array(
'type' => 'expiry',
'title' => get_string('defaultaccountinactivewarn', 'admin'),
'description' => get_string('defaultaccountinactivewarndescription', 'admin'),
'defaultvalue' => $data->defaultaccountinactivewarn,
'help' => true,
'theme' => array(
'type' => 'select',
'title' => get_string('theme','admin'),
'description' => get_string('sitethemedescription','admin'),
'defaultvalue' => $data->theme,
'collapseifoneoption' => true,
'options' => $themeoptions,
'help' => true,
),
'lockedfields' => array(
'value' => '<tr><th colspan="2">Locked fields '
......@@ -245,9 +240,9 @@ if ($institution || $add) {
}
else {
// Get a list of institutions
$institutions = get_records_sql_array('SELECT i.name, i.displayname, i.registerallowed, COUNT(u.id) AS hasmembers
$institutions = get_records_sql_array('SELECT i.name, i.displayname, i.registerallowed, COUNT(u.usr) AS hasmembers
FROM {institution} i
LEFT OUTER JOIN {usr} u ON (u.institution = i.name)
LEFT OUTER JOIN {usr_institution} u ON (u.institution = i.name)
GROUP BY 1, 2, 3
ORDER BY i.name', array());
$smarty->assign('institutions', $institutions);
......@@ -266,9 +261,8 @@ function institution_submit(Pieform $form, $values) {
$newinstitution->displayname = $values['displayname'];
$newinstitution->authplugin = $values['authplugin'];
$newinstitution->registerallowed = ($values['registerallowed']) ? 1 : 0;
$newinstitution->defaultaccountlifetime = ($values['defaultaccountlifetime']) ? intval($values['defaultaccountlifetime']) : null;
$newinstitution->defaultaccountinactiveexpire = ($values['defaultaccountinactiveexpire']) ? intval($values['defaultaccountinactiveexpire']) : null;
$newinstitution->defaultaccountinactivewarn = ($values['defaultaccountinactivewarn']) ? intval($values['defaultaccountinactivewarn']) : null;
$newinstitution->theme = $values['theme'];
$newinstitution->defaultmembershipperiod = ($values['defaultmembershipperiod']) ? intval($values['defaultmembershipperiod']) : null;
$allinstances = array_merge($values['authplugin']['instancearray'], $values['authplugin']['deletearray']);
......
......@@ -977,8 +977,7 @@ function login_submit(Pieform $form, $values) {
}
// Check if the user's account has become inactive
// To become a config setting:
$inactivetime = get_field('institution', 'defaultaccountinactiveexpire', 'name', 'mahara');
$inactivetime = get_config('defaultaccountinactiveexpire');
if ($inactivetime && $oldlastlogin > 0
&& $oldlastlogin + $inactivetime < time()) {
die_info(get_string('accountinactive'));
......@@ -1019,16 +1018,17 @@ function auth_handle_account_expiries() {
// The 'expiry' flag on the usr table
$sitename = get_config('sitename');
$wwwroot = get_config('wwwroot');
$expire = get_config('defaultaccountinactiveexpire');
$warn = get_config('defaultaccountinactivewarn');
// Expiry warning messages
if ($users = get_records_sql_array('SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, i.defaultaccountinactivewarn AS timeout
FROM {usr} u, {institution} i
WHERE u.institution = i.name
AND ? - ' . db_format_tsfield('u.expiry', false) . ' < i.defaultaccountinactivewarn
if ($users = get_records_sql_array('SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email
FROM {usr} u
AND ? - ' . db_format_tsfield('u.expiry', false) . ' < ' . $warn . '
AND expirymailsent = 0', array(time()))) {
foreach ($users as $user) {
$displayname = display_name($user);
$daystoexpire = ceil($user->timeout / 86400) . ' ';
$daystoexpire = ceil($warn / 86400) . ' ';
$daystoexpire .= ($daystoexpire == 1) ? get_string('day') : get_string('days');
email_user($user, null,
get_string('accountexpirywarning'),
......@@ -1049,15 +1049,15 @@ function auth_handle_account_expiries() {
}
}
// Inactivity (lastlogin is too old)
if ($users = get_records_sql_array('SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, i.defaultaccountinactivewarn AS timeout
FROM {usr} u, {institution} i
WHERE u.institution = i.name
AND (? - ' . db_format_tsfield('u.lastlogin', false) . ') > (i.defaultaccountinactiveexpire - i.defaultaccountinactivewarn)
if ($users = get_records_sql_array('SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email
FROM {usr} u
AND (? - ' . db_format_tsfield('u.lastlogin', false) . ') > ' . ($expire - $warn) . '
AND inactivemailsent = 0', array(time()))) {
foreach ($users as $user) {
$displayname = display_name($user);
$daystoinactive = ceil($user->timeout / 86400) . ' ';
$daystoinactive = ceil($warn / 86400) . ' ';
$daystoinactive .= ($daystoexpire == 1) ? get_string('day') : get_string('days');
email_user($user, null, get_string('accountinactivewarning'),
get_string('accountinactivewarningtext', 'mahara', $displayname, $sitename, $daystoinactive, $sitename),
......@@ -1070,8 +1070,7 @@ function auth_handle_account_expiries() {
// Actual inactive users
if ($users = get_records_sql_array('SELECT u.id
FROM {usr} u
LEFT JOIN {institution} i ON (u.institution = i.name)
WHERE ' . db_format_tsfield('lastlogin', false) . ' < ? - i.defaultaccountinactiveexpire', array(time()))) {
WHERE ' . db_format_tsfield('lastlogin', false) . ' < ? - ' . $expire, array(time()))) {
// Users have become inactive!
foreach ($users as $user) {
deactivate_user($user->id);
......@@ -1210,8 +1209,7 @@ class PluginAuth extends Plugin {
// ensure we have everything we need
$user = get_user($user['id']);
// To become a config setting:
$inactivetime = get_field('institution', 'defaultaccountinactiveexpire', 'name', 'mahara');
$inactivetime = get_config('defaultaccountinactiveexpire');
if ($user->suspendedcusr) {
$active = false;
}
......
......@@ -273,6 +273,8 @@ $string['institutiondisplayname'] = 'Institution display name';
$string['institutionupdatedsuccessfully'] = 'Institution updated successfully';
$string['registrationallowed'] = 'Registration allowed?';
$string['registrationalloweddescription'] = 'Whether users can register for the system with this institution';
$string['defaultmembershipperiod'] = 'Default membership period';
$string['defaultmembershipperioddescription'] = 'How long new members remain associated with the institution';
// general stuff
......
......@@ -28,9 +28,8 @@
<FIELD NAME="displayname" TYPE="char" LENGTH="255" NOTNULL="true"/>
<FIELD NAME="registerallowed" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1"/>
<FIELD NAME="updateuserinfoonlogin" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="0"/>
<FIELD NAME="defaultaccountlifetime" TYPE="int" NOTNULL="false"/>
<FIELD NAME="defaultaccountinactiveexpire" TYPE="int" NOTNULL="false"/>
<FIELD NAME="defaultaccountinactivewarn" TYPE="int" NOTNULL="false" DEFAULT="604800"/>
<FIELD NAME="theme" TYPE="char" LENGTH="255" NOTNULL="false"/>
<FIELD NAME="defaultmembershipperiod" TYPE="int" NOTNULL="true" DEFAULT="0"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="name" />
......
......@@ -450,10 +450,7 @@ function xmldb_core_upgrade($oldversion=0) {
execute_sql('DROP INDEX {usr_useaut_uix};');
execute_sql('CREATE UNIQUE INDEX {usr_use_uix} ON {usr} (username);');
$table = new XMLDBField('usr');
$field = new XMLDBField('institution');
drop_field($table, $field);
execute_sql('ALTER TABLE {usr} DROP COLUMN institution;');
if (!empty($renamed)) {
// Notify changed usernames to administrator
......@@ -487,6 +484,34 @@ function xmldb_core_upgrade($oldversion=0) {
}
}
if ($oldversion < 2007112600) {
// Move site-wide stuff from institution table to config table
$default = get_record('institution', 'name', 'mahara');
$c = new StdClass;
$c->field = 'defaultaccountinactivewarn';
$c->value = empty($default->defaultaccountinactivewarn) ? 604800 : $default->defaultaccountinactivewarn;
insert_record('config', $c);
if (!empty($default->defaultaccountlifetime)) {
$c->field = 'defaultaccountlifetime';
$c->value = $default->defaultaccountlifetime;
insert_record('config', $c);
}
if (!empty($default->defaultaccountinactiveexpire)) {
$c->field = 'defaultaccountinactiveexpire';
$c->value = $default->defaultaccountinactiveexpire;
insert_record('config', $c);
}
execute_sql('ALTER TABLE {institution} DROP COLUMN defaultaccountlifetime;');
execute_sql('ALTER TABLE {institution} DROP COLUMN defaultaccountinactiveexpire;');
execute_sql('ALTER TABLE {institution} DROP COLUMN defaultaccountinactivewarn;');
// Add theme, default institution membership period to institution table
execute_sql('ALTER TABLE {institution} ADD COLUMN theme varchar(255)');
execute_sql('ALTER TABLE {institution} ADD COLUMN defaultmembershipperiod bigint');
}
return $status;
}
......
......@@ -42,9 +42,8 @@ class Institution {
'displayname' => '',
'registerallowed' => 1,
'updateuserinfoonlogin' => 0,
'defaultaccountlifetime' => null,
'defaultaccountinactiveexpire' => null,
'defaultaccountinactivewarn' => 0
'theme' => 'default',
'defaultmembershipperiod' => 0
);
function __construct($name = null) {
......@@ -89,17 +88,13 @@ class Institution {
if (!is_numeric($value) || $value < 0 || $value > 1) {
throw new ParamOutOfRangeException("'updateuserinfoonlogin' should be zero or one");
}
} elseif ($name == 'defaultaccountlifetime') {
if (!empty($value) && (!is_numeric($value) || $value < 0 || $value > 9999999999)) {
throw new ParamOutOfRangeException("'defaultaccountlifetime' should be a number between 1 and 9,999,999,999");
}
} elseif ($name == 'defaultaccountinactiveexpire') {
if (!empty($value) && (!is_string($value) || empty($value) || strlen($value) > 255)) {
throw new ParamOutOfRangeException("'defaultaccountinactiveexpire' should be a number between 1 and 9,999,999,999");
} elseif ($name == 'theme') {
if (!is_string($value) || empty($value) || strlen($value) > 255) {
throw new ParamOutOfRangeException("'theme' ($value) should be a string between 1 and 255 characters in length");
}
} elseif ($name == 'defaultaccountinactivewarn') {
if (!empty($value) && strlen($value) > 255) {
throw new ParamOutOfRangeException("'defaultaccountinactivewarn' should be a number between 1 and 9,999,999,999");
} elseif ($name == 'defaultmembershipperiod') {
if (!empty($value) && (!is_numeric($value) || $value < 0 || $value > 9999999999)) {
throw new ParamOutOfRangeException("'defaultmembershipperiod' should be a number between 1 and 9,999,999,999");
}
}
$this->members[$name] = $value;
......@@ -185,9 +180,8 @@ class Institution {
$record->name = $this->name;
$record->displayname = $this->displayname;
$record->updateuserinfoonlogin = $this->updateuserinfoonlogin;
$record->defaultaccountlifetime = $this->defaultaccountlifetime;
$record->defaultaccountinactiveexpire = $this->defaultaccountinactiveexpire;
$record->defaultaccountinactivewarn = $this->defaultaccountinactivewarn;
$record->theme = $this->theme;
$record->defaultmembershipperiod = $this->defaultmembershipperiod;
if ($this->initialized == self::INITIALIZED) {
return insert_record('institution', $record);
......@@ -215,9 +209,8 @@ class Institution {
$this->displayname = $result->displayname;
$this->registerallowed = $result->registerallowed;
$this->updateuserinfoonlogin = $result->updateuserinfoonlogin;
$this->defaultaccountlifetime = $result->defaultaccountlifetime;
$this->defaultaccountinactiveexpire = $result->defaultaccountinactiveexpire;
$this->defaultaccountinactivewarn = $result->defaultaccountinactivewarn;
$this->theme = $result->theme;
$this->defaultmembershipperiod = $result->defaultmembershipperiod;
$this->verifyReady();
}
......
......@@ -27,7 +27,7 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2007112300;
$config->version = 2007112600;
$config->release = '0.9.0alpha3dev';
$config->minupgradefrom = 2007080700;
$config->minupgraderelease = '0.8.0 (release tag 0.8.0_RELEASE)';
......
......@@ -71,7 +71,7 @@ if (isset($key)) {
$registrationid = $registration->id;
unset($registration->id);
unset($registration->expiry);
if ($expirytime = get_field('institution', 'defaultaccountlifetime', 'name', $registration->institution)) {
if ($expirytime = get_config('defaultaccountlifetime')) {
$registration->expiry = db_format_timestamp(time() + $expirytime);
}
$registration->lastlogin = db_format_timestamp(time());
......
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