NEW: user deletion, user removal from groups
This commit is contained in:
parent
33762621e5
commit
3a4f502165
@ -10,7 +10,7 @@ Beware that Microsoft Excel uses the semicolon (;) as a separator instead the co
|
|||||||
|
|
||||||
To import, simply call the script passing the input file as STDIN or via the --file switch.
|
To import, simply call the script passing the input file as STDIN or via the --file switch.
|
||||||
|
|
||||||
./importa_utenti.pl [--file INPUTFILE] [--separator SEPARATOR] [--add_existing_users_to_groups]
|
./importa_utenti.pl [--file INPUTFILE] [--separator SEPARATOR] [--add_existing_users_to_groups] [--remove_users]
|
||||||
|
|
||||||
Some examples:
|
Some examples:
|
||||||
|
|
||||||
@ -19,4 +19,8 @@ Some examples:
|
|||||||
cat test.csv | ./importa_utenti.pl --separator "," --add_existing_users_to_groups
|
cat test.csv | ./importa_utenti.pl --separator "," --add_existing_users_to_groups
|
||||||
|
|
||||||
You can specify a custom field separator.
|
You can specify a custom field separator.
|
||||||
The import process will create all non-existing groups. If the user already exists and the "add_existing_users_to_groups" switch is NOT set, then the line is skipped (so if you specify a nonexistent group for this user, the group won't be created). If you enable "add_existing_users_to_groups" and the user is already present, the nonexisting groups are created and the user is assigned to all the specified groups.
|
The import process will create all non-existing groups. If the user already exists and the "add_existing_users_to_groups" switch is NOT set, then the line is skipped (so if you specify a nonexistent group for this user, the group won't be created). If you enable "add_existing_users_to_groups" and the user is already present, the non-existing groups are created and the user is assigned to all the specified groups.
|
||||||
|
|
||||||
|
If you specify a group name which starts with a "-" (dash), then the user will be removed from that group. This feature is working even if you don't enable "add_existing_users_to_groups".
|
||||||
|
|
||||||
|
To remove users you can use the same .CSV file you used for creation, or you can provide a file with a users list (one user per line). This file will be parsed for user deletion if you specify the "--remove_users" parameter.
|
||||||
|
@ -15,6 +15,8 @@ use File::Temp qw(tempfile);
|
|||||||
my $file;
|
my $file;
|
||||||
my $separator = "\t";
|
my $separator = "\t";
|
||||||
my $add_existing_users_to_groups;
|
my $add_existing_users_to_groups;
|
||||||
|
my $remove_users;
|
||||||
|
my $record;
|
||||||
my $user_already_exists;
|
my $user_already_exists;
|
||||||
my $displayHelp;
|
my $displayHelp;
|
||||||
my $accountsDb = esmith::AccountsDB->open() || die("Could not open accounts DB");
|
my $accountsDb = esmith::AccountsDB->open() || die("Could not open accounts DB");
|
||||||
@ -23,6 +25,7 @@ GetOptions (
|
|||||||
"file=s" => \$file,
|
"file=s" => \$file,
|
||||||
"separator=s" => \$separator,
|
"separator=s" => \$separator,
|
||||||
"add_existing_users_to_groups" => \$add_existing_users_to_groups,
|
"add_existing_users_to_groups" => \$add_existing_users_to_groups,
|
||||||
|
"remove_users" => \$remove_users,
|
||||||
"help" => \$displayHelp
|
"help" => \$displayHelp
|
||||||
) or displayHelp();
|
) or displayHelp();
|
||||||
|
|
||||||
@ -48,27 +51,39 @@ while(<FH>) {
|
|||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( ! $firstName) {
|
if( ! $firstName && ! $remove_users) {
|
||||||
warn "[WARNING] Account `$username` is missing FirstName column: skipped.\n";
|
warn "[WARNING] Account `$username` is missing FirstName column: skipped.\n";
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( ! $lastName) {
|
if( ! $lastName && ! $remove_users) {
|
||||||
warn "[WARNING] Account `$username` is missing LastName column: skipped.\n";
|
warn "[WARNING] Account `$username` is missing LastName column: skipped.\n";
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
$user_already_exists = 0;
|
$user_already_exists = 0;
|
||||||
if($accountsDb->get($username)) {
|
$record = $accountsDb->get($username);
|
||||||
warn "[WARNING] Account `$username` already registered: skipped.\n";
|
if($record) {
|
||||||
|
if ( $remove_users ) {
|
||||||
|
$record->delete;
|
||||||
|
warn "[INFO] deleted $username\n";
|
||||||
|
next;
|
||||||
|
} else {
|
||||||
if ( ! $add_existing_users_to_groups ) {
|
if ( ! $add_existing_users_to_groups ) {
|
||||||
|
warn "[WARNING] Account `$username` already registered: skipped.\n";
|
||||||
next;
|
next;
|
||||||
} else {
|
} else {
|
||||||
$user_already_exists = 1;
|
$user_already_exists = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach my $group (@groups) {
|
if ( $remove_users ) {
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
|
GRP: foreach my $group (@groups) {
|
||||||
|
next GRP if $group =~ /^-/;
|
||||||
if(!$accountsDb->get($group)) {
|
if(!$accountsDb->get($group)) {
|
||||||
my $groupRecord = $accountsDb->new_record($group, {
|
my $groupRecord = $accountsDb->new_record($group, {
|
||||||
'type' => 'group'
|
'type' => 'group'
|
||||||
@ -111,7 +126,14 @@ while(<FH>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach my $group (@groups) {
|
foreach my $group (@groups) {
|
||||||
|
if ( $group =~ m/^\-/ ) {
|
||||||
|
$group =~ s/^\-//;
|
||||||
|
$accountsDb->remove_user_from_groups($username, $group);
|
||||||
|
warn "[INFO] $username removed from group $group\n";
|
||||||
|
} else {
|
||||||
$accountsDb->add_user_to_groups($username, $group);
|
$accountsDb->add_user_to_groups($username, $group);
|
||||||
|
warn "[INFO] $username added to group $group\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
warn "[INFO] imported $username\n";
|
warn "[INFO] imported $username\n";
|
||||||
@ -120,5 +142,5 @@ while(<FH>) {
|
|||||||
|
|
||||||
|
|
||||||
sub displayHelp {
|
sub displayHelp {
|
||||||
die("Usage: $0 [--file INPUTFILE] [--separator SEPARATOR] [--add_existing_users_to_groups]\n");
|
die("Usage: $0 [--file INPUTFILE] [--separator SEPARATOR] [--add_existing_users_to_groups] [--remove_users]\n");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user