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