create a new user in mysql

silvere

Joined: 2004-10-22
Posts: 9
Posted: Thu, 2004-10-28 14:52

Hi!

I'm triing to add a new user directly in mysql :
when I send these SQL requests :
INSERT INTO `g2_User` ( `g_id` , `g_userName` , `g_fullName` , `g_hashedPassword` , `g_email` , `g_language` ) VALUES ('0', 'test', 'this is a test', SHA1( '1234' ) , 'test@est.g', NULL);

INSERT INTO `g2_UserGroupMap` ( `g_userId` , `g_groupId` )
VALUES (
'6', '2'
);

I get an error message when I try to log in :

Error (ERROR_BAD_PARAMETER)

* in modules/core/classes/helpers/GalleryEntityHelper_simple.class at line 51 (gallerystatus::error)
* in modules/core/classes/GalleryCoreApi.class at line 1934 (galleryentityhelper_simple::loadentitiesbyid)
* in modules/core/classes/helpers/GalleryUserHelper_medium.class at line 181 (gallerycoreapi::loadentitiesbyid)
* in modules/core/classes/GalleryCoreApi.class at line 377 (galleryuserhelper_medium::fetchuserbyusername)
* in modules/core/UserLogin.inc at line 51 (gallerycoreapi::fetchuserbyusername)
* in main.php at line 161 (userlogincontroller::handlerequest)
* in main.php at line 47
* in main.php at line 40

Any idea ?

 
baschny
baschny's picture

Joined: 2003-01-04
Posts: 328
Posted: Thu, 2004-10-28 16:03

silvere, you should not manipulate the mysql database directly, but instead use our API (or the UI). See modules/core/AdminCreateUser.inc for an example on how to do it through the API.

The problem in your specific case is that an g_id of "0" is not valid. What exactly are you trying to acomplish?

 
silvere

Joined: 2004-10-22
Posts: 9
Posted: Thu, 2004-10-28 16:13

than you for you answer.

I just want to create PHP form which creates a user which can access gallery BUT ALSO many other applications, all based on mysql.

That's why I cannot use the interface to do that in * one shot *

I've seen the g_id of "0" is incorrect but it doesn't help me....

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Fri, 2004-10-29 07:45

Creating users directly in the database is going to be much more effort than you probably want to put into it. However, we offer a simple external API that you can use to create users via PHP. So in your PHP form handler, you can simply make a call to the G2 code that creates the user for you.

