Skip to content

Commit

Permalink
Added "--delete userId [userId ...]" option to the "user" command
Browse files Browse the repository at this point in the history
Added "--delete userId [userId ...]" option to the "user" command
to delete user(s) by their userId.
  • Loading branch information
pbmarklf authored and haarg committed Jul 16, 2012
1 parent 0d53d61 commit 94018b6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
38 changes: 37 additions & 1 deletion lib/WGDev/Command/User.pm
Expand Up @@ -10,6 +10,7 @@ use WGDev::X ();

sub config_options {
return qw(
delete
findByPassword=s
findByDictionary=s
);
Expand All @@ -28,6 +29,33 @@ sub process {
if ( my $dictionary = $self->option('findByDictionary') ) {
return $self->find_by_dictionary($dictionary);
}

if ( $self->option('delete') ) {
return $self->delete_user();
}
}

# this is named 'delete_user()' because 'delete' is a built-in function in Perl
sub delete_user {
my $self = shift;
my $user = undef;
my $wgd = $self->wgd;
my $session = $wgd->session();

if ( !$self->arguments ) {
WGDev::X->throw("No user to delete!\n");
}

foreach my $userId ( $self->arguments ) {
eval {
$user = new WebGUI::User($session, $userId);
};
if ($@ || !$user->validUserId($session, $userId)) {
WGDev::X::UserNotFound->throw(userId => $userId);
}

$user->delete();
}
}

sub find_by_password {
Expand Down Expand Up @@ -89,7 +117,7 @@ sub find_by_dictionary {

=head1 SYNOPSIS
wgd user [--findByPassword <password>] [--findByDictionary <dictionary>]
wgd user [--delete userId [userId ...]] [--findByPassword <password>] [--findByDictionary <dictionary>]
=head1 DESCRIPTION
Expand All @@ -99,6 +127,10 @@ Utilities for manipulating WebGUI Users
=over 8
=item C<--delete>
Delete the specified user(s) by their userId.
=item C<--findByPassword>
Return a list of users that are using the given password (assumes
Expand All @@ -113,6 +145,10 @@ file in C</usr/share/dict/> or C</var/lib/dict/>
=back
=method delete_user
Deletes the specified user(s), given a list of userIds on the command line.
=method find_by_password
Hashes the given password and sees if any user IDs in the C<authentication> table
Expand Down
14 changes: 14 additions & 0 deletions lib/WGDev/X.pm
Expand Up @@ -66,6 +66,11 @@ use Exception::Class (
description => 'Bad asset class specified',
fields => ['class'],
},
'WGDev::X::UserNotFound' => {
isa => 'WGDev::X',
description => 'Specified user not found',
fields => ['userId'],
},
'WGDev::X::Module' => {
isa => 'WGDev::X',
description => 'Error loading module',
Expand Down Expand Up @@ -232,6 +237,15 @@ sub WGDev::X::AssetNotFound::full_message {
return $message;
}

sub WGDev::X::UserNotFound::full_message {
my $self = shift;
my $message = $self->SUPER::full_message;
if ( $self->userId ) {
$message .= ' FATAL ERROR: User not found - ' . $self->userId;
}
return $message;
}

sub WGDev::X::Module::full_message {
my $self = shift;
my $message = $self->description . q{ } . $self->module
Expand Down

0 comments on commit 94018b6

Please sign in to comment.