Commit 6c24a36a authored by Son Nguyen's avatar Son Nguyen Committed by Gerrit Code Review

Merge "Fix for automatic account expiry (Bug #1097565)"

parents a3691eb1 4919c4e1
......@@ -341,6 +341,19 @@ $siteoptionform = array(
'help' => true,
'disabled' => in_array('defaultaccountlifetime', $OVERRIDDEN),
),
'defaultaccountlifetimeupdate' => array(
'type' => 'radio',
'title' => get_string('defaultaccountlifetimeupdate', 'admin'),
'description' => get_string('defaultaccountlifetimeupdatedescription', 'admin'),
'defaultvalue' => get_config('defaultaccountlifetimeupdate'),
'separator' => '<br>',
'options' => array(
'none' => get_string('defaultaccountlifetimeupdatenone', 'admin'),
'some' => get_string('defaultaccountlifetimeupdatesome', 'admin'),
'all' => get_string('defaultaccountlifetimeupdateall', 'admin')
),
'help' => true,
),
'defaultaccountinactiveexpire' => array(
'type' => 'expiry',
'title' => get_string('defaultaccountinactiveexpire', 'admin'),
......@@ -679,7 +692,7 @@ function siteoptions_submit(Pieform $form, $values) {
$fields = array(
'sitename','lang','theme', 'dropdownmenu',
'defaultaccountlifetime', 'defaultregistrationexpirylifetime', 'defaultaccountinactiveexpire', 'defaultaccountinactivewarn',
'allowpublicviews', 'allowpublicprofiles', 'generatesitemap',
'defaultaccountlifetimeupdate', 'allowpublicviews', 'allowpublicprofiles', 'generatesitemap',
'registration_sendweeklyupdates', 'institutionexpirynotification', 'institutionautosuspend',
'showselfsearchsideblock', 'searchusernames', 'searchplugin', 'showtagssideblock',
'tagssideblockmaxtags', 'country', 'viewmicroheaders', 'userscanchooseviewthemes',
......@@ -691,7 +704,42 @@ function siteoptions_submit(Pieform $form, $values) {
'masqueradingreasonrequired', 'masqueradingnotified',
'eventloglevel', 'eventlogexpiry',
);
$count = 0;
$where_sql = " WHERE admin = 0 AND id != 0";
// if default account lifetime expiry has no end date
if (empty($values['defaultaccountlifetime'])) {
if ($values['defaultaccountlifetimeupdate'] == 'all') {
// need to remove user expiry
db_begin();
$count = count_records_sql("SELECT COUNT(*) FROM {usr} $where_sql");
execute_sql("UPDATE {usr} SET expiry = NULL $where_sql");
db_commit();
}
else {
// make the 'some' option the same as 'none' as it is meaningless to
// update existing users without expiry date to having 'no end date'
$values['defaultaccountlifetimeupdate'] = 'none';
}
}
else {
// fetch all the users that are not siteadmins
$user_expiry = mktime(0, 0, 0, date('n'), date('j'), date('Y')) + (int)$values['defaultaccountlifetime'];
if ($values['defaultaccountlifetimeupdate'] == 'some') {
// and the user's expiry is not set
$where_sql .= " AND expiry IS NULL";
$count = count_records_sql("SELECT COUNT(*) FROM {usr} $where_sql");
db_begin();
execute_sql("UPDATE {usr} SET expiry = ? $where_sql", array(format_date($user_expiry)));
db_commit();
}
else if ($values['defaultaccountlifetimeupdate'] == 'all') {
// and the user's expiry is set
db_begin();
$count = count_records_sql("SELECT COUNT(*) FROM {usr} $where_sql");
execute_sql("UPDATE {usr} SET expiry = ? $where_sql", array(format_date($user_expiry)));
db_commit();
}
}
// if public views are disabled, sitemap generation must also be disabled.
if ($values['allowpublicviews'] == false) {
$values['generatesitemap'] = false;
......@@ -740,11 +788,54 @@ function siteoptions_submit(Pieform $form, $values) {
$message .= ' ' . get_string('usersseenewthemeonlogin', 'admin');
$USER->reset_institutions();
}
if ($count) {
$message .= ' ' . get_string('numberusersupdated','admin', $count);
}
$form->reply(PIEFORM_OK, array('message' => $message, 'goto' => '/admin/site/options.php'));
}
$js = <<<EOF
jQuery(document).ready(function() {
var j = jQuery.noConflict();
var overrideuseraccountlifetime = j('#siteoptions input[name=defaultaccountlifetimeupdate]');
var defaultaccountlifetime = j('#siteoptions_defaultaccountlifetime_units');
function overrideuseraccountlife(v) {
// if default account lifetime is set to 'noenddate' disable the override options
if (v == 'noenddate') {
var value = j('#siteoptions input[name=defaultaccountlifetimeupdate]:checked').val();
j('#siteoptions input[name=defaultaccountlifetimeupdate][value=some]').attr('disabled',true);
j('#siteoptions input[name=defaultaccountlifetimeupdate][value=some]').attr('checked', false);
if (value != 'some') {
j('#siteoptions input[name=defaultaccountlifetimeupdate][value=' + value + ']').attr('checked', true);
}
}
else {
// enable the radio buttons
overrideuseraccountlifetime.attr('disabled',false);
if (!j('#siteoptions input[name=defaultaccountlifetimeupdate]:checked').val()) {
overrideuseraccountlifetime.each(function() {
if (typeof j(this).attr('checked') === 'undefined' || j(this).attr('checked') === false) {
// if no radio button already selected then choose the 'only for new users' option by default
if (j(this).val() == 'none') {
j(this).attr('checked',true);
}
}
});
}
}
}
// when default account lifetime changes rerun the override account lifetime checks
defaultaccountlifetime.change(function() {
overrideuseraccountlife(defaultaccountlifetime.val());
});
// initial setup
overrideuseraccountlife(defaultaccountlifetime.val());
});
EOF;
$thispage = json_encode(get_config('wwwroot') . 'admin/site/options.php');
$smarty = smarty(array('adminsiteoptions'));
$smarty->assign('siteoptionform', $siteoptionform);
$smarty->assign('PAGEHEADING', TITLE);
$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->display('admin/site/options.tpl');
......@@ -23,7 +23,9 @@
*/
// Add here as appropriate
var forceReloadElements = ['sitename', 'lang', 'theme'];
var forceReloadElements = ['sitename', 'lang', 'theme',
'defaultaccountlifetime_units',
'defaultaccountlifetimeupdate'];
var isReloadRequired = false;
// Disconnects the pieform submit handler and changes the form target back to
......
......@@ -312,7 +312,12 @@ $string['defaultaccountinactivewarndescription'] = 'The time before user account
$string['defaultregistrationexpirylifetime'] = 'Default registration expiry lifetime';
$string['defaultregistrationexpirylifetimedescription'] = 'How long registration applications that require an administrator action will stay active.';
$string['defaultaccountlifetime'] = 'Default account lifetime';
$string['defaultaccountlifetimedescription'] = 'If set, user accounts will expire after this period of time from when they have been created.';
$string['defaultaccountlifetimedescription'] = 'If set, user accounts will expire after this period of time from either today or the day they are created in the future.';
$string['defaultaccountlifetimeupdate'] = 'Override user account lifetime';
$string['defaultaccountlifetimeupdatedescription'] = 'If default account lifetime is set, then choose how to deal with user expiry times.';
$string['defaultaccountlifetimeupdatenone'] = 'Only for newly created users';
$string['defaultaccountlifetimeupdatesome'] = 'For new users and users without an account lifetime already set (excluding site administrators)';
$string['defaultaccountlifetimeupdateall'] = 'For all user accounts (excluding site administrators)';
$string['embeddedcontent'] = 'Embedded content';
$string['embeddedcontentdescription'] = 'If you would like users to be able to embed videos or other outside content into their portfolios, you can choose which sites to trust below.';
$string['Everyone'] = 'Everyone';
......
......@@ -2142,6 +2142,10 @@ function create_user($user, $profile=array(), $institution=null, $remoteauth=nul
if (empty($user->quota)) {
$user->quota = get_config_plugin('artefact', 'file', 'defaultquota');
}
if (get_config('defaultaccountlifetime')) {
// we need to set the user expiry to the site default one
$user->expiry = date('Y-m-d',mktime(0, 0, 0, date('m'), date('d'), date('Y')) + (int)get_config('defaultaccountlifetime'));
}
$user->id = insert_record('usr', $user, 'id', true);
}
// Bypass access check for 'copynewuser' institution/site views, because this user may not be logged in yet
......
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