Commit 944db49e authored by Lisa Seeto's avatar Lisa Seeto Committed by Robert Lyon
Browse files

Bug 1271618: Create users by CSV - allow specification of end date



Add expiry field to csv upload for account end date.

behatnotneeded

Change-Id: I08c7388f53120377a1d2f1fcdf6bf0b315f02189
Signed-off-by: default avatarLisa Seeto <lisaseeto@catalyst.net.nz>
parent 7a64e38a
......@@ -25,6 +25,8 @@ $specialcases = array('username', 'password', 'remoteuser');
// Don't upload social profiles for now. A user can have multiple profiles. Not sure how to put that in a csv.
$notallowed = array('socialprofile');
$ALLOWEDKEYS = array_keys(ArtefactTypeProfile::get_all_fields());
// Allow 'expiry' option
array_push($ALLOWEDKEYS, 'expiry');
$ALLOWEDKEYS = array_diff($ALLOWEDKEYS, $notallowed);
$maildisabled = array_search('maildisabled', $ALLOWEDKEYS);
unset($ALLOWEDKEYS[$maildisabled]);
......@@ -344,6 +346,20 @@ function uploadcsv_validate(Pieform $form, $values) {
$usernames[strtolower($username)]['remoteuser'] = $remoteuser;
}
}
if (array_key_exists('expiry', $formatkeylookup) && !empty($line[$formatkeylookup['expiry']])) {
$expirydate = $line[$formatkeylookup['expiry']];
try {
$date = new DateTime($expirydate);
}
catch (Exception $e) {
$csverrors->add($i, get_string('uploadcsverrorinvalidexpirydate', 'admin', $i, $expirydate));
$date = false;
}
$now = new DateTime("now");
if (!empty($date) && $date < $now) {
$csverrors->add($i, get_string('uploadcsverrorexpirydateinpast', 'admin', $i, $expirydate));
}
}
}
// If the admin is trying to overwrite existing users, identified by username,
......@@ -542,6 +558,19 @@ function uploadcsv_submit(Pieform $form, $values) {
}
continue;
}
if ($field == 'expiry' && !empty($record[$formatkeylookup[$field]])) {
$expirydate = $record[$formatkeylookup[$field]];
try {
$date = new DateTime($expirydate);
}
catch (Exception $e) {
$date = false;
}
if ($date) {
$profilefields->{$field} = $date->format('Y-m-d');
}
continue;
}
$profilefields->{$field} = $record[$formatkeylookup[$field]];
}
......@@ -634,6 +663,10 @@ foreach ($ALLOWEDKEYS as $type) {
if ($type == 'firstname' || $type == 'lastname' || $type == 'email' || $type == 'username' || $type == 'password') {
continue;
}
if ($type == 'expiry') {
$fields .= '<li>expiry' . get_help_icon('core', 'user', 'edituser_upload', $type) . '</li>';
continue;
}
$fields .= '<li>' . hsc($type) . "</li>\n";
}
$fields .= "<div class=cl></div></ul>\n";
......
......@@ -668,6 +668,8 @@ $string['uploadcsverrorduplicateremoteuser'] = 'Line %s of the file specifies a
$string['uploadcsverrorremoteusertaken'] = 'Line %s of the file specifies a remote username "%s" that is already taken by the user "%s".';
$string['uploadcsverrorusernotininstitution'] = 'Error on line %s: The user "%s" is not a member of the institution %s.';
$string['uploadcsverroruserinaninstitution'] = 'Error on line %s: The user "%s" is a member of the following institutions: %s. You cannot update this user\'s authentication method to "No Institution".';
$string['uploadcsverrorinvalidexpirydate'] = 'Error on line %s: The expiry "%s" is invalid. Please use a valid date format.';
$string['uploadcsverrorexpirydateinpast'] = 'Error on line %s: The expiry "%s" cannot be in the past.';
$string['uploadcsvpagedescription6'] = '<p>Here you can upload new users via a <acronym title="Comma Separated Values">CSV</acronym> file.</p>
<p>The first row of your CSV file should specify the format of your CSV data. For example, it should look like this:</p>
......
<!-- @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later -->
<!-- @copyright For copyright information on Mahara, please see the README file distributed with this software. -->
<h3>Expiry date</h3>
<p>This will set the date on which the user's login is automatically disabled.</p>
<p>The date needs to be in a valid date format, for example:<p>
<ul>
<li>2019-03-25</li>
<li>25 Mar 2019</li>
<li>+3 months</li>
</ul>
<p>This date will override any account expiry date previously set in site account settings.</p>
......@@ -2479,7 +2479,11 @@ function create_user($user, $profile=array(), $institution=null, $remoteauth=nul
}
$user->quota = $quota;
}
if (get_config('defaultaccountlifetime')) {
if (isset($profile->expiry)) {
//set the expiry date from the csv upload
$user->expiry = $profile->expiry;
}
else 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'));
}
......@@ -2496,7 +2500,7 @@ function create_user($user, $profile=array(), $institution=null, $remoteauth=nul
set_profile_field($user->id, 'lastname', $user->lastname, TRUE);
}
foreach ($profile as $k => $v) {
if (in_array($k, array('firstname', 'lastname', 'email'))) {
if (in_array($k, array('firstname', 'lastname', 'email', 'expiry'))) {
continue;
}
set_profile_field($user->id, $k, $v, TRUE);
......@@ -2595,6 +2599,33 @@ function update_user($user, $profile, $remotename=null, $accountprefs=array(), $
}
}
foreach (get_object_vars($profile) as $k => $v) {
if ($k == 'expiry') {
if (!$oldrecord->expiry) {
//adding an expiry for first time
$newrecord->expiry = $v;
$updated[$k] = $v;
}
else {
$unexpire = $oldrecord->expiry && strtotime($oldrecord->expiry) < time() && (empty($v) || strtotime($v) > time());
$newexpiry = db_format_timestamp($v);
if ($oldrecord->expiry != $newexpiry) {
$newrecord->expiry = $newexpiry;
$updated[$k] = $v;
if ($unexpire) {
$newrecord->expirymailsent = 0;
$newrecord->lastaccess = db_format_timestamp(time());
}
}
}
continue;
}
if (get_profile_field($userid, $k) != $v) {
set_profile_field($userid, $k, $v);
$updated[$k] = $v;
}
}
if (count(get_object_vars($newrecord))) {
$newrecord->id = $userid;
update_record('usr', $newrecord);
......@@ -2604,13 +2635,6 @@ function update_user($user, $profile, $remotename=null, $accountprefs=array(), $
}
}
foreach (get_object_vars($profile) as $k => $v) {
if (get_profile_field($userid, $k) != $v) {
set_profile_field($userid, $k, $v);
$updated[$k] = $v;
}
}
if ($remotename) {
$oldremote = get_field('auth_remote_user', 'remoteusername', 'authinstance', $oldrecord->authinstance, 'localusr', $userid);
if ($remotename != $oldremote) {
......
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