Recent Topics

1 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?

2 Jun 06, 2012 15:23

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] );
}

3 Jun 06, 2012 15:26

You can add as many user params as you want

$User->set( param, value );


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

4 Jun 06, 2012 16:02

where does that code go then?

5 Jun 06, 2012 17:17

admin.php is the best place, anywhere after


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

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

7 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

8 Jun 07, 2012 10:23

Well putting that into admin.php caused this error:

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?

9 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

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' );
}

10 Jun 07, 2012 20:04

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

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

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

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' ); 
} 

13 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


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' );  
}

14 Jun 08, 2012 15:29

Hey, I cracked it !!!!!

OK so I'm talking to myself

added grp_ID to this line:

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

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

should be this

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

added the number 4 for user group 4

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

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

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

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

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' );  

16 Jun 28, 2012 07:50

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

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