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 | |
570cf8ba |
15 | my ($dsn, $command) = ('dbi:SQLite:dbname=db/traditions.db', 'add', undef); |
f54b1ba7 |
16 | my ($username, $password, $tradition_id); |
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, |
570cf8ba |
24 | ) or usage(); |
25 | |
df8c12f0 |
26 | if(!$command || !($command ~~ [qw/add modify delete deactivate reactivate list/])) { |
570cf8ba |
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 | |
cf7e4e7b |
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; |
570cf8ba |
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') { |
cf7e4e7b |
56 | if(!$tradition_id && !$password) { |
f54b1ba7 |
57 | print "Can't modify a user without a valid password or a tradition\n\n"; |
570cf8ba |
58 | usage(); |
cf7e4e7b |
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; |
570cf8ba |
65 | } |
f54b1ba7 |
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) { |
cf7e4e7b |
75 | my $tradition = $userstore->tradition($tradition_id); |
f54b1ba7 |
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 { |
ec7ea4e6 |
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 | } |
cf7e4e7b |
88 | $userstore->update($tradition); |
f54b1ba7 |
89 | $userstore->update($user); |
90 | print "OK.\n"; |
91 | } |
92 | } |
570cf8ba |
93 | } |
94 | |
df8c12f0 |
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 | |
570cf8ba |
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"; |
f54b1ba7 |
149 | print "Usage: $0 -c modify -u jimbob -t \"Notre besoin\"\n"; |
570cf8ba |
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 | |
cf7e4e7b |
163 | admin_user.pl -c modify -u jimbob -t "mytradition" |
164 | |
df8c12f0 |
165 | admin_user.pl -c list -u jimbob |
166 | |
570cf8ba |
167 | admin_user.pl -c delete -u jimbob |
168 | |
169 | =head1 OPTIONS |
170 | |
171 | =over |
172 | |
173 | =item -c | --command |
174 | |
df8c12f0 |
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 |
570cf8ba |
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 | |
cf7e4e7b |
215 | =item -t | --tradition |
216 | |
217 | A Text::Tradition id or name which will be assigned to the user given. |
218 | |
570cf8ba |
219 | =back |