From 443c885da02c3555df1e2f445b42e30362b49e8b Mon Sep 17 00:00:00 2001 From: paspo Date: Thu, 31 Mar 2016 01:17:24 +0200 Subject: [PATCH] - 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) --- importa_utenti.pl | 92 +++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 34 deletions(-) diff --git a/importa_utenti.pl b/importa_utenti.pl index 3058763..7143efc 100755 --- a/importa_utenti.pl +++ b/importa_utenti.pl @@ -7,25 +7,35 @@ use strict; - +use Getopt::Long; use esmith::AccountsDB; use esmith::event; use File::Temp qw(tempfile); -my $file = shift; -my $separator = shift; +my $file; +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"); -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; } else { open(FH, "-"); } -if( ! $separator) { - $separator = "\t"; -} - while() { # Remove trailing whitespace: @@ -48,9 +58,14 @@ while() { next; } + $user_already_exists = 0; if($accountsDb->get($username)) { 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) { @@ -62,39 +77,48 @@ while() { } } - my $record = $accountsDb->new_record($username, { - 'type' => 'user', - 'FirstName' => $firstName, - 'LastName' => $lastName, - 'Samba' => 'enabled' - }); + if ( ! $user_already_exists ) { - if( ! $record ) { - warn "[ERROR] Account `$username` record creation failed.\n"; - next; - } + my $record = $accountsDb->new_record($username, { + 'type' => 'user', + '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) { $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"; } + + +sub displayHelp { + die("Usage: $0 [--file INPUTFILE] [--separator SEPARATOR] [--add_existing_users_to_groups]\n"); +}