Read docs/EMBEDDING to get an idea of what it is and how it works (the docs are rough, but it's all in there). You're going to want to make a call to GalleryEmbed::createUser() with all the relevant user details.

 
silvere

Joined: 2004-10-22
Posts: 9
Posted: Fri, 2004-10-29 09:01

Thank you, it works quite well!

For information, this is what I wrote :
define('G2_EMBED', 1);
$gallery2_dir= '/var/www/gallery2/';
require_once($gallery2_dir.'embed.php');
GalleryEmbed::init($gallery2_dir.'index.php?module=gallery2','.',$gallery2_dir.'index.php');
$args=array('email' => 'a@a.com', 'fullname' => 'full here','language' => 'en_GB', 'password' => '1234', 'hashedpassword' => md5('1234'), 'hashmethod' => 'MD5');
if (GalleryEmbed::createUser('classtest4',$args)) echo "Gallery user created.";
else echo "Gallery user creation ERROR.";

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2004-10-29 23:49

silvere, three notes:

1. you don't need to give "password" AND "hashedpassword", only one of them.
either give "password" or "hashedpassword" + "hashmethod" (only accepts md5).
but it works too if you give both to G2.

2.

 if (GalleryEmbed::createUser('classtest4',$args)) echo "Gallery user created.";
else echo "Gallery user creation ERROR.";

i don't think that works.
GalleryEmbed::createUser returns not a boolean or an integer, it returns a G2 status object.

Instead, use it like this:

 $ret = GalleryEmbed::createUser('classtest4',$args);
if ($ret->isSuccess()) {
    echo "Gallery user created.";
} else {
    echo "Gallery user creation ERROR.";
}

3. don't you use integer values to identify your users in the other applications uniquely? or why did you set $externalId = 'classtest4' ?
it works, but if 'classtest4' is a username and you allow to change usernames in other applications, you should use something as $externalId that doesn't change.

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sun, 2004-10-31 23:11

4. the define for G2_EMBED isn't needed.. embed.php does that.

 
pisolo

Joined: 2006-11-14
Posts: 7
Posted: Fri, 2006-12-08 21:18

Hi all!
I'm new to Gallery and I come from a not so good experience with Photopost. I used to have Gallery 1.x embedded in e-xoops quite a long ago and I had to find another solution because I didn't like the file structure. Now that Gallery moved to a full mySql implementation I installed it and I have to say that it works fine, even if there are some limitation with my provider PHP settings that there's no way to overcome.
By the way I'm an happy Gallery user with over 30.000 pics in my users albums.
The problem I've got now is that I manually migrated the 200+ accounts I had on Photopost but I'd like to enable all of my e-xoops users to log into Gallery creating accounts from the e-xoops db.
I don't need any integration at all, I want to keep the two thing separated, but inputing by hand another 2000+ users will drive me mad so I got to this post hping to find a solution.
I'm not into PHP too and I just copied silvere's code listed below

Quote:
<?php
define('G2_EMBED', 1);
$gallery2_dir= './m+gallerie/';
require_once($gallery2_dir.'embed.php');
GalleryEmbed::init($gallery2_dir.'index.php?module=gallery2','.',$gallery2_dir.'index.php');
$args=array('email' => 'testuser@mydomain.it', 'fullname' => 'Test User','language' => 'it', 'password' => 'fakepwd', 'hashedpassword' => md5('fakepwd'), 'hashmethod' => 'MD5');
if (GalleryEmbed::createUser('Test User',$args)) echo "Gallery user created.";
else echo "Gallery user creation ERROR.";
?>

When I call the script I get the message "Gallery user created." but I cannot find the user in the DB. So I changed the code according to valiant's suggestion to chech properly the $ret as follows:

Quote:
<?php
define('G2_EMBED', 1);
$gallery2_dir= './m+gallerie/';
require_once($gallery2_dir.'embed.php');
GalleryEmbed::init($gallery2_dir.'index.php?module=gallery2','.',$gallery2_dir.'index.php');
$args=array('email' => 'testuser@mydomain.it', 'fullname' => 'Test User','language' => 'it', 'password' => 'fakepwd', 'hashedpassword' => md5('fakepwd'), 'hashmethod' => 'MD5');
$ret = GalleryEmbed::createUser('classtest4',$args);
if ($ret->isSuccess()) {
echo "Gallery user created.";
} else {
echo "Gallery user creation ERROR.";
}
?>

The user is not created and I get this error: "Fatal error: Call to undefined function: issuccess() in /web/htdocs/www.modellismopiu.net/home/prova1.php on line 10".
Can anybody help?
Thanks in advance for your time and kindness
Pisolo

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2006-12-08 23:58

even if you don't want a permanent xoops integration, i suggest you use the existing xoops integration to import the xoops users into g2.
i have never looked at that integration, but some other integrations have user import/export code in their activation.
what i did in the xaraya integration is: import all xaraya users into gallery, and import all gallery users into xaraya. so at the end of this process, both applications have the same users.

your example code above is pretty old.
it won't work in gallery 2.1 and it won't store the changes permanentely.

to store the changes, you need to add $ret = GalleryEmbed::done(); at the end of your script.
and
if ($ret->isSuccess()) is no longer valid.

it should be:
if ($ret) {
echo "Gallery user creation ERROR.";
print $ret->getAsHtml(); exit;
} else {
echo "user successfully created";
}

and the arguments to GalleryEmbed::init are wrong as well. etc.

i really suggest you use an existing solution instead of coding php, which you don't seem to like anyway. :)

 
pisolo

Joined: 2006-11-14
Posts: 7
Posted: Sat, 2006-12-09 00:38

Valiant thanks a lot for your answer. Unfortunately I've got my cms on one server and Gallery on another one so I cannot integrate it fully.
I think that I have to find a way to cope with PHP coding. I had a look into your xaraya integration but I didn't find the bit of code I was looking for. Could you please tell me which part of the code imports the users into gallery?
Thanka a lot again
Pisolo

 
austinnoronha

Joined: 2007-10-09
Posts: 3
Posted: Wed, 2007-10-24 13:54

Thanks Guys
It was a great help , i too was creating a new user from MYSQL Query but after reading this i have solved my tensions...
My code is almost the same , here it is , but i still don't understand why we have to give an external id or the first arg i.e. user12,
could you help me out with it ,or is it something that i have to define....?



/***********************************************/

GalleryEmbed::init(array('fullInit' => true),
			$gallery2_dir.'index.php?module=gallery2',
			'.',
			$gallery2_dir.'index.php');

$args=array('email' => 'aol@aol.com',
			'fullname' => 'a_nasty',
			'password' => '1234', 
			'username' => 'fake2'
			);

GalleryEmbed::createUser('user12',$args);
$ret = GalleryEmbed::done();
if ($ret) {
echo "Gallery user creation ERROR.";
print $ret->getAsHtml(); exit;
} else {
echo "user successfully created";
}


/***********************************************/

 
Slayergirl
Slayergirl's picture

Joined: 2007-09-12
Posts: 180
Posted: Fri, 2007-10-26 11:23

The first is the External User ID. This comes from your application (your forum/cms etc.) You just have to get the external user ID to Gallery. This is then aved in the externalmapped with a link which Gallery user it is. The username you give up later (fake2) is the one saved in the User table of Gallery2 (if you have login enabled this is the username they use in Gallery). These 2 names CAN be the same!

I am not so good in explaining. Hope you get it a little bit. A example. You have a Joomla CMS.

Embed makes a entry in a special table that says
user12 (Joomla username) = fake2 (Gallery username)

The user could use fake2 to log into Gallery without Joomla. The user12 username will not work -> the normal user table does not know this name.

btw there is a little error in your script
GalleryEmbed::createUser('user12',$args);
$ret = GalleryEmbed::done();
if ($ret) {
echo "Gallery user creation ERROR.";
print $ret->getAsHtml(); exit;
} else {
echo "user successfully created";
}

This never gives an error EVEN when the user is not created in the g2_user table :( createUSer probably needs a $ret too to catch an error.

 
Markus83Muc

Joined: 2006-09-16
Posts: 2
Posted: Wed, 2008-02-27 14:19

Hello,
hopefully you can help me, because I'm not very familiar with php. I'm having a simmelar problem. I tried to add users from an external user database.
I made a test script and hoped that after I open it a new user will appear in the g2_User table of the gallery2 DB, but there must be some mistake in my script.
The Gallery2 dir is one folder above my test folder.

Quote:
<HTML>
<HEAD>
<TITLE>Gallery2 Test</TITLE>
</HEAD>
<BODY>
<?php
function insertUserGallery2($user, $email, $pass, $vorname, $nachname){
$fullname = '$vorname $nachname';
$gallery2_dir = '../gallery2/';

GalleryEmbed::init(array('fullInit' => true),
$gallery2_dir.'index.php?module=gallery2',
'.',
$gallery2_dir.'index.php');

$args=array('email' => '$email',
'fullname' => '$fullname',
'password' => '$pass',
'username' => '$user'
);

GalleryEmbed::createUser($user ,$args); //Username user12 anpassen
$ret = GalleryEmbed::done();
if ($ret) {
echo "Gallery user creation ERROR.";
print $ret->getAsHtml(); exit;
} else {
echo "user successfully created";
}
}
insertUserGallery2('test2', 'test@test.de', 'testpassword', 'FirstName', 'Name');
?>
</BODY>
</HTML>

 
Markus83Muc

Joined: 2006-09-16
Posts: 2
Posted: Tue, 2008-03-18 11:19

I solved my problem, I missed

Quote:
require_once($gallery2_dir.'embed.php');

before GalleryEmbed:init(.....