- changed command line syntax (we need command line switches)

- added help switch
- added input file switch (it's still possible to use STDIN)
- added separator switch
- added add_existing_users_to_groups switch (when importing an existing user, we still parse group creation and assignation)
This commit is contained in:
Paolo Asperti 2016-03-31 01:17:24 +02:00
parent ebcd35d691
commit 443c885da0
1 changed files with 58 additions and 34 deletions

View File

@ -7,25 +7,35 @@
use strict; use strict;
use Getopt::Long;
use esmith::AccountsDB; use esmith::AccountsDB;
use esmith::event; use esmith::event;
use File::Temp qw(tempfile); use File::Temp qw(tempfile);
my $file = shift; my $file;
my $separator = shift; my $separator = "\t";
my $add_existing_users_to_groups;
my $user_already_exists;
my $displayHelp;
my $accountsDb = esmith::AccountsDB->open() || die("Could not open accounts DB"); my $accountsDb = esmith::AccountsDB->open() || die("Could not open accounts DB");
if($file) { GetOptions (
"file=s" => \$file,
"separator=s" => \$separator,
"add_existing_users_to_groups" => \$add_existing_users_to_groups,
"help" => \$displayHelp
) or displayHelp();
if ( $displayHelp ) {
displayHelp();
}
if ( $file ) {
open(FH, "<", $file) or die; open(FH, "<", $file) or die;
} else { } else {
open(FH, "-"); open(FH, "-");
} }
if( ! $separator) {
$separator = "\t";
}
while(<FH>) { while(<FH>) {
# Remove trailing whitespace: # Remove trailing whitespace:
@ -48,9 +58,14 @@ while(<FH>) {
next; next;
} }
$user_already_exists = 0;
if($accountsDb->get($username)) { if($accountsDb->get($username)) {
warn "[WARNING] Account `$username` already registered: skipped.\n"; warn "[WARNING] Account `$username` already registered: skipped.\n";
next; if ( ! $add_existing_users_to_groups ) {
next;
} else {
$user_already_exists = 1;
}
} }
foreach my $group (@groups) { foreach my $group (@groups) {
@ -62,39 +77,48 @@ while(<FH>) {
} }
} }
my $record = $accountsDb->new_record($username, { if ( ! $user_already_exists ) {
'type' => 'user',
'FirstName' => $firstName,
'LastName' => $lastName,
'Samba' => 'enabled'
});
if( ! $record ) { my $record = $accountsDb->new_record($username, {
warn "[ERROR] Account `$username` record creation failed.\n"; 'type' => 'user',
next; 'FirstName' => $firstName,
} 'LastName' => $lastName,
'Samba' => 'enabled'
});
if( ! $record ) {
warn "[ERROR] Account `$username` record creation failed.\n";
next;
}
if( ! esmith::event::event_signal('user-create', $username) ) {
warn "[ERROR] Account `$username` user-create event failed.\n";
next;
}
if($password) {
my ($pfh, $pfilename) = tempfile('import_users_XXXXX', UNLINK=>0, DIR=>'/tmp');
print $pfh $password;
close($pfh);
if( ! esmith::event::event_signal('password-modify', $username, $pfilename) ) {
warn "[ERROR] Account `$username` user-create event failed.\n";
next;
}
unlink $pfilename;
}
if( ! esmith::event::event_signal('user-create', $username) ) {
warn "[ERROR] Account `$username` user-create event failed.\n";
next;
} }
foreach my $group (@groups) { foreach my $group (@groups) {
$accountsDb->add_user_to_groups($username, $group); $accountsDb->add_user_to_groups($username, $group);
} }
if($password) {
my ($pfh, $pfilename) = tempfile('import_users_XXXXX', UNLINK=>0, DIR=>'/tmp');
print $pfh $password;
close($pfh);
if( ! esmith::event::event_signal('password-modify', $username, $pfilename) ) {
warn "[ERROR] Account `$username` user-create event failed.\n";
next;
}
unlink $pfilename;
}
warn "[INFO] imported $username\n"; warn "[INFO] imported $username\n";
} }
sub displayHelp {
die("Usage: $0 [--file INPUTFILE] [--separator SEPARATOR] [--add_existing_users_to_groups]\n");
}