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