From: Jess Robinson Date: Wed, 16 May 2012 13:01:23 +0000 (+0000) Subject: Initial code for adding traditions to users X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f54b1ba72a1195818f413fc316cdd5e4a2b1d9b5;p=scpubgit%2Fstemmatology.git Initial code for adding traditions to users --- diff --git a/lib/Text/Tradition.pm b/lib/Text/Tradition.pm index 62b50f4..ef50df6 100644 --- a/lib/Text/Tradition.pm +++ b/lib/Text/Tradition.pm @@ -53,6 +53,13 @@ has 'stemmata' => ( }, default => sub { [] }, ); + +has 'user' => ( + is => 'rw', + isa => 'Text::Tradition::User', + required => 0, + predicate => 'has_user', + ); # Create the witness before trying to add it around 'add_witness' => sub { diff --git a/lib/Text/Tradition/User.pm b/lib/Text/Tradition/User.pm index 6c21a7d..c78b8e2 100644 --- a/lib/Text/Tradition/User.pm +++ b/lib/Text/Tradition/User.pm @@ -12,12 +12,20 @@ has 'password' => (is => 'rw', required => 1); has 'active' => (is => 'rw', default => sub { 1; }); # 'traits' => ['Array'] ? # https://metacpan.org/module/Moose::Meta::Attribute::Native::Trait::Array -has 'traditions' => (is => 'rw', isa => 'ArrayRef[Text::Tradition]', default => sub { [] }, required => 0); - -# after add_tradition => sub { -# $tradition->set_user($self) -# unless $tradition->user->id eq $self->id; -# } +has 'traditions' => (is => 'rw', + traits => ['Array'], + handles => { + 'add_tradition' => 'push', + }, + isa => 'ArrayRef[Text::Tradition]', + default => sub { [] }, + required => 0); + +after add_tradition => sub { + my ($self, $tradition) = @_; + $tradition->user($self) + unless $tradition->has_user && $tradition->user->id eq $self->id; +}; 1; diff --git a/script/admin_users.pl b/script/admin_users.pl index 35aaebe..f407917 100644 --- a/script/admin_users.pl +++ b/script/admin_users.pl @@ -8,18 +8,21 @@ use v5.10.0; use Getopt::Long; ## using prompt(): use ExtUtils::MakeMaker(); +use lib 'lib'; + use Text::Tradition::UserStore; +use Text::Tradition::Directory; -use lib 'lib'; my ($dsn, $command) = ('dbi:SQLite:dbname=db/traditions.db', 'add', undef); -my ($username, $password); +my ($username, $password, $tradition_id); GetOptions( 'c|command:s' => \$command, 'dsn:s' => \$dsn, 'u|username=s' => \$username, 'p|password:s' => \$password, + 't|tradition:s' => \$tradition_id, ) or usage(); if(!$command || !($command ~~ [qw/add modify delete deactivate reactivate/])) { @@ -50,17 +53,34 @@ given ($command) { } when ('modify') { - if(!$password || !$userstore->validate_password($password)) { - print "Can't modify a user without a valid password\n\n"; + if(!$tradition_id || (!$password || !$userstore->validate_password($password))) { + print "Can't modify a user without a valid password or a tradition\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"; - } + if($password) { + 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"; + } + } elsif($tradition_id) { + my $directory = Text::Tradition::Directory->new( + dsn => $dsn, + ); + my $new_scope = $directory->new_scope; + my $tradition = $directory->tradition($tradition_id); + my $user = $userstore->find_user({ username => $username }); + if(!$tradition || !$user) { + print "Can't find one of '$username' or '$tradition_id' in the database!\n"; + } else { + $user->add_tradition($tradition); + $directory->update($tradition); + $userstore->update($user); + print "OK.\n"; + } + } } when ('deactivate') { @@ -101,6 +121,7 @@ sub usage { 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 modify -u jimbob -t \"Notre besoin\"\n"; print "Usage: $0 -c deactivate -u jimbob\n"; }