Passing logged in user to G2

rswyatt

Joined: 2005-08-10
Posts: 5
Posted: Thu, 2005-08-11 02:28

Alright - I've read EMBEDDING and read a couple of posts on this - but I'm not getting it (and they pay me to live/eat PHP all day :P )

Anyhow - I found the following:

Quote:
the activeUserId parameter in the GalleryEmbed::init() call is not the G2 user id.
It's the id of the user in your CMS.

you have to map the $cmsUserId to the $g2User->getId() with GalleryEmbed::addExternalIdMap() for existing users.

For new G2 users, you can create the G2 user and map it to the cms userId in a single step with GalleryEmbed::createUser();

But I'm not following...

Here is what I'm doing. I have a single-sign-on Intranet that is authenticating via Kerberos and I'm doing some security checking with LDAP queries... That's minutae...

The part I'm having problems with is that I'm attempting to force a login as the admin user when I myself am logged in...

My code says:

 
// if anonymous user, set g2 activeUser to ''
    if($_SESSION['userinfo']['samaccountname'] == 'rwyatt'){
       
this is where I'm having the problem...  What goes here.  If I am logged in (I verified that it is recognizing that I'm logged in) how can I then make Gallery2 display the admin options, etc... ?  What would $uid = ?  The admin g_id is 6 for my particular install...  I've tried GalleryEmbed::addExternalIdMap('admin') and a whole host of other things... addExternalIdMap always gives me a "call to non-member function" - so I don't know what's up with that...

        }
        else $uid = '';

    // initiate G2
    $ret = GalleryEmbed::init(array('embedUri' => 'index.php?id=167',
                                        'embedPath' => '/staff/',
                                    'relativeG2Path' => '../modules/gallery2',
                                    'loginRedirect' => 'index.php?id=167',
                                    'activeUserId' => $uid));
    if (!$ret->isSuccess()) {
      $data['bodyHtml'] = $ret->getAsHtml();
      return $data;
    }

Based on the above... Any help or do you need more code?

EXTREME appreciation for your help in advance of receiving it! :-)[/b]

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2005-08-11 07:53

A user has two IDs. The ID you use for users in your application (externalId) and the ID we use for users in G2 (entityId). They are not the same. But we map those IDs in a table, such that we know which G2 user corresponds to which user in your application.

The activeUserId is the externalId of the current user.

What's the unique identifier in your single sign on solution / user management? Does each user have an id, name, user data or just name + user data?
You should use the unique identifier as activeUserId. If there's no id, maybe just use the username, if the username is unique.

But...

Before you can call GalleryEmbed::init(array('activeUSerId' => 15)); // assume 15 is the id of the admin user in your application.

you have to map your admin user with G2's admin user, else G2 doesn't know which user to load when you tell it "activeUserId = 15".

