Skip to content

Commit 86f6598

Browse files
committed
Fix the change_user_id script for the case that a user does not have a password.
It is now the case that a user is not required to have a password record in the database. However, the `change_user_id` script was never updated for that case. So an exception is thrown if the script is used for a user that does not have a password record (for example a user created via LTI authentication). Note that perltidy has also been run on the file. The script does not have the `.pl` extension and so the workflow doesn't check this file and the `run-perltidy.pl` script doesn't attempt to format it. Hide whitespace changes to see the important change (there is also some clean up of the comments at the beginning of the file). Also fix the check for the type of a database record in the `checkArgs` method of `lib/WeBWorK/DB.pm`. Just because a variable is a `ref` does not mean that it is an object for which the `isa` method can be called. The proper check is `blessed $obj && $obj->isa('Package')`. Note the defined check was unnecessary in any case. This prevented the error in the script from giving more useful information. Instead it complained about the inability to call `isa` when it should have given the intended error message from this check.
1 parent 983c1c6 commit 86f6598

2 files changed

Lines changed: 69 additions & 71 deletions

File tree

bin/change_user_id

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
#!/usr/bin/env perl
2+
3+
# Sometimes a webwork user id changes. This script transfers the webwork data for the old user_id to the new user_id.
24
#
3-
#Sometimes a webwork user id changes. This script transfers the webwork data for the old user_id to the new user_id
4-
# Update database tables
5-
#user
6-
#permission
7-
#password
8-
#key
9-
#set_user
10-
#problem_user
11-
#set_locations_user
12-
#global_achievement_user
13-
#achievement_user
5+
# The script updates the following database database tables
6+
# user
7+
# permission
8+
# password
9+
# key
10+
# set_user
11+
# problem_user
12+
# set_locations_user
13+
# global_achievement_user
14+
# achievement_user
1415
#
15-
# Update answer_log
16+
# and updates the answer_log.
1617

1718
use strict;
1819
use warnings;
@@ -21,7 +22,7 @@ use File::Basename;
2122

2223
BEGIN {
2324
use Mojo::File qw(curfile);
24-
use Env qw(WEBWORK_ROOT);
25+
use Env qw(WEBWORK_ROOT);
2526

2627
$WEBWORK_ROOT = curfile->dirname->dirname;
2728
}
@@ -32,13 +33,13 @@ use WeBWorK::CourseEnvironment;
3233
use WeBWorK::DB;
3334
use Data::Dumper;
3435

