From: Jess Robinson Date: Wed, 9 May 2012 14:07:35 +0000 (+0000) Subject: Initial admin_users script, with more tests and additions to userstore for modifying... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ef02228c9ea504ab0d38cb0ace2103dfab2eca40;p=scpubgit%2Fstemmatology.git Initial admin_users script, with more tests and additions to userstore for modifying users --- diff --git a/lib/Text/Tradition/UserStore.pm b/lib/Text/Tradition/UserStore.pm index 4475986..dbcba48 100644 --- a/lib/Text/Tradition/UserStore.pm +++ b/lib/Text/Tradition/UserStore.pm @@ -10,14 +10,21 @@ extends 'KiokuX::Model'; use Text::Tradition::User; +has MIN_PASS_LEN => ( is => 'ro', isa => 'Num', default => sub { 8 } ); + # has 'directory' => ( # is => 'rw', # isa => 'KiokuX::Model', # handles => [] # ); +## To die or not to die, on error, this is the question. sub add_user { - my ($self, $username, $password) = @_; + my ($self, $userinfo) = @_; + my $username = $userinfo->{username}; + my $password = $userinfo->{password}; + + return unless $username && $self->validate_password($password); my $user = Text::Tradition::User->new( id => $username, @@ -38,4 +45,35 @@ sub find_user { } +sub modify_user { + my ($self, $userinfo) = @_; + my $username = $userinfo->{username}; + my $password = $userinfo->{password}; + + return unless $username && $self->validate_password($password); + + my $user = $self->find_user({ username => $username }); + return unless $user; + + my $scope = $self->new_scope; + $user->password(crypt_password($password)); + + $self->update($user); + + return $user; +} + +sub delete_user { +} + + +sub validate_password { + my ($self, $password) = @_; + + return if !$password; + return if length($password) < $self->MIN_PASS_LEN; + + return 1; +} + 1; diff --git a/script/admin_users,pl b/script/admin_users,pl new file mode 100644 index 0000000..7abedea --- /dev/null +++ b/script/admin_users,pl @@ -0,0 +1,109 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use v5.10.0; + +use Getopt::Long; +use Text::Tradition::UserStore; + +use lib 'lib'; + +my ($dsn, $command) = ('dbi:SQLite:dbname=db/traditions.db', 'add', undef); +my ($username, $password); + +GetOptions( + 'c|command:s' => \$command, + 'dsn:s' => \$dsn, + 'u|username=s' => \$username, + 'p|password:s' => \$password, + ) or usage(); + +if(!$command || !($command ~~ [qw/add modify delete/])) { + print "No command supplied, chickening out ... \n\n"; + usage(); +} + +if(!$username) { + print "No username supplied, confused ... \n\n"; + usage(); +} + +my $userstore = Text::Tradition::UserStore->new( dsn => $dsn); + +given ($command) { + when ('add') { + if(!$password || !$userstore->validate_password($password)) { + print "Can't add a new user without a valid password\n\n"; + usage(); + } + my $user = $userstore->add_user({ username => $username, + password => $password }); + if(!$user) { + print "Failed to add user! (you should see errors above this..)\n"; + } else { + print "OK.\n"; + } + } + + when ('modify') { + if(!$password || !$userstore->validate_password($password)) { + print "Can't modify a user without a valid password\n\n"; + usage(); + } + my $user = $userstore->modify_user({ username => $username, + password => $password }); + if(!$user) { + print "Failed to modify user! (you should see errors above this..)\n"; + } else { + print "OK.\n"; + } + } + when ('delete') { + my $user = $userstore->delete_user({ username => $username}); + if(!$user) { + print "Failed to modify user! (you should see errors above this..)\n"; + } else { + print "OK.\n"; + } + } +} + +sub usage { + print "User Admin tool, to add/remove/modify users\n"; + print "===========================================\n"; + print "Usage: $0 -c add -u jimbob -p hispassword\n"; + print "Usage: $0 -c modify -u jimbob -p hisnewpassword\n"; + print "Usage: $0 -c delete -u jimbob\n"; +} + +=head1 NAME + +admin_users.pl - add / modify / delete users + +=head1 SYNOPSIS + + admin_user.pl -c add -u jimbob -p "jimspassword" + + admin_user.pl -c modify -u jimbob -p "jimsnewpassword" + + admin_user.pl -c delete -u jimbob + +=head1 OPTIONS + +=over + +=item -c | --command + +The action to take, can be one of: add, modify, delete. + +=item -u | --username + +The username of the new user or user to change. + +=item -p | --password + +The new password or password to change. + +=back diff --git a/t/text_tradition_user.t b/t/text_tradition_user.t index fc06fe0..4a01463 100644 --- a/t/text_tradition_user.t +++ b/t/text_tradition_user.t @@ -13,18 +13,29 @@ my $fh = File::Temp->new(); my $file = $fh->filename; $fh->close; my $dsn = "dbi:SQLite:dbname=$file"; -# my $d = KiokuX::Model->new( 'dsn' => $dsn,'extra_args' => { 'create' => 1 } ); -my $user_store = Text::Tradition::UserStore->new('dsn' => $dsn,'extra_args' => { 'create' => 1 } ); +my $user_store = Text::Tradition::UserStore->new('dsn' => $dsn, + 'extra_args' => { 'create' => 1 } ); + +## passwords +my $shortpass = 'bloggs'; +ok(!$user_store->validate_password($shortpass), '"bloggs" is too short for a password'); ## create user -my $new_user = $user_store->add_user('fred', 'bloggs'); +my $new_user = $user_store->add_user({ username => 'fred', + password => 'bloggspass'}); isa_ok($new_user, 'Text::Tradition::User'); ## find user my $find_user = $user_store->find_user({ username => 'fred'}); isa_ok($find_user, 'Text::Tradition::User'); -ok($find_user->check_password('bloggs'), 'Stored & retrieved with correct password'); - +ok($find_user->check_password('bloggspass'), 'Stored & retrieved with correct password'); + +## modify user +my $changed_user = $user_store->modify_user({ username => 'fred', + password => 'passbloggs' }); +isa_ok($changed_user, 'Text::Tradition::User'); +my $changed = $user_store->find_user({ username => 'fred'}); +ok($changed->check_password('passbloggs'), 'Modified & retrieved with correct new password');