Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 378137d

Browse files
committedJul 13, 2012
Added "--delete userId [userId ...]" option to the "user" command
Added "--delete userId [userId ...]" option to the "user" command to delete user(s) by their userId.
1 parent a7c470d commit 378137d

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
 

‎lib/WGDev/Command/User.pm

+37-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use WGDev::X ();
1010

1111
sub config_options {
1212
return qw(
13+
delete
1314
findByPassword=s
1415
findByDictionary=s
1516
);
@@ -28,6 +29,33 @@ sub process {
2829
if ( my $dictionary = $self->option('findByDictionary') ) {
2930
return $self->find_by_dictionary($dictionary);
3031
}
32+
33+
if ( $self->option('delete') ) {
34+
return $self->delete_user();
35+
}
36+
}
37+
38+
# this is named 'delete_user()' because 'delete' is a built-in function in Perl
39+
sub delete_user {
40+
my $self = shift;
41+
my $user = undef;
42+
my $wgd = $self->wgd;
43+
my $session = $wgd->session();
44+
45+
if ( !$self->arguments ) {
46+
WGDev::X->throw("No user to delete!\n");
47+
}
48+
49+
foreach my $userId ( $self->arguments ) {
50+
eval {
51+
$user = new WebGUI::User($session, $userId);
52+
};
53+
if ($@ || !$user->validUserId($session, $userId)) {
54+
WGDev::X::UserNotFound->throw(userId => $userId);
55+
}
56+
57+
$user->delete();
58+
}
3159
}
3260

3361
sub find_by_password {
@@ -89,7 +117,7 @@ sub find_by_dictionary {
89117

90118
=head1 SYNOPSIS
91119
92-
wgd user [--findByPassword <password>] [--findByDictionary <dictionary>]
120+
wgd user [--delete userId [userId ...]] [--findByPassword <password>] [--findByDictionary <dictionary>]
93121
94122
=head1 DESCRIPTION
95123
@@ -99,6 +127,10 @@ Utilities for manipulating WebGUI Users
99127
100128
=over 8
101129
130+
=item C<--delete>
131+
132+
Delete the specified user(s) by their userId.
133+
102134
=item C<--findByPassword>
103135
104136
Return a list of users that are using the given password (assumes
@@ -113,6 +145,10 @@ file in C</usr/share/dict/> or C</var/lib/dict/>
113145
114146
=back
115147
148+
=method delete_user
149+
150+
Deletes the specified user(s), given a list of userIds on the command line.
151+
116152
=method find_by_password
117153
118154
Hashes the given password and sees if any user IDs in the C<authentication> table

‎lib/WGDev/X.pm

+14
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ use Exception::Class (
6666
description => 'Bad asset class specified',
6767
fields => ['class'],
6868
},
69+
'WGDev::X::UserNotFound' => {
70+
isa => 'WGDev::X',
71+
description => 'Specified user not found',
72+
fields => ['userId'],
73+
},
6974
'WGDev::X::Module' => {
7075
isa => 'WGDev::X',
7176
description => 'Error loading module',
@@ -232,6 +237,15 @@ sub WGDev::X::AssetNotFound::full_message {
232237
return $message;
233238
}
234239

240+
sub WGDev::X::UserNotFound::full_message {
241+
my $self = shift;
242+
my $message = $self->SUPER::full_message;
243+
if ( $self->userId ) {
244+
$message .= ' FATAL ERROR: User not found - ' . $self->userId;
245+
}
246+
return $message;
247+
}
248+
235249
sub WGDev::X::Module::full_message {
236250
my $self = shift;
237251
my $message = $self->description . q{ } . $self->module

0 commit comments

Comments
 (0)
Please sign in to comment.