Show traditions based on the current logged-in user
[scpubgit/stemmatology.git] / script / admin_users,pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use v5.10.0;
7
8 use Getopt::Long;
9 use Text::Tradition::UserStore;
10
11 use lib 'lib';
12
13 my ($dsn, $command) = ('dbi:SQLite:dbname=db/traditions.db', 'add', undef);
14 my ($username, $password);
15
16 GetOptions(
17     'c|command:s' => \$command,
18     'dsn:s' => \$dsn,
19     'u|username=s' => \$username,
20     'p|password:s' => \$password,
21     ) or usage();
22
23 if(!$command || !($command ~~ [qw/add modify delete/])) {
24     print "No command supplied, chickening out ... \n\n";
25     usage();
26 }
27
28 if(!$username) {
29     print "No username supplied, confused ... \n\n";
30     usage();
31 }
32
33 my $userstore = Text::Tradition::UserStore->new( dsn => $dsn);
34
35 given ($command) {
36     when ('add') {
37         if(!$password || !$userstore->validate_password($password)) {
38             print "Can't add a new user without a valid password\n\n";
39             usage();
40         }
41         my $user = $userstore->add_user({ username => $username, 
42                                           password => $password });
43         if(!$user) {
44             print "Failed to add user! (you should see errors above this..)\n";
45         } else {
46             print "OK.\n";
47         }
48     }
49
50     when ('modify') {
51         if(!$password || !$userstore->validate_password($password)) {
52             print "Can't modify a user without a valid password\n\n";
53             usage();
54         }
55         my $user = $userstore->modify_user({ username => $username, 
56                                              password => $password });
57         if(!$user) {
58             print "Failed to modify user! (you should see errors above this..)\n";
59         } else {
60             print "OK.\n";
61         }        
62     }
63     when ('delete') {
64         my $user = $userstore->delete_user({ username => $username});
65         if(!$user) {
66             print "Failed to modify user! (you should see errors above this..)\n";
67         } else {
68             print "OK.\n";
69         }        
70     }
71 }
72
73 sub usage {
74     print "User Admin tool, to add/remove/modify users\n";
75     print "===========================================\n";
76     print "Usage: $0 -c add -u jimbob -p hispassword\n";
77     print "Usage: $0 -c modify -u jimbob -p hisnewpassword\n";
78     print "Usage: $0 -c delete -u jimbob\n";
79 }
80
81 =head1 NAME
82
83 admin_users.pl - add / modify / delete users
84
85 =head1 SYNOPSIS
86
87     admin_user.pl -c add -u jimbob -p "jimspassword"
88
89     admin_user.pl -c modify -u jimbob -p "jimsnewpassword"
90
91     admin_user.pl -c delete -u jimbob
92
93 =head1 OPTIONS
94
95 =over
96
97 =item -c | --command
98
99 The action to take, can be one of: add, modify, delete.
100
101 =item -u | --username
102
103 The username of the new user or user to change.
104
105 =item -p | --password
106
107 The new password or password to change.
108
109 =back