X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=script%2Fadmin_users.pl;h=737573dbafb903ba4c67f752fcc9433892d20714;hb=fc5c4949b38572067ab389afa8c1cafec2b5dbd0;hp=59e326ae2b63fd28f5dc9d1d1d6a5a63e8221778;hpb=cf7e4e7bbd96ff1cd20f79032af32e85f968d0c8;p=scpubgit%2Fstemmatology.git diff --git a/script/admin_users.pl b/script/admin_users.pl index 59e326a..737573d 100644 --- a/script/admin_users.pl +++ b/script/admin_users.pl @@ -12,8 +12,8 @@ use lib 'lib'; use Text::Tradition::Directory; -my ($dsn, $command) = ('dbi:SQLite:dbname=db/traditions.db', 'add', undef); -my ($username, $password, $tradition_id); +my ($dsn, $command) = ('dbi:SQLite:dbname=db/traditions.db', 'add'); +my ($username, $password, $tradition_id, $rolename); GetOptions( 'c|command:s' => \$command, @@ -21,9 +21,10 @@ GetOptions( 'u|username=s' => \$username, 'p|password:s' => \$password, 't|tradition:s' => \$tradition_id, + 'r|role:s' => \$rolename, ) or usage(); -if(!$command || !($command ~~ [qw/add modify delete deactivate reactivate/])) { +if(!$command || !($command ~~ [qw/add modify delete deactivate reactivate list/])) { print "No command supplied, chickening out ... \n\n"; usage(); } @@ -39,12 +40,18 @@ my $new_scope = $userstore->new_scope; given ($command) { when ('add') { + ## We only add local users here, OpenID etc users will get auto-added + ## when they login if(!$password || !$userstore->validate_password($password)) { print "Can't add a new user without a valid password\n\n"; usage(); + break; } - my $user = $userstore->add_user({ username => $username, - password => $password }); + ## Set role as passed in rolename, if set (else gets default "user") + my $user = $userstore->add_user({ username => $username, + password => $password, + ( $rolename ? (role => $rolename) : () ), + }); if(!$user) { print "Failed to add user! (you should see errors above this..)\n"; } else { @@ -53,7 +60,7 @@ given ($command) { } when ('modify') { - if(!$tradition_id && !$password) { + if(!$tradition_id && !$password && !$rolename) { print "Can't modify a user without a valid password or a tradition\n\n"; usage(); break; @@ -63,26 +70,56 @@ given ($command) { usage(); break; } - 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 @set_password = ( $password ? ( password => $password ) : () ); + my @set_role = ( $rolename ? ( role => $rolename ) : () ); + + my $user = $userstore->modify_user({ username => $username, + @set_password, + @set_role, + }); + if(!$user) { + print "Failed to modify user! (you should see errors above this..)\n"; + } else { + print "Modified User.\n"; + } + + if($tradition_id) { my $tradition = $userstore->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); + if(grep { $userstore->object_to_id($_) + eq + $userstore->object_to_id($tradition)} + @{$user->traditions}) { + $user->remove_tradition($tradition); + } else { + $user->add_tradition($tradition); + } $userstore->update($tradition); $userstore->update($user); - print "OK.\n"; + print "Added Tradition.\n"; } - } + } + + print "OK\n"; + } + + when ('list') { + my $user = $userstore->find_user({ username => $username }); + if(!$user) { + print "Can't find user '$username'\n"; + break; + } + my $traditions = $user->traditions; + + print "User: $username\n"; + print "Has traditions: \n"; + foreach my $t (@$traditions) { + print " ", $t->name, "\n"; + } + print "OK.\n"; } when ('deactivate') { @@ -91,7 +128,7 @@ given ($command) { print "Failed to deactivate user! (you should see errors above this..)\n"; } else { print "OK.\n"; - } + } } when ('reactivate') { @@ -100,7 +137,7 @@ given ($command) { print "Failed to reactivate user! (you should see errors above this..)\n"; } else { print "OK.\n"; - } + } } when ('delete') { @@ -124,6 +161,7 @@ sub usage { 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 modify -u jimbob -r \"admin\"\n"; print "Usage: $0 -c deactivate -u jimbob\n"; } @@ -135,10 +173,16 @@ admin_users.pl - add / modify / etc users admin_user.pl -c add -u jimbob -p "jimspassword" + admin_user.pl -c add -u jimbob -p "jimspassword" -r "admin" + admin_user.pl -c modify -u jimbob -p "jimsnewpassword" + admin_user.pl -c modify -u jimbob -r "admin" + admin_user.pl -c modify -u jimbob -t "mytradition" + admin_user.pl -c list -u jimbob + admin_user.pl -c delete -u jimbob =head1 OPTIONS @@ -147,7 +191,37 @@ admin_users.pl - add / modify / etc users =item -c | --command -The action to take, can be one of: add, modify, deactivate, reactivate, delete. +The action to take, can be one of: add, modify, deactivate, reactivate, delete, list. + +=over + +=item add + +Create a new user and store it in the Directory + +=item modify + +Change an existing stored user, with a -p this will change the user's +password, with a -t will add or remove the named tradition from the +user. + +=item list + +List the given user's traditions. + +=item deactivate + +Deactivate this user. + +=item reactivate + +Re-activate this user. + +=item delete + +Delete the user permanently. + +=back =item -u | --username @@ -161,4 +235,8 @@ The new password or password to change. A Text::Tradition id or name which will be assigned to the user given. +=item -r | --role + +A rolename to add or modify, this is a plain text string to check it carefully. Use C to change if necessary. + =back