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,
}
+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;
--- /dev/null
+#!/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
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');