Commit dbaebc65 authored by Ghada El-Zoghbi's avatar Ghada El-Zoghbi Committed by Aaron Wells

Bug 1606101: usr.suspendedcusr must be non-zero

It turns out a lot of existing code checks the boolean
value of usr.suspendedcusr to determine if a user should
be treated as suspended or not. The LDAP sync cron (and,
indeed, any code suspending users via a cron task) was
setting usr.suspendedcusr to 0, which is boolean false,
so these users would be treated as not suspended.

We are going to update all usr.suspendedcusr = 0
to a valid site admin ID.

Change-Id: Iecfbfd8a4cdd98d5d07149bb40c64308262ea234
behatnotneeded: Test to come later
parent 3fd34589
......@@ -823,7 +823,9 @@ $string['phpuploaderror_8'] = 'File upload stopped by extension.';
$string['adminphpuploaderror'] = 'A file upload error was probably caused by your server configuration.';
$string['youraccounthasbeensuspendedtext2'] = 'Your account at %s has been suspended by %s.'; // @todo: more info?
$string['youraccounthasbeensuspendedtextcron'] = 'Your account at %s has been suspended.';
$string['youraccounthasbeensuspendedreasontext'] = "Your account at %s has been suspended by %s. Reason:\n\n%s";
$string['youraccounthasbeensuspendedreasontextcron'] = "Your account at %s has been suspended. Reason:\n\n%s";
$string['youraccounthasbeenunsuspendedtext2'] = 'Your account at %s has been unsuspended. You may once again log in and use the site.'; // can't provide a login link because we don't know how they log in - it might be by xmlrpc
// size of stuff
......
......@@ -4300,5 +4300,12 @@ function xmldb_core_upgrade($oldversion=0) {
change_field_precision($table, $field);
}
if ($oldversion < 2015092926) {
log_debug('Updating usr.suspendedcusr from 0 to a valid site admin ID for users suspended via a cron task.');
$admins = get_site_admins();
$suspendinguserid = $admins[0]->id;
set_field('usr', 'suspendedcusr', $suspendinguserid, 'suspendedcusr', 0);
}
return $status;
}
......@@ -1394,6 +1394,15 @@ function suspend_user($suspendeduserid, $reason, $suspendinguserid=null) {
$suspendinguserid = $USER->get('id');
}
$iscron = false;
if ($suspendinguserid == 0) {
// root user has ID = 0 -> happens when run in cron.
// Use a valid site admin ID.
$iscron = true;
$admins = get_site_admins();
$suspendinguserid = $admins[0]->id;
}
$suspendrec = new StdClass;
$suspendrec->id = $suspendeduserid;
$suspendrec->suspendedcusr = $suspendinguserid;
......@@ -1410,12 +1419,26 @@ function suspend_user($suspendeduserid, $reason, $suspendinguserid=null) {
$message->users = array($suspendeduserid);
$message->subject = get_string_from_language($lang, 'youraccounthasbeensuspended');
if ($reason == '') {
$message->message = get_string_from_language($lang, 'youraccounthasbeensuspendedtext2', 'mahara',
get_config('sitename'), display_name($suspendinguserid, $suspendeduserid));
if ($iscron) {
// Suspended by a cron task
$message->message = get_string_from_language($lang, 'youraccounthasbeensuspendedtextcron', 'mahara',
get_config('sitename'));
}
else {
$message->message = get_string_from_language($lang, 'youraccounthasbeensuspendedtext2', 'mahara',
get_config('sitename'), display_name($suspendinguserid, $suspendeduserid));
}
}
else {
$message->message = get_string_from_language($lang, 'youraccounthasbeensuspendedreasontext', 'mahara',
get_config('sitename'), display_name($suspendinguserid, $suspendeduserid), $reason);
if ($iscron) {
// Suspended by a cron task
$message->message = get_string_from_language($lang, 'youraccounthasbeensuspendedreasontextcron', 'mahara',
get_config('sitename'), $reason);
}
else {
$message->message = get_string_from_language($lang, 'youraccounthasbeensuspendedreasontext', 'mahara',
get_config('sitename'), display_name($suspendinguserid, $suspendeduserid), $reason);
}
}
require_once('activity.php');
activity_occurred('maharamessage', $message);
......@@ -3116,3 +3139,23 @@ function get_user_favorites($userid, $limit=5, $offset=0) {
return $users;
}
/**
* Returns a list of a site admin user IDs order by the oldest ID first.
*
* @returns array of stdclass objects containing site admin ids.
*/
function get_site_admins() {
// get a list of all the admins ordered by oldest ID.
// There will always be at least one site admin in the system.
if ($admins = get_records_sql_array('
SELECT u.id
FROM {usr} u
WHERE u.admin = 1
AND u.active = 1
ORDER BY u.id', array())) {
return $admins;
}
// just in case there is something horribly wrong.
return false;
}
......@@ -16,7 +16,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/wiki/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2015092925;
$config->version = 2015092926;
$config->series = '15.10';
$config->release = '15.10.5testing';
$config->minupgradefrom = 2012080604;
......
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