35-
if((scalar(@ARGV) != 3)) {
36-
print "\nSyntax is: change_user_id course_id old_user_id new_user_id";
37-
print "\n (e.g. newpassword MAT_123 jjones jsmith\n\n";
38-
exit();
36+
if ((scalar(@ARGV) != 3)) {
37+
print "\nSyntax is: change_user_id course_id old_user_id new_user_id";
38+
print "\n (e.g. newpassword MAT_123 jjones jsmith\n\n";
39+
exit();
3940
}
4041

41-
my $courseID = shift;
42+
my $courseID = shift;
4243
my $old_user_id = shift;
4344
my $new_user_id = shift;
4445

@@ -50,63 +51,63 @@ my $ce = WeBWorK::CourseEnvironment->new({
5051
my $db = WeBWorK::DB->new($ce);
5152
die "Error: $old_user_id does not exist!" unless $db->existsUser($old_user_id);
5253

53-
unless($db->existsUser($new_user_id)) {
54-
my $user = $db->getUser($old_user_id);
55-
$user->{user_id}=$new_user_id;
56-
$user->{comment} = $user->{comment}."Record created from $old_user_id record";
57-
$db->addUser($user);
54+
unless ($db->existsUser($new_user_id)) {
55+
my $user = $db->getUser($old_user_id);
56+
$user->{user_id} = $new_user_id;
57+
$user->{comment} = $user->{comment} . "Record created from $old_user_id record";
58+
$db->addUser($user);
5859
}
5960

60-
unless($db->existsPassword($new_user_id)) {
61-
my $password = $db->getPassword($old_user_id);
62-
$password->{user_id} = $new_user_id;
63-
$db->addPassword($password);
61+
if (!$db->existsPassword($new_user_id) && (my $password = $db->getPassword($old_user_id))) {
62+
$password->{user_id} = $new_user_id;
63+
$db->addPassword($password);
6464
}
6565

66-
unless($db->existsPermissionLevel($new_user_id)) {
67-
my $permission = $db->getPermissionLevel($old_user_id);
68-
$permission->{user_id} = $new_user_id;
69-
$db->addPermissionLevel($permission);
66+
unless ($db->existsPermissionLevel($new_user_id)) {
67+
my $permission = $db->getPermissionLevel($old_user_id);
68+
$permission->{user_id} = $new_user_id;
69+
$db->addPermissionLevel($permission);
7070
}
7171

7272
my @old_user_sets = $db->listUserSets($old_user_id);
73-
foreach(@old_user_sets) {
74-
my $set_id = $_;
75-
my $new_set = $db->newUserSet;
76-
$new_set->user_id($new_user_id);
77-
$new_set->set_id($set_id);
78-
eval{$db->addUserSet($new_set)};
79-
my $old_set = $db->getUserSet($old_user_id,$set_id);
80-
foreach(keys %$old_set) {
81-
next if /user_id|set_id/;
82-
$new_set->$_($old_set->$_);
83-
}
84-
85-
$db->putUserSet($new_set) unless $db->existsUserSet($new_user_id,$set_id);
86-
my @global_problems = grep { defined $_} $db->getAllGlobalProblems($set_id);
87-
foreach(@global_problems) {
88-
if($db->existsUserProblem($old_user_id,$set_id,$_->{problem_id})) {
89-
my $old_user_problem = $db->getUserProblem($old_user_id,$set_id,$_->{problem_id});
90-
my $new_user_problem = $db->newUserProblem;
91-
$new_user_problem->user_id($new_user_id);
92-
$new_user_problem->set_id($set_id);
93-
$new_user_problem->problem_id($_->{problem_id});
94-
$db->addUserProblem($new_user_problem) unless $db->existsUserProblem($new_user_id,$set_id,$_->{problem_id});
95-
foreach(keys %$old_user_problem) {
96-
next if /(user_id|set_id|problem_id)/;
97-
$new_user_problem->$_($old_user_problem->$_);
98-
}
99-
$db->putUserProblem($new_user_problem);
100-
}
101-
}
73+
foreach (@old_user_sets) {
74+
my $set_id = $_;
75+
my $new_set = $db->newUserSet;
76+
$new_set->user_id($new_user_id);
77+
$new_set->set_id($set_id);
78+
eval { $db->addUserSet($new_set) };
79+
my $old_set = $db->getUserSet($old_user_id, $set_id);
80+
foreach (keys %$old_set) {
81+
next if /user_id|set_id/;
82+
$new_set->$_($old_set->$_);
83+
}
84+
85+
$db->putUserSet($new_set) unless $db->existsUserSet($new_user_id, $set_id);
86+
my @global_problems = grep { defined $_ } $db->getAllGlobalProblems($set_id);
87+
foreach (@global_problems) {
88+
if ($db->existsUserProblem($old_user_id, $set_id, $_->{problem_id})) {
89+
my $old_user_problem = $db->getUserProblem($old_user_id, $set_id, $_->{problem_id});
90+
my $new_user_problem = $db->newUserProblem;
91+
$new_user_problem->user_id($new_user_id);
92+
$new_user_problem->set_id($set_id);
93+
$new_user_problem->problem_id($_->{problem_id});
94+
$db->addUserProblem($new_user_problem)
95+
unless $db->existsUserProblem($new_user_id, $set_id, $_->{problem_id});
96+
foreach (keys %$old_user_problem) {
97+
next if /(user_id|set_id|problem_id)/;
98+
$new_user_problem->$_($old_user_problem->$_);
99+
}
100+
$db->putUserProblem($new_user_problem);
101+
}
102+
}
102103
}
103104

104105
my $answer_log = $ce->{courseFiles}->{logs}->{answer_log};
105-
my $dirname = dirname($answer_log);
106-
copy($answer_log,"$dirname/answer_log.bak");
107-
open(my $in,'<',"$dirname/answer_log.bak") or die "Can't open $dirname/answer_log.bak:$!";
108-
open(my $out,'>',$answer_log);
109-
while(<$in>) {
110-
s/$old_user_id/$new_user_id/g;
111-
print $out $_;
106+
my $dirname = dirname($answer_log);
107+
copy($answer_log, "$dirname/answer_log.bak");
108+
open(my $in, '<', "$dirname/answer_log.bak") or die "Can't open $dirname/answer_log.bak:$!";
109+
open(my $out, '>', $answer_log);
110+
while (<$in>) {
111+
s/$old_user_id/$new_user_id/g;
112+
print $out $_;
112113
}

lib/WeBWorK/DB.pm

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,11 +2233,8 @@ sub checkArgs {
22332233

22342234
if (defined $table) {
22352235
my $class = $self->{$table}{record};
2236-
#print "arg=$arg class=$class\n";
22372236
croak "argument $pos must be of type $class"
2238-
unless defined $arg
2239-
and ref $arg
2240-
and $arg->isa($class);
2237+
unless blessed $arg && $arg->isa($class);
22412238
eval { checkKeyfields($arg, $versioned) };
22422239
croak "argument $pos contains $@" if $@;
22432240
} else {

0 commit comments

Comments
 (0)