Creating many Users

tonymontana234

Joined: 2005-08-23
Posts: 13
Posted: Wed, 2005-08-31 07:18

How can I create a large number of users for example out of a .csv or Excel-file?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Wed, 2005-08-31 13:46

you'd have to write your own small script for it to work.

require_once('embed.php');
GalleryEmbed::init(array('fullInit' => true));

// do here the user import stuff =
// 1. read in your list of users, with php file functions
// 2. call GalleryCoreApi::createUser
// i'm just copying some code from GalleryEmbed, you'll have to adjust it. and you'll have to call this for each user

list ($ret, $user) = GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryUser');
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}
	if (!isset($user)) {
	    return GalleryStatus::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__);
	}

	$ret = $user->create($args['username']);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}
if (!empty($args['password'])) {
	    $user->changePassword($args['password']);
	} elseif (isset($args['hashmethod']) && $args['hashmethod'] == 'md5'
		&& !empty($args['hashedpassword'])) {
	    $user->setHashedPassword($args['hashedpassword']);
	} elseif ($create) {
	    /* Create a random password */
	    $user->changePassword('G' . rand(100000,999999) . '2');
	}

	if (isset($args['username'])) {
	    $user->setUserName($args['username']);
	}
	if (isset($args['email'])) {
	    $user->setEmail($args['email']);
	}
	if (isset($args['fullname'])) {
	    $user->setFullName($args['fullname']);
	}
	if (isset($args['language'])) {
	    list ($languageCode) = GalleryTranslator::getSupportedLanguageCode($args['language']);
	    $user->setLanguage($languageCode);
	}
	if (isset($args['creationtimestamp'])) {
	    $user->setCreationTimestamp($args['creationtimestamp']);
	}
$ret = $user->save();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

GalleryEmbed::done();

 
cesc

Joined: 2005-11-16
Posts: 3
Posted: Thu, 2005-11-17 22:01

Hi,

I'm a newbie in php and programming, so I didn't understand exactly where have I to write that code. (http://gallery.menalto.com/node/35493)

I tried to generate a .cvs file which can be read by mySql-g2_User (trough phpMyAdmin), but I don't know how to create the g_hashedPassword. I'm working under Linux.

Thanks,

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2005-11-17 22:07

does the cvs file contain the plaintext password? then you can use that instead of the hashedpassword.

you've have to open the cvs file in php with fopen() and fread and parse line by line and use the values in the createUser call.

obviously you don't know php, but you could use http://php.net/fopen for examples etc.

 
cesc

Joined: 2005-11-16
Posts: 3
Posted: Fri, 2005-11-18 11:38

sorry but I'm not a newbie, I never see php code. I wos trying to put the users directly to the g_Users table, I couldn't. I think that is beter insert all of my 200 users by hand. Thanks for all.

I'm going to see if anybody explain, in the future, how to create many users from .csv or Excel-file, for non php users (end-end-end users).

Instead I think it's a GOOD Project. (sorry, my english could be beter, I know)

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-11-18 13:57

1. you can't put users directly in the g_Users table since other tables in g2 also need a corresponding row
if it was that easy, i'd have suggested it. you should use the G2 API (php) to do it.

yes, the above code wasn't meant for an end to end user solution. you're free to file a feature request.
coding a csv parser around the above code wouldn't take much time though. maybe someone else has the same interest as you...

 
cesc

Joined: 2005-11-16
Posts: 3
Posted: Fri, 2005-11-18 14:40

Thanks valerós!