Recent Topics

[4.1.x] Adding Users in Bulk

Started by on Jun 05, 2012 – Contents updated: Jun 05, 2012

Jun 05, 2012 13:37    

My b2evolution Version: 4.1.x

I'd like to add up to 150 users to a B2 install. In Wordpress there is a plugin to do it but all I could find for B2 Evo was script that only partially worked. Any ideas?

Jun 06, 2012 15:23

PHP

function add_user$login$pass$email )
{
    $User = new User();
    $User->set'login'$login );
    $User->set'pass'md5($pass) );
    $User->set'email'$email );
    $User->dbinsert();
}
 
$userlist array(
    array'name''mypass''e@ma.il' ),
    array'another''pass''my@addr.es' ),
);
 
foreach( $userlist as $user )
{
    add_user$user[0], $user[1], $user[2] );
}

Jun 06, 2012 15:26

You can add as many user params as you want

PHP

$User->setparamvalue );

See /inc/users/model/_user.class.php for more info

Jun 06, 2012 16:02

where does that code go then?

Jun 06, 2012 17:17

admin.php is the best place, anywhere after

PHP

/*
 * Asynchronous processing options that may be required on any page
 */
require_once $inc_path.'_async.inc.php';

Jun 06, 2012 17:22

thanks for your help but there's gotta be a more elegant way to do this. I don't fancy hacking admin.php every time I need to add a bunch of users. Wordpress has a plugin, it's a shame B2 doesn't.

Jun 07, 2012 09:37

OK over in http://forums.b2evolution.net/viewtopic.php?t=24498

Sam2kb said:

You should NEVER use this method, don't even bother fixing this script. You can easily adapt the code I posted in another topic to read user data from CSV file.

Ok - let's take another look at this feller: I'm going to try it here http://tecexplore.com/b2 cos it's a test install on an experimental web account. I'm no coder so look out B2

Jun 07, 2012 10:23

Well putting that into admin.php caused this error:

Code

MySQL error!
 
Duplicate entry 'name' for key 'user_login'(Errno=1062)
 
Your query: DataObject::dbinsert()
 
INSERT INTO evo_users (user_login, user_pass, user_locale, user_email, user_level, dateYMDhour, user_allow_msgform, user_notify, user_notify_moderation, user_unsubscribe_key, user_showonline)
 
VALUES ('name', 'a029d0df84eb5549c641e04a9ef389e5', 'en-US', 'e@ma.il', 1, '2012-06-07 03:19:49', '3', 0, 0, 'tbc6m6RHBcmIxMtkKoYilpGnczLxcLfx', 1)

I guess that's pretty obvious to someone who understands these things but I'm not very clear. It would seem that I may have tried to duplicate a user.

Are you sure that putting this into admin.php is the way to go?

Jun 07, 2012 20:02

Are you sure that putting this into admin.php is the way to go?

Yes, it's the easiest and secure enough way. Of course, you will need to remove the script (or comment it out) after you finish adding users.

Edit the script this way

PHP

foreach( $userlist as $user )
{
    if( user_exists($user[0]) )
    {
        $Messages->add'User exists: '.$user[0], 'note' );
        continue; // skip
    }
    add_user$user[0], $user[1], $user[2] );
 
    $Messages->add'User added: '.$user[0], 'success' );
}

Jun 07, 2012 20:04

Please post here what you have done, I'll check.

Jun 07, 2012 20:47

what I'm concerned about here is that this could be as arduous as adding users one by one. Can we at least be talking about calling up a CSV file for the users.

Jun 08, 2012 08:10

Ok - adapted code had the effect of creating two new users both at admin level. I removed the code and deleted the users and then added a third user to the code and it created three users at admin level.

Sorry to be so squeamish this works a treat thanks. Now I'll see if I can figure how to reduce their perms level and add to specific user groups. For ref the full code I added is below.

PHP

function add_user$login$pass$email 
    $User = new User(); 
    $User->set'login'$login ); 
    $User->set'pass'md5($pass) ); 
    $User->set'email'$email ); 
    $User->dbinsert(); 
 
