Auto create a user that doesn't exist

cowmoo

Joined: 2007-07-18
Posts: 20
Posted: Fri, 2007-10-12 02:35

Hey all, I've been searching the forums for around a week no with little success. What I'm trying to do seems quite common, although I can't seem to get it working. :(

I'm trying to pass an external user ID (from a 3rd party php application), and have gallery check that ID and log the user in, if it exists.
If it doesn't exist I want G2 to create it then log in.

Here's my code so far... (in a file called main_wrapper.php in the gallery2 root dir)

<?php
session_start();

error_reporting(E_ALL);
$data = runGallery();
$data['title'] = (isset($data['title']) && !empty($data['title'])) ? $data['title'] : 'Gallery';

if (isset($data['bodyHtml'])) {
print <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>{$data['title']}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
{$data['javascript']}
{$data['css']}
</head>

<body>
{$data['bodyHtml']}
</body>
</html>
EOF;
}

function runGallery() {

include('embed.php');
$data = array();

// if anonymous user, set g2 activeUser to ''
$externalUID = '1234';
if (isset($_SESSION['username'])) {
$uid = $_SESSION['username'];

}

// initiate G2 - this call ok?
$ret = GalleryEmbed::init(array('g2Uri' => './',\
				'embedUri' => 'main.php',
				'fullInit' => 'true',
				'activeUserId' => $uid));

if (!$ret) {
	$data['bodyHtml'] = $ret->getAsHtml();
	return $data;
} else {
	print $ret->getAsHtml();
	$ret = GalleryEmbed::isExternalIdMapped($uid, 'GalleryUser');
	print $ret->getAsHtml();

	// Where can I find a list of error codes?
	if ($ret->getErrorCode() == 5) {

		// How do I get the g2 ID of the user created in the line below?
		$ret = GalleryEmbed::createUser($externalUID , array('username' => $uid, 
                                        'email' => 'email@email.com',
					'password' => 'test',
                                        'fullname' => 'test user'));

		print $ret->getAsHtml();

		// Should the $uid in the line below be the g2 id of the user just created in the line above?
		$ret = GalleryEmbed::init(array('g2Uri' => './', 'embedUri' => 'main.php',
					'fullInit' => 'true', 'activeUserId' => $uid));

		print $ret->getAsHtml();
		//$ret = GalleryEmbed::addExternalIdMapEntry($uid, 'test', 'GalleryUser');
		$data['bodyHtml'] = $ret->getAsHtml();
		return;
	} else {
		/* user already exists, login */
	}
}
// finish off
GalleryEmbed::done();

// handle the G2 request
$g2moddata = GalleryEmbed::handleRequest();

// show error message if isDone is not defined
if (!isset($g2moddata['isDone'])) {
$data['bodyHtml'] = 'isDone is not defined, something very bad must have happened.';
return $data;
}
// exit if it was an immediate view / request (G2 already outputted some data)
if ($g2moddata['isDone']) {
exit;
}

// put the body html from G2 into the xaraya template
$data['bodyHtml'] = isset($g2moddata['bodyHtml']) ? $g2moddata['bodyHtml'] : '';

// get the page title, javascript and css links from the <head> html from G2
$title = '';
$javascript = array();
$css = array();

if (isset($g2moddata['headHtml'])) {
list($data['title'], $css, $javascript) = GalleryEmbed::parseHead($g2moddata['headHtml']);
$data['headHtml'] = $g2moddata['headHtml'];
}

/* Add G2 javascript */
if (!empty($javascript)) {
foreach ($javascript as $script) {
$data['javascript'] .= "\n".$script;
}
}

/* Add G2 css */
if (!empty($css)) {
foreach ($css as $style) {
$data['css'] .= "\n".$style;
}
}

// sidebar block
if (isset($g2moddata['sidebarBlocksHtml']) && !empty($g2moddata['sidebarBlocksHtml'])) {
$data['sidebarHtml'] = $g2moddata['sidebarBlocksHtml'];
}

return $data;
}

?>

The output I get from getAsHtml();

