From ebcd35d69183edbec4a6eb15d7502018ee4ddb2c Mon Sep 17 00:00:00 2001 From: paspo Date: Wed, 30 Mar 2016 23:19:15 +0200 Subject: [PATCH] manage multiple groups --- README.md | 10 ++++------ importa_utenti.pl | 20 ++++++++++++-------- test.csv | 11 +++++------ 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index fde46e4..24e4b81 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,16 @@ Batch import a lot of users and groups. You need a CSV file with this layout: - username Firstname Lastname group password - + username Firstname Lastname password group1 [group2] [group3] [groupN] + 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). +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). - +The import process will create all non-existing groups. If the user already exists then the line is skipped (so if you specify a nonexistent group for this user, the group won't be created). diff --git a/importa_utenti.pl b/importa_utenti.pl index ae702af..3058763 100755 --- a/importa_utenti.pl +++ b/importa_utenti.pl @@ -32,7 +32,7 @@ while() { chomp $_; $_ =~ s/\s+$//; - my ($username, $firstName, $lastName, $group, $password) = split(/$separator/, $_); + my ($username, $firstName, $lastName, $password, @groups) = split(/$separator/, $_); if( ! $username) { next; @@ -53,11 +53,13 @@ while() { next; } - if(!$accountsDb->get($group)) { - my $groupRecord = $accountsDb->new_record($group, { - 'type' => 'group' - }); - warn "[INFO] created group $group.\n"; + foreach my $group (@groups) { + if(!$accountsDb->get($group)) { + my $groupRecord = $accountsDb->new_record($group, { + 'type' => 'group' + }); + warn "[INFO] created group $group.\n"; + } } my $record = $accountsDb->new_record($username, { @@ -77,8 +79,10 @@ while() { next; } - $accountsDb->add_user_to_groups($username, $group); - + 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; diff --git a/test.csv b/test.csv index 0b6effe..2e637eb 100644 --- a/test.csv +++ b/test.csv @@ -1,6 +1,5 @@ -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 - +utente1 Nome1 Cognome1 alsdlajsd utonti1 lusers1 everybody +utente2 Nome2 Cognome2 alsdlajsd utonti1 everybody +utente3 Nome3 Cognome3 alsdlajsd lusers1 everybody +utente4 Nome4 Cognome4 alsdlajsd lusers4 lusers1 everybody +utente5 Nome5 Cognome5 alsdlajsd lusers4 everybody