e32739515673e9c9320c92609b6e36fe0a5199cd
[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 ## using prompt():
10 use ExtUtils::MakeMaker();
11 use lib 'lib';
12
13 use Text::Tradition::Directory;
14
15 my ($dsn, $command) = ('dbi:SQLite:dbname=db/traditions.db', 'add', undef);
16 my ($username, $password, $tradition_id);
17
18 GetOptions(
19     'c|command:s' => \$command,
20     'dsn:s' => \$dsn,
21     'u|username=s' => \$username,
22     'p|password:s' => \$password,
23     't|tradition:s' => \$tradition_id,
24     ) or usage();
25
26 if(!$command || !($command ~~ [qw/add modify delete deactivate reactivate list/])) {
27     print "No command supplied, chickening out ... \n\n";
28     usage();
29 }
30
31 if(!$username) {
32     print "No username supplied, confused ... \n\n";
33     usage();
34 }
35
36 # my $userstore = Text::Tradition::UserStore->new( dsn => $dsn);
37 my $userstore = Text::Tradition::Directory->new( dsn => $dsn);
38 my $new_scope = $userstore->new_scope;
39
40 given ($command) {
41     when ('add') {
42         if(!$password || !$userstore->validate_password($password)) {
43             print "Can't add a new user without a valid password\n\n";
44             usage();
45         }
46         my $user = $userstore->add_user({ username => $username, 
47                                           password => $password });
48         if(!$user) {
49             print "Failed to add user! (you should see errors above this..)\n";
50         } else {
51             print "OK.\n";
52         }
53     }
54
55     when ('modify') {
56         if(!$tradition_id && !$password) {
57             print "Can't modify a user without a valid password or a tradition\n\n";
58             usage();
59             break;
60         }
61         if( $password && !$userstore->validate_password($password)) {
62             print "Can't modify a user without a valid password\n\n";
63             usage();
64             break;
65         }
66         if($password) {
67             my $user = $userstore->modify_user({ username => $username, 
68                                                  password => $password });
69             if(!$user) {
70                 print "Failed to modify user! (you should see errors above this..)\n";
71             } else {
72                 print "OK.\n";
73             }
74         } elsif($tradition_id) {
75             my $tradition = $userstore->tradition($tradition_id);
76             my $user = $userstore->find_user({ username => $username });
77             if(!$tradition || !$user) {
78                 print "Can't find one of '$username' or '$tradition_id' in the database!\n";
79             } else {
80                 $user->add_tradition($tradition);
81                 $userstore->update($tradition);
82                 $userstore->update($user);
83                 print "OK.\n";
84             }
85         } 
86     }
87
88     when ('list') {
89         my $user = $userstore->find_user({ username => $username });
90         if(!$user) {
91             print "Can't find user '$username'\n";
92             break;
93         }
94         my $traditions = $user->traditions;
95
96         print "User: $username\n";
97         print "Has traditions: \n";
98         foreach my $t (@$traditions) {
99             print "    ", $t->name, "\n";
100         }
101         print "OK.\n";
102     }
103
104     when ('deactivate') {
105         my $user = $userstore->deactivate_user({ username => $username});
106         if(!$user) {
107             print "Failed to deactivate user! (you should see errors above this..)\n";
108         } else {
109             print "OK.\n";
110         }        
111     }
112
113     when ('reactivate') {
114         my $user = $userstore->reactivate_user({ username => $username});
115         if(!$user) {
116             print "Failed to reactivate user! (you should see errors above this..)\n";
117         } else {
118             print "OK.\n";
119         }        
120     }
121
122     when ('delete') {
123         my $yesno = ExtUtils::MakeMaker::prompt("Permanently delete $username? (y/N)", "n");
124         if($yesno !~ /^y$/i) {
125             print "Not deleting $username\n";
126             break;
127         }
128         my $user = $userstore->delete_user({ username => $username});
129         if(!$user) {
130             print "Failed to delete user! (you should see errors above this..)\n";
131         } else {
132             print "OK.\n";
133         }        
134     }
135 }
136
137 sub usage {
138     print "User Admin tool, to add/modify/deactivate/reactivate/delete users\n";
139     print "===========================================\n";
140     print "Usage: $0 -c add -u jimbob -p hispassword\n";
141     print "Usage: $0 -c modify -u jimbob -p hisnewpassword\n";
142     print "Usage: $0 -c modify -u jimbob -t \"Notre besoin\"\n";
143     print "Usage: $0 -c deactivate -u jimbob\n";
144 }
145
146 =head1 NAME
147
148 admin_users.pl - add / modify / etc users
149
150 =head1 SYNOPSIS
151
152     admin_user.pl -c add -u jimbob -p "jimspassword"
153
154     admin_user.pl -c modify -u jimbob -p "jimsnewpassword"
155
156     admin_user.pl -c modify -u jimbob -t "mytradition"
157
158     admin_user.pl -c list -u jimbob
159
160     admin_user.pl -c delete -u jimbob
161
162 =head1 OPTIONS
163
164 =over
165
166 =item -c | --command
167
168 The action to take, can be one of: add, modify, deactivate, reactivate, delete, list.
169
170 =over
171
172 =item add
173
174 Create a new user and store it in the Directory
175
176 =item modify
177
178 Change an existing stored user, with a -p this will change the user's
179 password, with a -t will add or remove the named tradition from the
180 user.
181
182 =item list
183
184 List the given user's traditions.
185
186 =item deactivate
187
188 Deactivate this user.
189
190 =item reactivate
191
192 Re-activate this user.
193
194 =item delete
195
196 Delete the user permanently.
197
198 =back
199
200 =item -u | --username
201
202 The username of the new user or user to change.
203
204 =item -p | --password
205
206 The new password or password to change.
207
208 =item -t | --tradition
209
210 A Text::Tradition id or name which will be assigned to the user given. 
211
212 =back