| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
user-simple-admin - Example script to administer a User::Simple database |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
What you always have to specify: |
| 10 |
- How to get to the information: a DSN (--dsn), the table name (--tbl), |
| 11 |
and probably the database user (--dbuser) and password (--dbpass) |
| 12 |
- An action to perform: --new_user, --mod_user, --remove_user |
| 13 |
- The attributes needed for your query (--login, --name, --level, --password) |
| 14 |
- Optionally, to get a listing of the users in the database, use --dump_users |
| 15 |
|
| 16 |
Table creation: |
| 17 |
user-simple-admin --dsn 'dbi:Pg:dbname=database;host=some.host.org' |
| 18 |
--table user_simple --dbuser useradmin --dbpass s3kr3t --create_plain |
| 19 |
user-simple-admin --dsn 'dbi:Pg:dbname=database;host=some.host.org' |
| 20 |
--table user_simple --dbuser useradmin --dbpass s3kr3t --create_rdbms |
| 21 |
|
| 22 |
User creation example: |
| 23 |
user-simple-admin --dsn 'dbi:Pg:dbname=database;host=some.host.org' |
| 24 |
--table user_simple --dbuser useradmin --dbpass s3kr3t --new_user |
| 25 |
--login joe --name 'Joe Schmoe' --passwd mys3kr17 --level 5 |
| 26 |
|
| 27 |
User remotion: |
| 28 |
user-simple-admin --dsn 'dbi:Pg:dbname=database;host=some.host.org' |
| 29 |
--table user_simple--dbuser useradmin --dbpass s3kr3t --remove_user |
| 30 |
--login john |
| 31 |
|
| 32 |
User modification: |
| 33 |
user-simple-admin --dsn 'dbi:Pg:dbname=database;host=some.host.org' |
| 34 |
--table user_simple --dbuser useradmin --dbpass s3kr3t --new_user |
| 35 |
--login joe --name 'Joe A. Schmoe' --passwd dont_forget_it --level 2 |
| 36 |
|
| 37 |
=head1 DESCRIPTION |
| 38 |
|
| 39 |
This script gives you access from the command line to the main |
| 40 |
User::Simple::Admin functionality. Please note that it has been written more as |
| 41 |
an example than as a script you will use in your day to day administration - |
| 42 |
But if it suits you, well... :) |
| 43 |
|
| 44 |
The script is made to be run non-interactively, taking all of its input via the |
| 45 |
command line (using the standard L<Getopts::Long> syntax), and signalling |
| 46 |
either success or failure as the exit code (as always, 0 means success, and any |
| 47 |
other thing means failure). |
| 48 |
|
| 49 |
About the L<Getopts::Long> syntax, in a nutshell: You can specify the options |
| 50 |
to the script in any order, as long as you keep the switches next to their |
| 51 |
values (that is, C<--tbl tablename --dump_users> is the same as C<--dump_users |
| 52 |
--tbl tablename>, but it is not the same as C<--tbl --dump_users tablename>. |
| 53 |
You can use the shortest possible not ambiguous string for each of the switches |
| 54 |
(i.e. C<--remove_user> is equivalent to C<--r>). |
| 55 |
|
| 56 |
First of all, you have to get the script to get to the users' data - In |
| 57 |
order to do so, you have to specify a DSN (a DBI Data Source Name - A bit |
| 58 |
obscure, yes, I should replace it to something more intuitive as soon as I have |
| 59 |
some time to do so) and the name of the table where the information is located. |
| 60 |
This should be done with the C<--dsn> and C<--tbl> options. Probably you will |
| 61 |
also specify a database user name and password - do so using the C<--dbuser> |
| 62 |
and C<--dbpass> options. |
| 63 |
|
| 64 |
=head2 Table creation |
| 65 |
|
| 66 |
If you specify C<--create_plain> or C<--create_rdbms>, the User::Admin::Simple |
| 67 |
object will be instantiated by creating (instead of just accessing) the |
| 68 |
specified table. Beware, as attempting to create an already existing table will |
| 69 |
not work! |
| 70 |
|
| 71 |
C<--create_rdbms> should be used whenever possible (this is, whenever you are |
| 72 |
using a real RDBMS behind User::Simple, in contraposition to a simple |
| 73 |
file-based structure such as DBD::XBase, DBD::CSV or similar ones). If you are |
| 74 |
using a file-based structure, use C<--create_plain> instead. For further |
| 75 |
details, refer to the L<User::Simple::Admin> manual. |
| 76 |
|
| 77 |
=head2 User creation |
| 78 |
|
| 79 |
To create a new user, specify C<--new_user> and give the desired user |
| 80 |
information - The only required option is C<--login>, as it is the unique |
| 81 |
descriptor for this user - The other available switches are C<--name>, |
| 82 |
C<--passwd> and C<--level>. Again, refer to L<User::Simple::Admin> for further |
| 83 |
details. |
| 84 |
|
| 85 |
If you do not specify a password upon user creation, the account will be |
| 86 |
created but access to it will be disabled until a password is set. |
| 87 |
|
| 88 |
=head2 User remotion |
| 89 |
|
| 90 |
Use C<--remove_user>. You have to specify the C<--login> you will be removing - |
| 91 |
Don't specify any other data; the script will refuse to work if you specify |
| 92 |
any of the other data options. |
| 93 |
|
| 94 |
=head2 User modification |
| 95 |
|
| 96 |
Use C<--mod_user>. You need to specify the C<--login> you are referring to. |
| 97 |
You cannot change a user's login, it is an immutable field. You can specify |
| 98 |
one or more of the C<--name>, C<--passwd> and C<--level> data options. |
| 99 |
|
| 100 |
=head2 Querying |
| 101 |
|
| 102 |
If you specify C<--dump_users>, a user listing will be returned to you. Please |
| 103 |
note that you can ask for C<--dump_users> when performing other opertions (i.e. |
| 104 |
creation, remotion) - It will be run as the last operation, however, if the |
| 105 |
operation fails, the users list will not be dumped. |
| 106 |
|
| 107 |
=head2 Other switches |
| 108 |
|
| 109 |
C<--quiet> supresses all output (including errors produced by this script, the |
| 110 |
errors generated by User::Simple::Admin, DBI or whatever else will still be |
| 111 |
sent) - Only the users dump will be printed. |
| 112 |
|
| 113 |
C<--verbose> will give status debug information you will very seldom require. |
| 114 |
|
| 115 |
=head2 Examples |
| 116 |
|
| 117 |
Create a table in a remote PostgreSQL database and insert a user in it: |
| 118 |
|
| 119 |
user-simple-admin --dsn 'dbi:Pg:dbname=userdb;host=dbserver' --dbuser dbadmin --dbpass dbs3kr37 --tbl test --create_rdbms --new_user --login someone --name 'A random user' |
| 120 |
|
| 121 |
Change that user's password in order to enable the account |
| 122 |
|
| 123 |
user-simple-admin --dsn 'dbi:Pg:dbname=userdb;host=dbserver' --dbuser dbadmin --dbpass dbs3kr37 --tbl test --mod_user --login someone --passwd thepassword |
| 124 |
|
| 125 |
Remove that user |
| 126 |
|
| 127 |
user-simple-admin --dsn 'dbi:Pg:dbname=userdb;host=dbserver' --dbuser dbadmin --dbpass dbs3kr37 --tbl test --remove_user --login someone |
| 128 |
|
| 129 |
Get the users listing in a DBD::XBase database |
| 130 |
|
| 131 |
user-simple-admin --dsn 'dbi:XBase:/home/user/databases/users' --tbl user_simple --dump_users |
| 132 |
|
| 133 |
=head1 DEPENDS ON |
| 134 |
|
| 135 |
L<DBI> (and a suitable L<DBD> backend) |
| 136 |
|
| 137 |
L<Getopt::Long> |
| 138 |
|
| 139 |
L<User::Simple::Admin> |
| 140 |
|
| 141 |
=head1 SEE ALSO |
| 142 |
|
| 143 |
L<User::Simple> and L<User::Simple::Admin> |
| 144 |
|
| 145 |
=head1 AUTHOR |
| 146 |
|
| 147 |
Gunnar Wolf <[email protected]> |
| 148 |
|
| 149 |
=head1 COPYRIGHT |
| 150 |
|
| 151 |
Copyright 2005 Gunnar Wolf / Instituto de Investigaciones Económicas UNAM |
| 152 |
This module is Free Software, it can be redistributed under the same terms |
| 153 |
as Perl. |
| 154 |
|
| 155 |
=cut |
| 156 |
|
| 157 |
use lib qw(/home/gwolf/User-Simple/lib); |
| 158 |
use strict; |
| 159 |
use warnings; |
| 160 |
use Carp; |
| 161 |
use DBI; |
| 162 |
use Getopt::Long; |
| 163 |
use User::Simple::Admin; |
| 164 |
|
| 165 |
my (%conf, $db, $ua); |
| 166 |
|
| 167 |
# Default values, to be overwritten by Getopt::Long |
| 168 |
%conf = ( dsn => 'DBI:XBase:/tmp/user_simple', |
| 169 |
dbtable => 'user_simple', |
| 170 |
dbuser => undef, |
| 171 |
dbpass => undef, |
| 172 |
verbose => 1, |
| 173 |
create_plain => undef, |
| 174 |
create_rdbms => undef, |
| 175 |
new_user => undef, |
| 176 |
remove_user => undef, |
| 177 |
mod_user => undef, |
| 178 |
login => undef, |
| 179 |
passwd => undef, |
| 180 |
name => undef, |
| 181 |
level => undef |
| 182 |
); |
| 183 |
|
| 184 |
GetOptions ('dsn=s' => \$conf{dsn}, |
| 185 |
'tbl=s' => \$conf{dbtable}, |
| 186 |
'dbuser=s' => \$conf{dbuser}, |
| 187 |
'dbpass=s' => \$conf{dbpass}, |
| 188 |
'create_plain' => \$conf{create_plain}, |
| 189 |
'create_rdbms' => \$conf{create_rdbms}, |
| 190 |
'dump_users' => \$conf{dump_users}, |
| 191 |
'new_user' => \$conf{new_user}, |
| 192 |
'remove_user' => \$conf{remove_user}, |
| 193 |
'mod_user' => \$conf{mod_user}, |
| 194 |
'login=s' => \$conf{login}, |
| 195 |
'passwd=s' => \$conf{passwd}, |
| 196 |
'name=s' => \$conf{name}, |
| 197 |
'level=i' => \$conf{level}, |
| 198 |
'quiet' => sub {$conf{verbose} = 0}, |
| 199 |
'verbose' => sub {$conf{verbose} = 2} |
| 200 |
) || usage_err(); |
| 201 |
debug(2,'Options parsed correctly'); |
| 202 |
|
| 203 |
#use Data::Dumper; warn Dumper \%conf; |
| 204 |
|
| 205 |
# Check we are requested to carry out at least one operation - Finish |
| 206 |
# otherwise. Check as well for mutually exclusive options |
| 207 |
usage_err() unless (# Operations |
| 208 |
$conf{create_plain} or $conf{create_rdbms} or |
| 209 |
$conf{new_user} or $conf{remove_user} or |
| 210 |
$conf{mod_user} or $conf{dump_users} or |
| 211 |
# Mutually exclusive |
| 212 |
($conf{create_plain} and $conf{create_rdbms}) or |
| 213 |
($conf{new_user} and $conf{remove_user}) or |
| 214 |
($conf{new_user} and $conf{mod_user}) or |
| 215 |
($conf{mod_user} and $conf{remove_user}) |
| 216 |
); |
| 217 |
|
| 218 |
# Connect to the DB |
| 219 |
$db = DBI->connect($conf{dsn},$conf{dbuser},$conf{dbpass},{AutoCommit => 1}); |
| 220 |
ref($db) or dbconn_err(); |
| 221 |
debug(2,'Database connection established'); |
| 222 |
|
| 223 |
# get a User::Simple::Admin instance (creating the structure if we were |
| 224 |
# requested to) |
| 225 |
if ($conf{create_rdbms}) { |
| 226 |
$ua = User::Simple::Admin->create_rdbms_db_structure($db, $conf{dbtable}); |
| 227 |
} elsif ($conf{create_plain}) { |
| 228 |
$ua = User::Simple::Admin->create_plain_db_structure($db, $conf{dbtable}); |
| 229 |
} else { |
| 230 |
$ua = User::Simple::Admin->new($db, $conf{dbtable}); |
| 231 |
} |
| 232 |
ref($ua) or useradmin_err(); |
| 233 |
debug(2,'User::Simple::Admin instance established'); |
| 234 |
|
| 235 |
# Ok, everything is now ready - Do what we were told to do! |
| 236 |
new_user() if $conf{new_user}; |
| 237 |
remove_user() if $conf{remove_user}; |
| 238 |
mod_user() if $conf{mod_user}; |
| 239 |
dump_users() if $conf{dump_users}; |
| 240 |
|
| 241 |
debug(2,'Program finished successfully'); |
| 242 |
|
| 243 |
exit 0; |
| 244 |
|
| 245 |
sub new_user { |
| 246 |
# For new user creation, we only require a login |
| 247 |
usage_err() unless $conf{login}; |
| 248 |
|
| 249 |
unless ($ua->new_user($conf{login}, $conf{name}, |
| 250 |
$conf{passwd}, $conf{level})) { |
| 251 |
newuser_err(); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
sub remove_user { |
| 256 |
# For user remotion, we require a login - But name, password and level |
| 257 |
# should not be specified. |
| 258 |
my ($id); |
| 259 |
usage_err() if ($conf{name} or $conf{passwd} or $conf{level}); |
| 260 |
|
| 261 |
$id = $ua->id($conf{login}) or no_user_err(); |
| 262 |
|
| 263 |
$ua->remove_user($id) or remove_err(); |
| 264 |
} |
| 265 |
|
| 266 |
sub mod_user { |
| 267 |
# For user modification, we require at least one of name, password and |
| 268 |
# level - and we require login _not_ to be set. |
| 269 |
my ($id); |
| 270 |
usage_err() unless ($conf{name} or $conf{passwd} or $conf{level} or |
| 271 |
!$conf{login}); |
| 272 |
|
| 273 |
$id = $ua->id($conf{login}) or no_user_err(); |
| 274 |
|
| 275 |
if (defined $conf{name}) { |
| 276 |
$ua->set_name($id, $conf{name}) or mod_err(); |
| 277 |
} |
| 278 |
if (defined $conf{level}) { |
| 279 |
$ua->set_level($id, $conf{level}) or mod_err(); |
| 280 |
} |
| 281 |
if (defined $conf{passwd}) { |
| 282 |
$ua->set_passwd($id, $conf{passwd}) or mod_err(); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
sub dump_users { |
| 287 |
my %users = $ua->dump_users; |
| 288 |
|
| 289 |
# Print the header first |
| 290 |
printf("%-4s %-15s %-30s %-5s\n", 'ID', 'Login', 'Name', 'Level'); |
| 291 |
print '='x60,"\n"; |
| 292 |
|
| 293 |
for my $user (sort {$a<=>$b} keys %users) { |
| 294 |
printf("%4d %-15s %-30s %5d\n", $user, $users{$user}{login}, |
| 295 |
$users{$user}{name}, $users{$user}{level}); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
sub usage_err { |
| 300 |
my $progname = $0; |
| 301 |
$progname =~ s!^.+/([^/]+)$!$1!; |
| 302 |
|
| 303 |
debug(1,<<"USAGE"); |
| 304 |
$progname: Incorrect invocation! |
| 305 |
|
| 306 |
What you always have to specify: |
| 307 |
- How to get to the information: a DSN (--dsn), the table name (--tbl), |
| 308 |
and probably the database user (--dbuser) and password (--dbpass) |
| 309 |
- An action to perform: --new_user, --mod_user, --remove_user |
| 310 |
- The attributes needed for your query (--login, --name, --level, --password) |
| 311 |
- Optionally, to get a listing of the users in the database, use --dump_users |
| 312 |
|
| 313 |
User creation example: |
| 314 |
$progname --dsn 'dbi:Pg:dbname=database;host=some.host.org' |
| 315 |
--table user_simple --dbuser useradmin --dbpass s3kr3t --new_user |
| 316 |
--login joe --name 'Joe Schmoe' --passwd mys3kr17 --level 5 |
| 317 |
|
| 318 |
User remotion: |
| 319 |
$progname --dsn 'dbi:Pg:dbname=database;host=some.host.org' |
| 320 |
--table user_simple--dbuser useradmin --dbpass s3kr3t --remove_user |
| 321 |
--login john |
| 322 |
|
| 323 |
User modification: |
| 324 |
$progname --dsn 'dbi:Pg:dbname=database;host=some.host.org' |
| 325 |
--table user_simple --dbuser useradmin --dbpass s3kr3t --new_user |
| 326 |
--login joe --name 'Joe A. Schmoe' --passwd dont_forget_it --level 2 |
| 327 |
|
| 328 |
Please check the full documentation for further details: |
| 329 |
perldoc $0 |
| 330 |
|
| 331 |
USAGE |
| 332 |
|
| 333 |
exit 1; |
| 334 |
} |
| 335 |
|
| 336 |
sub dbconn_err { |
| 337 |
debug(1,<<"DBERR"); |
| 338 |
Could not open DB connection - Please check that the provided data source |
| 339 |
(dsn), user (dbuser) and password (dbpass) are correct. |
| 340 |
DBERR |
| 341 |
|
| 342 |
exit 2; |
| 343 |
} |
| 344 |
|
| 345 |
sub useradmin_err { |
| 346 |
debug(1,<<"USERADMIN"); |
| 347 |
Could not create User::Simple::Admin - This might have happened because you |
| 348 |
requested to create a table with the same name an existing one, or because you |
| 349 |
asked to use a table which does not yet exist. |
| 350 |
USERADMIN |
| 351 |
|
| 352 |
exit 3; |
| 353 |
} |
| 354 |
|
| 355 |
sub newuser_err { |
| 356 |
debug(1,<<"NEWUSER"); |
| 357 |
Could not create the requested user - This can be because the requested login |
| 358 |
is already registered. |
| 359 |
NEWUSER |
| 360 |
|
| 361 |
exit 4; |
| 362 |
} |
| 363 |
|
| 364 |
sub no_user_err { |
| 365 |
debug(1,<<"NOUSER"); |
| 366 |
Could not find the requested user in the database - The specified login might |
| 367 |
be wrong, or the user might have been removed. |
| 368 |
NOUSER |
| 369 |
|
| 370 |
exit 5; |
| 371 |
} |
| 372 |
|
| 373 |
sub remove_err { |
| 374 |
debug(1,<<"REMOVE"); |
| 375 |
Could not remove the requested user from the database - It might be being |
| 376 |
referred to from another table if you have such a setup. |
| 377 |
REMOVE |
| 378 |
|
| 379 |
exit 6; |
| 380 |
} |
| 381 |
|
| 382 |
sub mod_err { |
| 383 |
debug(1,<<"MODIF"); |
| 384 |
Could not modify the requested user in the database |
| 385 |
MODIF |
| 386 |
|
| 387 |
exit 7; |
| 388 |
} |
| 389 |
|
| 390 |
sub debug { |
| 391 |
my $level = shift; |
| 392 |
return unless $conf{verbose} >= $level; |
| 393 |
warn @_,"\n"; |
| 394 |
} |