From 3a4f5021654131cd9c55d9a4b9e6a1f1402801af Mon Sep 17 00:00:00 2001 From: Paolo Asperti Date: Sun, 18 Sep 2016 10:05:58 +0200 Subject: [PATCH] NEW: user deletion, user removal from groups --- README.md | 8 ++++++-- importa_utenti.pl | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6e7cba9..a5fb739 100644 --- a/README.md +++ b/README.md @@ -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. - ./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: @@ -19,4 +19,8 @@ Some examples: cat test.csv | ./importa_utenti.pl --separator "," --add_existing_users_to_groups 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. diff --git a/importa_utenti.pl b/importa_utenti.pl index 7143efc..8a39251 100755 --- a/importa_utenti.pl +++ b/importa_utenti.pl @@ -15,6 +15,8 @@ use File::Temp qw(tempfile); my $file; my $separator = "\t"; my $add_existing_users_to_groups; +my $remove_users; +my $record; my $user_already_exists; my $displayHelp; my $accountsDb = esmith::AccountsDB->open() || die("Could not open accounts DB"); @@ -23,6 +25,7 @@ GetOptions ( "file=s" => \$file, "separator=s" => \$separator, "add_existing_users_to_groups" => \$add_existing_users_to_groups, + "remove_users" => \$remove_users, "help" => \$displayHelp ) or displayHelp(); @@ -48,27 +51,39 @@ while() { next; } - if( ! $firstName) { + if( ! $firstName && ! $remove_users) { warn "[WARNING] Account `$username` is missing FirstName column: skipped.\n"; next; } - if( ! $lastName) { + if( ! $lastName && ! $remove_users) { warn "[WARNING] Account `$username` is missing LastName column: skipped.\n"; next; } $user_already_exists = 0; - if($accountsDb->get($username)) { - warn "[WARNING] Account `$username` already registered: skipped.\n"; - if ( ! $add_existing_users_to_groups ) { - next; + $record = $accountsDb->get($username); + if($record) { + if ( $remove_users ) { + $record->delete; + warn "[INFO] deleted $username\n"; + next; } else { - $user_already_exists = 1; + if ( ! $add_existing_users_to_groups ) { + warn "[WARNING] Account `$username` already registered: skipped.\n"; + next; + } else { + $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)) { my $groupRecord = $accountsDb->new_record($group, { 'type' => 'group' @@ -111,7 +126,14 @@ while() { } foreach my $group (@groups) { - $accountsDb->add_user_to_groups($username, $group); + 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); + warn "[INFO] $username added to group $group\n"; + } } warn "[INFO] imported $username\n"; @@ -120,5 +142,5 @@ while() { 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"); }