first import
This commit is contained in:
parent
5e6836f496
commit
b731b3382d
18
README.md
18
README.md
@ -1 +1,19 @@
|
|||||||
# nethserver-usercsvimport
|
# nethserver-usercsvimport
|
||||||
|
Batch import a lot of users and groups.
|
||||||
|
You need a CSV file with this layout:
|
||||||
|
|
||||||
|
username Firstname Lastname group password
|
||||||
|
|
||||||
|
See test.csv file for a sample.
|
||||||
|
You can use the TAB as a separator (this is the default), or whatever character you want.
|
||||||
|
Beware that Microsoft Excel uses the semicolon (;) as a separator instead the comma (,) for a CSV file (COMMA separated value).
|
||||||
|
|
||||||
|
To import, simply call the script, by passing the CSV file as a parameter and the separator as the second parameter.
|
||||||
|
|
||||||
|
./importa_utenti.pl test.CSV
|
||||||
|
./importa_utenti.pl test.CSV ";"
|
||||||
|
./importa_utenti.pl test.CSV ","
|
||||||
|
|
||||||
|
The import process will create all non-existing groups. If the user already exists then the line is skipped (so if you specified a nonexistent group for this user, the group won't be created).
|
||||||
|
|
||||||
|
|
||||||
|
96
importa_utenti.pl
Executable file
96
importa_utenti.pl
Executable file
@ -0,0 +1,96 @@
|
|||||||
|
#!/usr/bin/perl
|
||||||
|
|
||||||
|
# TODO: quando un gruppo viene cancellato, nel database cambia "tipo"
|
||||||
|
# e da "group" diventa "group-deleted".
|
||||||
|
# questo fa casino, perchè il gruppo esiste, ma è cancellato.
|
||||||
|
# bisogna gestire correttamente questa cosa
|
||||||
|
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
|
||||||
|
use esmith::AccountsDB;
|
||||||
|
use esmith::event;
|
||||||
|
use File::Temp qw(tempfile);
|
||||||
|
|
||||||
|
my $file = shift;
|
||||||
|
my $separator = shift;
|
||||||
|
my $accountsDb = esmith::AccountsDB->open() || die("Could not open accounts DB");
|
||||||
|
|
||||||
|
if($file) {
|
||||||
|
open(FH, "<", $file) or die;
|
||||||
|
} else {
|
||||||
|
open(FH, "-");
|
||||||
|
}
|
||||||
|
|
||||||
|
if( ! $separator) {
|
||||||
|
$separator = "\t";
|
||||||
|
}
|
||||||
|
|
||||||
|
while(<FH>) {
|
||||||
|
|
||||||
|
# Remove trailing whitespace:
|
||||||
|
chomp $_;
|
||||||
|
$_ =~ s/\s+$//;
|
||||||
|
|
||||||
|
my ($username, $firstName, $lastName, $group, $password) = split(/$separator/, $_);
|
||||||
|
|
||||||
|
if( ! $username) {
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( ! $firstName) {
|
||||||
|
warn "[WARNING] Account `$username` is missing FirstName column: skipped.\n";
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( ! $lastName) {
|
||||||
|
warn "[WARNING] Account `$username` is missing LastName column: skipped.\n";
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($accountsDb->get($username)) {
|
||||||
|
warn "[WARNING] Account `$username` already registered: skipped.\n";
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!$accountsDb->get($group)) {
|
||||||
|
my $groupRecord = $accountsDb->new_record($group, {
|
||||||
|
'type' => 'group'
|
||||||
|
});
|
||||||
|
warn "[INFO] created group $group.\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
$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";
|
||||||
|
|
||||||
|
}
|
6
test.csv
Normal file
6
test.csv
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
utente1 Nome1 Cognome1 utonti1 alsdlajsd
|
||||||
|
utente2 Nome2 Cognome2 utonti1 alsdlajsd
|
||||||
|
utente3 Nome3 Cognome3 lusers1 alsdlajsd
|
||||||
|
utente4 Nome4 Cognome4 lusers4 alsdlajsd
|
||||||
|
utente5 Nome5 Cognome5 lusers4 alsdlajsd
|
||||||
|
|
|
Loading…
x
Reference in New Issue
Block a user