- 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:
parent
ebcd35d691
commit
443c885da0
@ -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(<FH>) {
|
||||
|
||||
# Remove trailing whitespace:
|
||||
@ -48,9 +58,14 @@ while(<FH>) {
|
||||
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(<FH>) {
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user