Permalink
Browse files

add new /change_password to change the pw of another user

  • Loading branch information...
szabgab committed Mar 11, 2012
1 parent fda05a8 commit 32c5ec2e77fc40e0066f30f024753b6af3b92c53
Showing with 62 additions and 13 deletions.
  1. +2 −0 Changes
  2. +9 −4 lib/Dwimmer/Admin.pm
  3. +20 −7 script/dwimmer
  4. +31 −2 t/003_client.t
View
@@ -1,6 +1,8 @@
Changes to the Dwimmer system. See http://dwimmer.org/
- rename /change_password to /change_my_password
+ - add new /change_password to change the password
+ of another user
- fix command line adding user
- add command line listing all the users
View
@@ -682,10 +682,10 @@ post '/set_site_config.json' => sub {
post '/change_password.json' => sub {
- my %params = _clean_params(qw(uid new_password admin_password));
- return render_response 'error', { 'no_uid' => 1 } if not $params{uid};
+ my %params = _clean_params(qw(uid name password admin_password));
+ return render_response 'error', { 'no_uid_or_name' => 1 } if not $params{uid} and not $params{name};
return render_response 'error', { 'no_new_password' => 1 } if not $params{new_password};
- return render_response 'error', { 'no_old_password' => 1 } if not $params{admin_password};
+ return render_response 'error', { 'no_admin_password' => 1 } if not $params{admin_password};
my $new_sha1 = sha1_base64( $params{new_password} );
my $admin_sha1 = sha1_base64( $params{admin_password} );
@@ -696,7 +696,12 @@ post '/change_password.json' => sub {
return render_response 'error', { 'invalid_admin_password' => 1 }
if $admin->sha1 ne $admin_sha1;
- my $user = $db->resultset('User')->find( { id => $params{uid} } );
+ my $user;
+ if ($params{uid}) {
+ $user = $db->resultset('User')->find( { id => $params{uid} } );
+ } else {
+ $user = $db->resultset('User')->find( { name => $params{name} } );
+ }
return render_response 'error', { 'no_user_found' => 1 } if not $user;
View
@@ -19,8 +19,9 @@ pod2usage() if not $opt{url} or not $opt{username} or not $opt{password};
$opt{url} =~ s{/+$}{};
my %commands = (
- add_user => [qw(uname email password)],
- list_users => [],
+ add_user => [qw(uname email password)],
+ list_users => [],
+ change_password => [qw(name password)],
);
use Dwimmer::Client;
@@ -40,11 +41,6 @@ if ($commands{$cmd}) {
}
@data{ @fields } = @ARGV;
- if ($cmd eq 'add_user') {
- $data{verify} = 'verified';
- $data{pw1} = $data{pw2} = delete $data{password};
- }
-
my $dw = Dwimmer::Client->new( host => $opt{url} );
my $login = $dw->login( username => $opt{username}, password => $opt{password} );
if ($login->{error}) {
@@ -56,8 +52,18 @@ if ($commands{$cmd}) {
}
}
#print Dumper $login;
+
+ if ($cmd eq 'add_user') {
+ $data{verify} = 'verified';
+ $data{pw1} = $data{pw2} = delete $data{password};
+ } elsif ($cmd eq 'change_password') {
+ $data{new_password} = delete $data{password};
+ $data{admin_password} = $opt{password};
+ }
+
my $r = $dw->$cmd(%data);
+
if ($cmd eq 'list_users' ) {
print "Users: \n\n";
foreach my $u ( sort { $a->{name} cmp $b->{name} } @{ $r->{users} } ) {
@@ -77,6 +83,12 @@ if ($commands{$cmd}) {
} else {
die "No success and not error? " . Dumper $r;
}
+ } elsif ($cmd eq 'change_password') {
+ if ($r->{success}) {
+ print "Success. Password changed\n";
+ } else {
+ die Dumper $r;
+ }
} else {
print Dumper $r;
}
@@ -105,6 +117,7 @@ Commands:
add_user USERNAME EMAIL PASSWORD
list_users
+ change_password USERNAME PASSWORD
For example if you would like to add a user called 'foo' with a password 'secret'
View
@@ -19,7 +19,7 @@ plan( skip_all => 'Unsupported OS' ) if not $run;
my $url = "http://localhost:$ENV{DWIMMER_PORT}";
my $URL = "$url/";
-plan( tests => 59 );
+plan( tests => 63 );
my @pages = (
{},
@@ -437,7 +437,7 @@ is_deeply(
'user logged in with new password'
);
-
+diag('Check if admin can change the password of another user based on uid');
my $pw3 = 'dgjkl';
is_deeply(
$admin->change_password( new_password => $pw3, admin_password => $users[0]{password}, uid => 2 ),
@@ -467,6 +467,35 @@ is_deeply(
);
+diag('Check if admin can change the password of another user based on name');
+my $pw4 = 'ladbhlash';
+is_deeply(
+ $admin->change_password( new_password => $pw4, admin_password => $users[0]{password}, name => $users[0]{uname} ),
+ { success => 1 }, 'password changed'
+);
+
+is_deeply( $user->logout, { success => 1 }, 'logout' );
+cmp_deeply(
+ $user->session,
+ { logged_in => 0,
+ data => ignore(),
+ site => ignore(),
+
+ # dwimmer_version => $Dwimmer::Client::VERSION,
+ },
+ 'session'
+);
+
+is_deeply(
+ $user->login( username => $users[0]{uname}, password => $pw4 ),
+ { success => 1,
+ username => $users[0]{uname},
+ userid => 2,
+ logged_in => 1,
+ },
+ 'user logged in with new password'
+);
+

0 comments on commit 32c5ec2

Please sign in to comment.