Error (ERROR_MISSING_OBJECT) : admin GalleryUser

    * in modules/core/classes/helpers/GalleryEntityHelper_simple.class at line 124 (GalleryCoreApi::error)
    * in modules/core/classes/GalleryCoreApi.class at line 2298 (GalleryEntityHelper_simple::loadEntityByExternalId)
    * in modules/core/classes/GalleryEmbed.class at line 215 (GalleryCoreApi::loadEntityByExternalId)
    * in modules/core/classes/GalleryEmbed.class at line 120 (GalleryEmbed::checkActiveUser)
    * in main_wrapper.php at line 54 (GalleryEmbed::init)
    * in main_wrapper.php at line 20

Error (ERROR_MISSING_OBJECT) : admin GalleryUser

    * in modules/core/classes/GalleryEmbed.class at line 904 (GalleryCoreApi::error)
    * in main_wrapper.php at line 61 (GalleryEmbed::isExternalIdMapped)
    * in main_wrapper.php at line 20

Error (ERROR_COLLISION)

    * in modules/core/classes/GalleryUser.class at line 149 (GalleryCoreApi::error)
    * in modules/core/classes/GalleryEmbed.class at line 344 (GalleryUser::create)
    * in main_wrapper.php at line 68 (GalleryEmbed::createUser)
    * in main_wrapper.php at line 20

Error (ERROR_MISSING_OBJECT) : admin GalleryUser

    * in modules/core/classes/helpers/GalleryEntityHelper_simple.class at line 124 (GalleryCoreApi::error)
    * in modules/core/classes/GalleryCoreApi.class at line 2298 (GalleryEntityHelper_simple::loadEntityByExternalId)
    * in modules/core/classes/GalleryEmbed.class at line 215 (GalleryCoreApi::loadEntityByExternalId)
    * in modules/core/classes/GalleryEmbed.class at line 120 (GalleryEmbed::checkActiveUser)
    * in main_wrapper.php at line 71 (GalleryEmbed::init)
    * in main_wrapper.php at line 20
 
cowmoo

Joined: 2007-07-18
Posts: 20
Posted: Fri, 2007-10-12 08:00

Ok, after alot more reading through the forums, I've managed to get it to create/login users on the fly, still having some problems with CSS display tho, but it's 5 mins till knockoff on a Friday, so that can wait. :D
I've now started again with code I found in this thread http://gallery.menalto.com/node/69886
incase anyone wants it: (not complete yet, but working better than the previous post, will update more on monday)

<?php
// IS WORKING ATM BUT THE CSS ISN'T GETTING IMPORTED
include("embed.php");
$emAppUserId = "123451";
//$ret = GalleryEmbed::init(array('embedUri' => $embedUri, 'g2Uri' => $g2Uri, 'activeUserId' => $emAppUserId));
$ret = GalleryEmbed::init(array('embedUri' => 'http://www.example.com/', 'g2Uri' => './main.php'));
$ret2 = GalleryEmbed::isExternalIdMapped($emAppUserId, 'GalleryUser');
if ($ret2) {
	/* Error! */
	/* Did we get an error because the user doesn't exist in g2 yet? */

	/* if not $ret user is deleted but still in map table */
	if ($ret2 && $ret2->getErrorCode() & ERROR_MISSING_OBJECT) {
		/* The user does not exist in G2 yet. Create in now on-the-fly */
		$ret = GalleryEmbed::createUser($emAppUserId, array('username' => $emAppUserId));

		if ($ret) {
			/* An error during user creation. Not good, print an error or do whatever is appropriate
			* in your emApp when an error occurs */
			print "An error occurred during the on-the-fly user creation <br>";
			print $ret->getAsHtml();
			exit;
		}
	} else {
		/* The error we got wasn't due to a missing user, it was a real error */
		if ($ret2) {
			print "An error occurred while checking if a user already exists<br>";
			print $ret2->getAsHtml();
		}
		print "An error occurred while trying to initialize G2<br>";
		print $ret->getAsHtml();
		exit;
	}
}
/* At this point we know that either the user either existed already before or that it was just created
* proceed with the normal request to G2 */
GalleryEmbed::checkActiveUser($emAppUserId);
$g2moddata = GalleryEmbed::handleRequest();
print $g2moddata['bodyHtml'];
?>

btw, thanks for the code Slayergirl :)

 
Slayergirl
Slayergirl's picture

Joined: 2007-09-12
Posts: 180
Posted: Wed, 2007-10-17 12:54

YW, I found it almost complete in the documentation after some digging around. My editted version worked with usergroups too but I kicked that out of the code in the forum ;)