$userlist array
    array'adminhack1''mypass''e@ma.il' ), 
    array'adminhack2''pass''my@addr.es' ), 
    array'adminhack3''pass2''my@addr2.es' ), 
); 
foreach( $userlist as $user 
    if( user_exists($user[0]) ) 
    { 
        $Messages->add'User exists: '.$user[0], 'note' ); 
        continue; // skip 
    
    add_user$user[0], $user[1], $user[2] ); 
 
    $Messages->add'User added: '.$user[0], 'success' ); 
}

Jun 08, 2012 08:43

I've edited the code (below) to try and add the users to group 4 - basic bloggers. The code below adds the users but still as admin. I've tried various hacks that cause various errors but I'm stumbling around in the dark. I've tried $Group_ID

PHP

function add_user$login$pass$email$Group)  
{  
    $User = new User();  
    $User->set'login'$login );  
    $User->set'pass'md5($pass) );  
    $User->set'email'$email ); 
    $User->set'group'$Group ); 
    $User->dbinsert();  
}  
 
$userlist array(  
    array'adminhack4''mypass''e@ma.il''4' ),  
    array'adminhack5''pass''my@addr.es''4'),  
    array'adminhack6''pass2''my@addr2.es''4' ), 
    array'adminhack7''pass2b''my@addr2b.es''4' ),   
);  
foreach( $userlist as $user )  
{  
    if( user_exists($user[0]) )  
    {  
        $Messages->add'User exists: '.$user[0], 'note' );  
        continue; // skip  
    }  
    add_user$user[0], $user[1], $user[2], );  
 
    $Messages->add'User added: '.$user[0], 'success' );  
}

Jun 08, 2012 15:29

Hey, I cracked it !!!!!

OK so I'm talking to myself

added grp_ID to this line:

PHP

function add_user$login$pass$email$grp_ID)

tried several combos $Group_ID etc etc - took a look at the actual database for inspiration.

This

PHP

$User->set'group'$Group );

should be this

PHP

$User->set'grp_ID'$grp_ID );

added the number 4 for user group 4

PHP

$userlist array(  
    array'adminhack4''mypass''e@ma.il''4' ),

and finally this
added $user[3]); to this

PHP

add_user$user[0], $user[1], $user[2], $user[3]);

I've no idea what's going on, I just applied some kind of pattern logic

I'll add some other params now

Jun 08, 2012 15:43

Completely cracked it now. Set up to insert username, password, email, email validated yes, locale and blog group. Code below.

PHP

function add_user$login$pass$email$grp_ID$locale$validated)  
 
{  
    $User = new User();  
    $User->set'login'$login );  
    $User->set'pass'md5($pass) );  
    $User->set'email'$email ); 
    $User->set'grp_ID'$grp_ID ); 
    $User->set'locale'$locale ); 
    $User->set'validated'$validated ); 
    $User->dbinsert();  
}  
 
$userlist array(  
    array'adminhack8''mypass8''e@8ma.il''4''en-GB''1' ),  
    array'adminhack9''mypass9''e@9ma.il''4''en-GB''1' ),   
    array'adminhack10''mypass10''e@10ma.il''4''en-GB''1' ), 
);  
foreach( $userlist as $user )  
{  
    if( user_exists($user[0]) )  
    {  
        $Messages->add'User exists: '.$user[0], 'note' );  
        continue; // skip  
    }  
    add_user$user[0], $user[1], $user[2], $user[3], $user[4], $user[5]);  
 
    $Messages->add'User added: '.$user[0], 'success' );

Jun 28, 2012 07:50

Hey dude , whr to add the code you gave above ??? :O

Jun 29, 2012 17:21

admin.php is the best place, anywhere after
PHP:
/*
* Asynchronous processing options that may be required on any page
*/
require_once $inc_path.'_async.inc.php';

_________________


Form is loading...

powered by b2evolution – This forum is powered by b2evolution CMS, a complete engine for your website.