Remove unused files
[scpubgit/stemmatology.git] / script / admin_users.pl
CommitLineData
570cf8ba 1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5
6use v5.10.0;
7
8use Getopt::Long;
9## using prompt():
10use ExtUtils::MakeMaker();
f54b1ba7 11use lib 'lib';
12
f54b1ba7 13use Text::Tradition::Directory;
570cf8ba 14
570cf8ba 15my ($dsn, $command) = ('dbi:SQLite:dbname=db/traditions.db', 'add', undef);
f54b1ba7 16my ($username, $password, $tradition_id);
570cf8ba 17
18GetOptions(
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
26if(!$command || !($command ~~ [qw/add modify delete deactivate reactivate/])) {
27 print "No command supplied, chickening out ... \n\n";
28 usage();
29}
30
31if(!$username) {
32 print "No username supplied, confused ... \n\n";
33 usage();
34}
35
cf7e4e7b 36# my $userstore = Text::Tradition::UserStore->new( dsn => $dsn);
37my $userstore = Text::Tradition::Directory->new( dsn => $dsn);
38my $new_scope = $userstore->new_scope;
570cf8ba 39
40given ($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 {
80 $user->add_tradition($tradition);
cf7e4e7b 81 $userstore->update($tradition);
f54b1ba7 82 $userstore->update($user);
83 print "OK.\n";
84 }
85 }
570cf8ba 86 }
87
88 when ('deactivate') {
89 my $user = $userstore->deactivate_user({ username => $username});
90 if(!$user) {
91 print "Failed to deactivate user! (you should see errors above this..)\n";
92 } else {
93 print "OK.\n";
94 }
95 }
96
97 when ('reactivate') {
98 my $user = $userstore->reactivate_user({ username => $username});
99 if(!$user) {
100 print "Failed to reactivate user! (you should see errors above this..)\n";
101 } else {
102 print "OK.\n";
103 }
104 }
105
106 when ('delete') {
107 my $yesno = ExtUtils::MakeMaker::prompt("Permanently delete $username? (y/N)", "n");
108 if($yesno !~ /^y$/i) {
109 print "Not deleting $username\n";
110 break;
111 }
112 my $user = $userstore->delete_user({ username => $username});
113 if(!$user) {
114 print "Failed to delete user! (you should see errors above this..)\n";
115 } else {
116 print "OK.\n";
117 }
118 }
119}
120
121sub usage {
122 print "User Admin tool, to add/modify/deactivate/reactivate/delete users\n";
123 print "===========================================\n";
124 print "Usage: $0 -c add -u jimbob -p hispassword\n";
125 print "Usage: $0 -c modify -u jimbob -p hisnewpassword\n";
f54b1ba7 126 print "Usage: $0 -c modify -u jimbob -t \"Notre besoin\"\n";
570cf8ba 127 print "Usage: $0 -c deactivate -u jimbob\n";
128}
129
130=head1 NAME
131
132admin_users.pl - add / modify / etc users
133
134=head1 SYNOPSIS
135
136 admin_user.pl -c add -u jimbob -p "jimspassword"
137
138 admin_user.pl -c modify -u jimbob -p "jimsnewpassword"
139
cf7e4e7b 140 admin_user.pl -c modify -u jimbob -t "mytradition"
141
570cf8ba 142 admin_user.pl -c delete -u jimbob
143
144=head1 OPTIONS
145
146=over
147
148=item -c | --command
149
150The action to take, can be one of: add, modify, deactivate, reactivate, delete.
151
152=item -u | --username
153
154The username of the new user or user to change.
155
156=item -p | --password
157
158The new password or password to change.
159
cf7e4e7b 160=item -t | --tradition
161
162A Text::Tradition id or name which will be assigned to the user given.
163
570cf8ba 164=back