So, before you use G2, you'll have to call GalleryEmbed::addExternalIdMapEntry($externalId, $entityId, 'GalleryUser');
$entityId is in this case the id of the admin user in G2. There's not just one admin user in G2 and no super admin. so i recommend mapping the first admin user the db returns.
// how to get the g2 id of the admin user
ist ($ret, $groupId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
if ($ret->isError()) {
print $ret->getAsHtml;
exit;
}
list ($ret, $users) = fetchUsersForGroup($groupId, 1);
if ($ret->isError()) {
print $ret->getAsHtml;
exit;
}
list ($adminUserEntityId, $adminUserObject) = each($users);

 
rswyatt

Joined: 2005-08-10
Posts: 5
Posted: Thu, 2005-08-11 12:48

valiant - thanks for the fast reply and indeed - I'm getting closer..

I'm going to post the whole function just to make sure I'm not missing something else...

function runGallery() {

    require_once('../modules/gallery2/embed.php');
    
	$data = array();
	
 
    // initiate G2 
    $ret = GalleryEmbed::init(array('embedUri' => 'index.php?id=167',
					'embedPath' => '/staff/',
				    'relativeG2Path' => '../modules/gallery2',
				    'loginRedirect' => 'index.php?id=167',
				    'activeUserId' => $uid));
	   // if anonymous user, set g2 activeUser to ''
    if($_SESSION['userinfo']['samaccountname'] == 'rwyatt'){
		list ($ret, $groupId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); 
		if ($ret->isError()) { 
			print $ret->getAsHtml; 
			exit; 
		} 
		list ($ret, $users) = GalleryCoreApi::fetchUsersForGroup($groupId, 1); 
		if ($ret->isError()) { 
			print $ret->getAsHtml; 
			exit; 
		} 
		$uid = list ($adminUserEntityId, $adminUserObject) = each($users); 
		$entityId = $uid[0];
		GalleryEmbed::addExternalIdMapEntry('rwyatt', $entityId, 'GalleryUser'); 
		$uid = 'rwyatt';
	}  
	else  { $uid = '';
 	}
	
	$ret = GalleryEmbed::init(array('embedUri' => 'index.php?id=167',
				'embedPath' => '/staff/',
				'relativeG2Path' => '../modules/gallery2',
				'loginRedirect' => 'index.php?id=167',
				'activeUserId' => $uid));
	
    if (!$ret->isSuccess()) {
      $data['bodyHtml'] = $ret->getAsHtml();
      return $data;
    }

    // user interface: you could disable sidebar in G2 and get it as separate HTML to put it into a block
     GalleryCapabilities::set('showSidebar', true);
	 GalleryCapabilities::set('login',true);
    // 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['sidebarHtml']) && !empty($g2moddata['sidebarHtml'])) {
      $data['sidebarHtml'] = $g2moddata['sidebarHtml'];
    }
    
    return $data;

I'm not sure if this is right - but before I could call any of the functions I had to do an initial init - and then do it again after I got the UID?

Anyhow - something still isn't right...

Things that are right: the database table g2_ExternalMapID gives me the following:

rwyatt GalleryUser 6

rwyatt is my externalId and 6 happens to be the first uid returned from what I pasted from you in the above.

Now - when I run the script I get:

Error
Error (ERROR_PLATFORM_FAILURE) : 
in modules/core/classes/GalleryTemplate.class at line 270 (gallerystatus::error) 
in modules/core/classes/GalleryTemplate.class at line 200 (gallerytemplate::_initcompiledtemplatedir) 
in main.php at line 312 (gallerytemplate::fetch) 
in main.php at line 90
in modules/core/classes/GalleryEmbed.class at line 155
in /home/httpd/vhosts/home.bsbdesign.com/httpdocs/staff/index.php at line 110 (galleryembed::handlerequest) 
in /home/httpd/vhosts/home.bsbdesign.com/httpdocs/staff/index.php at line 6

Any thoughts on where I went wrong?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2005-08-11 12:55

several things.

1. you call GallerEmbed::init before $uid is defined. do your ldap authentication before the init() call, set $uid accordingly and then call ::init
2. you don't have to add the externalid map entry on each request. this is like an account creation, just do it once. it creates a a database table row, G2 will know your mappings once you have called addexternalidmap.
3. call init only once per HTTP request

 
rswyatt

Joined: 2005-08-10
Posts: 5
Posted: Thu, 2005-08-11 12:59

Excellent - works great... Whew! Thanks!

 
khamilton

Joined: 2005-08-21
Posts: 5
Posted: Thu, 2005-09-15 14:46

thanks a lot for this post
it was pretty much the last major item to get my embed finished

huge thanks to teh gallery crew - its the best image app out there in my opinion

these forums are a great resource as well

 
khamilton

Joined: 2005-08-21
Posts: 5
Posted: Thu, 2005-09-15 14:47

thanks a lot for this post
it was pretty much the last major item to get my embed finished

huge thanks to teh gallery crew - its the best image app out there in my opinion

these forums are a great resource as well

 
TheCowboy

Joined: 2004-10-19
Posts: 1
Posted: Thu, 2005-09-22 05:39

I don't know what the problem is, but when I do this, I always seem to get the error below, or another error,
saying pretty much the same thing. Whats going on with gallerycoreapi?

Fatal error: Undefined class name 'gallerycoreapi' in ..../modules/core/classes/GalleryEmbed.class on line 781

----- FIXED -------

For anyone else reading this, my problem was, I didn't have the init as the very first thing.

As soon as I did that (and a whole lot more things). It worked fine