Author Topic: Adding many users in bulk to multiple groups.  (Read 9570 times)

tuxmania

  • Zen Apprentice
  • *
  • Posts: 45
  • Karma: +1/-0
    • View Profile
Adding many users in bulk to multiple groups.
« on: September 09, 2010, 09:13:28 am »
Hi,

Im tinkering a bit with Zentyal in advance of our deployment. I managed to add users to more than one group and thought i would toss it here if someone would ever want to do that:

The script is called groupadd.pl but you can name it anything.pl I had trouble running it from /root because of permissions but it works if i put it in my /home/adminuser dir and run it with sudo.


#!/usr/bin/perl

use strict;
use warnings;

use EBox;
use EBox::Global;

EBox::init();
my $usersModule = EBox::Global->modInstance('users');

my @users = ();
open(my $USERS,"users") or die "Error opening 'users' file: $!";
while(my $line = <$USERS>) {
   chomp ($line);
   my $user;
   my ($username,$givenname,$surname,$password,$group1, $group2) = split(',',$line);
   $user->{'user'} = $username;
   $user->{'givenname'} = $givenname;
   $user->{'surname'} = $surname;
   $user->{'password'} = $password;
print "\n Creating user: $username $givenname $surname \n";
   $usersModule->addUser($user,0);
   $usersModule->addUserToGroup($username, $group1);
   $usersModule->addUserToGroup($username, $group2);
}
close($USERS);

1;


The script uses a csv formatted file that has to be in the same path and be called users This is its format:

Username1,Firstname1,Lastname1,Password1, Firstgroup1, Secondgroup1
Username2,Firstname2,Lastname2,Password2, Firstgroup2, Secondgroup2

etc...





exekias

  • Zentyal Staff
  • Zen Warrior
  • *****
  • Posts: 196
  • Karma: +21/-0
    • View Profile
    • The Big Bug Theory
Re: Adding many users in bulk to multiple groups.
« Reply #1 on: November 04, 2010, 01:13:33 pm »
Thanks for sharing this tuxmania!

Great script

tuxmania

  • Zen Apprentice
  • *
  • Posts: 45
  • Karma: +1/-0
    • View Profile
Re: Adding many users in bulk to multiple groups.
« Reply #2 on: November 05, 2010, 07:35:36 am »
Yes, the script is great but not mine, i just changed someone elses.

simonschmeisser

  • Zen Apprentice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Adding many users in bulk to multiple groups.
« Reply #3 on: April 02, 2013, 10:07:55 am »
just for the record, here is my version which uses the API somewhat different and does ANY number of groups

Code: [Select]
#!/usr/bin/perl

use strict;
use warnings;

use EBox;
use EBox::UsersAndGroups::User;
use EBox::UsersAndGroups::Group;
use EBox::UsersAndGroups;

EBox::init();


open (my $USERS, 'new_users.txt');

while (my $line = <$USERS>) {
    chomp ($line);
    my $user;
    my ($username, $givenname, $surname, $password, $groups) = split(',', $line);
    $user->{'user'} = $username;
    $user->{'givenname'} = $givenname;
    $user->{'surname'} = $surname;
    $user->{'password'} = $password;
   
    print "\n Creating user: $username $givenname $surname \n";
    my $newUser = EBox::UsersAndGroups::User->create($user, 0);
    print "done\n";
    foreach my $groupname (split(' ' , $groups)) {
    print "Adding to Group $groupname \n";
    my $group = new EBox::UsersAndGroups->group($groupname);
    #print $group;
    $newUser->addGroup($group)
    }
}
close ($USERS);

1;

you'll need a file with the format
$username, $givenname, $surname, $password, $groups
where groups are SPACE seperated groups

have fun

bubnov-pi

  • Zen Samurai
  • ****
  • Posts: 425
  • Karma: +27/-0
    • View Profile
Re: Adding many users in bulk to multiple groups.
« Reply #4 on: July 09, 2013, 08:46:56 am »
Good idea, but I think, that " " is not a best separator for groups - better way is to use ";" - so your groups can be called for example "Mail users" or "Sale department".

zette

  • Zen Apprentice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: Adding many users in bulk to multiple groups.
« Reply #5 on: September 01, 2013, 01:23:26 pm »
Last script works, but needs groups, that are beeing assinged to user, to be created earlier.
I try to modify it adding:
Code: [Select]
my $group = new EBox::UsersAndGroups::Group->create($groupname);
but API returns:
Quote
Missing argument: entry|dn|ldifk

Any ideas how to deal with it?

paulreynand

  • Zen Apprentice
  • *
  • Posts: 19
  • Karma: +1/-0
    • View Profile
Re: Adding many users in bulk to multiple groups.
« Reply #6 on: November 11, 2013, 04:20:50 am »
hi guys,

any progress in this? im also looking for a way on how to do user and group bulk provisioning. I tried both scripts in this thread but no success.

The first script returns this error:

Creating user: useraccount firstname lastname
Can't locate object method "addUser" via package "EBox::Users" at groupprov1.pl line 23, <$USERS> line 1.


The second script  returns this error:

Can't locate EBox/UsersAndGroups/User.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at groupprov2.pl line 7.
BEGIN failed--compilation aborted at groupprov2.pl line 7.


Thanks in advance,

massoud

  • Zen Apprentice
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: Adding many users in bulk to multiple groups.
« Reply #7 on: February 04, 2019, 10:01:07 pm »
Hi

Could you please let me know how we can do it in the reverse I mean disable or delete users?

Thanks in advance!