NEW: user deletion, user removal from groups

This commit is contained in:
Paolo Asperti 2016-09-18 10:05:58 +02:00
parent 33762621e5
commit 3a4f502165
2 changed files with 38 additions and 12 deletions

View File

@ -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.

View File

@ -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(<FH>) {
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(<FH>) {
}
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(<FH>) {
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");
}