FacebookTwitterFlickrYoutuberss

Import Users in csv

From Zentyal Linux Small Business Server
Jump to: navigation, search

This HOWTO is intended to show you how to import users into Zentyal from a CSV (comma separated file) in bulk.

For Zentyal 3.0

#perl
#!/usr/bin/perl

use strict;
use warnings;

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

EBox::init();

my @users;
open (my $USERS, 'users');

while (my $line = <$USERS>) {
    chomp ($line);
    my $user;
    my ($username, $givenname, $surname, $password) = split(',', $line);
    $user->{'user'} = $username;
    $user->{'givenname'} = $givenname;
    $user->{'surname'} = $surname;
    $user->{'password'} = $password;
    push (@users, $user);
}
close ($USERS);

foreach my $user (@users) {
    EBox::UsersAndGroups::User->create($user, 0);
}

1;

For eBox > 1.1.0

  1. Put the following script in a text file called bulkusers and make it executable for root in ebox's home directory (~ebox) or in /tmp, you can remove the files after using this script.
#perl
#!/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) = split(',',$line);
   $user->{'user'} = $username;
   $user->{'givenname'} = $givenname;
   $user->{'surname'} = $surname;
   $user->{'password'} = $password;
   push(@users,$user);
}
close($USERS);

foreach my $user (@users) {
   $usersModule->addUser($user, 0);
}

1;
2. Before running the script you need to have a file called users in the same directory. Inside the file it should look like the following:
jblow,Joe,Blow,jblowspassword,
jdoe,Jane,Doe,jdoespassword,
   Notes on this users file:
      1. No blank lines or the script blows up
      2. It must include a trailing comma after the password (otherwise the account may be created but the password isn't added). I exported this file from excel and thought that MSDOS CSV file type would be what to choose but the trailing commas were not added. Apparently Windows CSV file type adds the trailing commas.
      3. Before running the script open the file in vi or something and get rid of the trailing !M's on the end of lines (:%s/!M$//g is the command to do this in vi where !^M is created by pressing ctrl-v then ctrl-m)


= For eBox <== 1.0

  1. Put the following script in a text file called bulkusers and make it executable for root in root's home directory.
#perl
#!/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"); 
while(my $line = <$USERS>) {
   chomp ($line);
   my $user;
   my ($username,$fullname,$password) = split(',',$line);
   $user->{'user'} = $username;
   $user->{'fullname'} = $fullname;
   $user->{'password'} = $password;
   push(@users,$user);
}
close($USERS);

foreach my $user (@users) {
   $usersModule->addUser($user, 0);
}

1;
2. Before running the script you need to have a file called users in the same directory. Inside the file it should look like the following:
jblow,Joe Blow,jblowspassword,
jdoe,Jane Doe,jdoespassword,
   Notes on this users file:
      1. No blank lines or the script blows up
      2. It must include a trailing comma after the password (otherwise the account may be created but the password isn't added). I exported this file from excel and thought that MSDOS CSV file type would be what to choose but the trailing commas were not added. Apparently Windows CSV file type adds the trailing commas.
      3. Before running the script open the file in vi or something and get rid of the trailing !M's on the end of lines (:%s/!M$//g is the command to do this in vi where !^M is created by pressing ctrl-v then ctrl-m)


Thanks to Jamie Reid and other testers


Template:TracNotice

Personal tools
Namespaces

Variants
Actions

Zentyal Wiki

Zentyal Doc
Navigation
Toolbox