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