Remove the "logout" but *KEEP* the account *OR* is the a LogOutRedirect option?

woozy

Joined: 2010-11-13
Posts: 32
Posted: Sat, 2010-11-27 02:42

I have embedded Gallery 2.3 into my Web site so that logging in is handled by my website procedure. This is geat and fantastic.

So, as the documentation points out, the line $gallery->setConfig('login', true) in the embedding file displays the login/logout and and account info links and leaving the line out does not display it.

My website doesn't monitor events so to keep synchronization between my Web site accounts and my G2 accounts is done on the fly. My methodology goes like this: If a Web site member goes to my gallery, G2 looks to see if there's an entry in the ExternalIdMap table, if there is log the guy into G2; if there isn't, create an embedded G2 account based on the web account info and log the guy into. It's simple, maybe a tad flimsy, but it works just fine.

*HOWEVER* if at some time later some account info is changed on my website (say the member changes the spelling of his name or or, more likely, his e-mail address) this information will not be recorded in G2.

Well, I *could* write a few lines in my web-site profile update page to update G2 at the same time and that wouldn't be hard but .... I don't want to. (I don't want to because, I want to keep my embedded programming clean so that the only places I have to worry about G2 {or any other embedded application} is in the G2 {or any embedded application} itself.) Now the G2 user can modify his by clicking on the "account" link. But I TURNED THAT OFF WHEN I TURNED OFF THE LOGIN LINK.

Q1)So is there a way to get the "account" link back without getting the login link back?

While I'm asking, I might as well ask if there are ways to configure what a user can reset?

Q2) Since embedding doesn't use the G2 password can I modify so the user can not reset the G2 password? Can I make is so that modifying his email address doesn't need a password?

As I write this I realize that I could simply not include a login link for my Web Site, leave the $gallery->setConfig('login', true) in the embedding file, and have a 'loginRedirect'=>{web login page} in the GalleryEmbed::init() call. Then the G2 login link will log the user into my Web site, good, the 'account' link will allow the user to modify his account, good, and the 'logout' link will log the G2 account out while leaving him logged into the Web Site so G2 will embed and log him right back in again, !BAD!...

Q3)Is there a 'logoutRedirect' value I can set?

Hmmmm.

 
woozy

Joined: 2010-11-13
Posts: 32
Posted: Sat, 2010-11-27 03:04

Know what?

Since it's only the user's full name and email address that I care about, it wouldn't be too unweildy to write a clause into the embedding page that will check these each time and update these two values if nescessary.

So never mind.

HOWEVER, if anyone wants to give me hints and pointers on how to write that ($user = $gallery->getActiveUser() gives me the active user but what class is the active user in? What methods can I call? How do I set his full name and email?) I'd be grateful but I'm going to look in the DOCs and see if I can figure it out.

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Sat, 2010-11-27 03:10

having the $user already gives your all the user's info print_r($user) should show you all the user info.
look to modules/core/classes/GalleryUser.class

-s
FlashYourWeb and Your Gallery with The E2 XML Media Player for Gallery2

 
woozy

Joined: 2010-11-13
Posts: 32
Posted: Sat, 2010-11-27 04:20

Thank you, I hope. I figured pretty quickly that I wanted to do something like:

$user = $gallery->getActiveUser();
if ($user->fullName != $member['first']." ".$member['last'])
$user->setFullName($member['first']." ".$member['last']);
if ($user->email != $member['email'])
$user->setEmail($member['email']);

But immediately I discovered that for some reason

$user = $gallery->getActiveUserId(); $user == 31 (which is as I'd expect)

BUT

$user = $gallery->getActiveUser(); $user = '' ???HUH????

So are you saying "$user" is a pre-defined variable (as $gallery seems to be)?

If not, do you have any idea why $gallery->getActiveUser() returns a null????

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Sat, 2010-11-27 04:23

$user = $gallery->getActiveUser();
should at least return the guest user if you are not logged in.

-s
FlashYourWeb and Your Gallery with The E2 XML Media Player for Gallery2

 
woozy

Joined: 2010-11-13
Posts: 32
Posted: Sat, 2010-11-27 05:09

Here's my code. Any comment will be appreciated:

function runGallery($member) {
    // $member an associative array, represents my web site user information
    // i.e. $member['id'] = my web site user's id, $member['first'] - my web site user's first name, etc. etc.
    // $member = null, if no-one is logged into my web site 

    require_once('./gallery2/embed.php');
    $data = array();
    $uid = ($member) ? $member['id'] : ''; 
       //so you get it, right?  $uid is the external idea or empty string depending on whether my web site user is logged in 
	
    $init_array = array('g2Uri' => '/photos/gallery2/', 
				    'embedUri' => '/photos/main.php',
				    'activeUserId' => $uid);
					'loginRedirect' => "/login/LogIn.php");
//obviously this is what I'm going to use to initialize the embedding

	if ($_SESSION['gallery']){
		$init_array['gallerySessionId'] = $_SESSION['gallery'];
	}
//Okay, that bit is probably crap, but I tossed it in because I was having trouble with expiring sessions.
//It's probably not doing any harm or any good, but... well I figured I should paste it in just in case it's doing something weird
 
    // initiate G2 
    $ret = GalleryEmbed::init($init_array);

    if ($ret) {
     	/* Error! */
     	/* Did we get an error because the user doesn't exist in g2 yet? */
     	$ret2 = GalleryEmbed::isExternalIdMapped($uid, 'GalleryUser');
     	if ($uid && $ret2 && $ret2->getErrorCode() & ERROR_MISSING_OBJECT) {
         	/* The user does not exist in G2 yet. Create in now on-the-fly */
         	$ret = GalleryEmbed::createUser($uid, array('username' => $member['username'],
                                                         'password' => $member['password'],
								'fullname' => $member['first']." ".$member['last'],
								'email' => $member['email']));

//The creating a user works just fine.  Cut and pasted it directly from the Docs.
			if(! $ret)
				$ret = GalleryEmbed::init($init_array);

     	}
   }
   if ($ret){	
      $data['bodyHtml'] = $ret->getAsHtml();
      return $data;
    }
//return errors: not relevant

Okay, at this point we've initialize the G2 and set the active user id.
But is it possible that we've only set the id and *not* created the user?????

The next part of my code deals with checking whether my web user is in the proper groups. (On my web site a logged in member has 'status' that is one of the four terms-- 'admin', 'assoc member', 'member', or 'guest'-- perhaps 'guest' and 'member' were a poor choice of words as they mean completely different things in other context so keep in mind that $member['status'] == 'guest' just means that the logged-in *and* registered user of my web site has a characteristic called g-u-e-s-t. I want to make sure that if my web user is a g-u-e-s-t then as a G2 user he is put in a G2 group called 'visitors' but otherwise he is put in a group called 'full'. Again, perhaps not the best choice of terms.)

(With me so far?)

	
	if ($member){
		// Cookieless browsing.  I shouldn't have to use this but I got some 'sessionid expired' buggies
		$gallerySessionId = GalleryEmbed::getSessionId(); 
		$_SESSION['gallery'] = $gallerySessionId;
		
		//Rio Piedras Members go to group "full", logged-in Rio Piedras "guests" go to group "visitors"
		if ($member['status'] == 'guest')
			$g_name = "visitors";
		else
			$g_name ="full";
		$g = GalleryCoreApi::fetchGroupByGroupName($g_name);
		$g_id = $g[1]->getId();
		$u_id = $gallery->getActiveUserId();
		$in_group = GalleryCoreApi::isUserInGroup($u_id, $g_id);
		if (! $in_group[1])
			$ret = GalleryCoreApi::addUserToGroup($u_id, $g_id);

and *that* part of the code works just fine! (Which actually surprised me!!!)

But here's my current perplexity:

			
		//check full name and e-mail
		$user = $gallery->getActiveUser();

//  $user == null!!!  ???HUH???
//		$user = $gallery->activeUser;

//  $user is *still* null;

		if ($user->fullName != $member['first']." ".$member['last'])
			$user->setFullName($member['first']." ".$member['last']);
		if ($user->email != $member['email'])
			$user->setEmail($member['email']);
//That part doesn't run but with some tweaking I've figured out is is simply because $user is null		
		
	}else{
             //if no-one is logged on to my web-site, unset $_SESSION['gallery'], this should just be crap...
		unset($_SESSION['gallery']);
	}


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


    .... rest of code not particularly relevant...
}

So... I'm trying to get the active user *before* I send the request. Is *that* the problem? I'm doing a bunch of crap with session ids. Is *that* the problem?

Or is it... I dunno ....

 
woozy

Joined: 2010-11-13
Posts: 32
Posted: Sat, 2010-11-27 05:26

NOW I GET IT!

Yes, active user isn't set until *after* the handle call (duh). Somehow I thought if I put my code after that handle call, it would go into effect untill after the page loaded. But I guess that's what the 'idDone' property is for, isn't it.

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Sat, 2010-11-27 05:37

Yes, GalleryEmbed::done(); commits any GalleryEmbed methods.
this is my usual init:

function init($full = false) {
    require_once ('embed.php');
    $ret = GalleryEmbed::init(array('fullInit'=>$full, 'embedUri'=>'/media/index.php', 'g2Uri'=>'/gallery2/'));
    if ($ret) {
        print 'GalleryEmbed::init failed, here is the error message: '.$ret->getAsHtml();
        exit;
    }
    GalleryEmbed::done();
}

First run we just get gallery running $full=false
Then after we figure out our user we re-init $full=true

-s
FlashYourWeb and Your Gallery with The E2 XML Media Player for Gallery2

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Sat, 2010-11-27 05:39

to check your user mapping:

$ret = GalleryEmbed::isExternalIdMapped($uid, 'GalleryUser');
if ($ret && ($ret->getErrorCode() & ERROR_MISSING_OBJECT)) {
    //map our new user
}

-s
FlashYourWeb and Your Gallery with The E2 XML Media Player for Gallery2

 
woozy

Joined: 2010-11-13
Posts: 32
Posted: Sat, 2010-11-27 06:04

Now I'm having trouble that

$user->setFullName($member['first']." ".$member['last']);

Doesn't seem to set the user's full name in the database. !!Argh!!

Because $gallery->activeUser is only the current instance of the user and not the permanent record of the user????

function runGallery($member) {

   require_once('./gallery2/embed.php');
   $data = array();
	
   $uid = ($member) ? $member['id'] : '';
   $init_array = array('g2Uri' => '/photos/gallery2/', 
				    'embedUri' => '/photos/main.php',
				    'activeUserId' => $uid,
					'loginRedirect' => "/login/LogIn.php");
 
    // initiate G2 
    $ret = GalleryEmbed::init($init_array);

    if ($ret) {
     	/* Error! */
     	/* Did we get an error because the user doesn't exist in g2 yet? */
     	$ret2 = GalleryEmbed::isExternalIdMapped($uid, 'GalleryUser');
     	if ($uid && $ret2 && $ret2->getErrorCode() & ERROR_MISSING_OBJECT) {
         	/* The user does not exist in G2 yet. Create in now on-the-fly */
         	$ret = GalleryEmbed::createUser($uid, array('username' => $member['username'],
                                                         'password' => $member['password'],
								'fullname' => $member['first']." ".$member['last'],
								'email' => $member['email']));
			if(! $ret)
				$ret = GalleryEmbed::init($init_array);
     	}
     }
     if ($ret){	
      $data['bodyHtml'] = $ret->getAsHtml();
      return $data;
     }

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

    //update user if nescessary
    if ($member){
		$user = $gallery->getActiveUser();
              /*  NOW $user is the active user */
              /*  $user->fullName == "Sam Good" */
              /*  BUT it should be "Mike Bad"   */
              /*  because $member['first'] = "Mike" and $member['last']


		if ($user->getFullName != $member['first']." ".$member['last']){
			$user->setFullName($member['first']." ".$member['last']);
		}
		if ($user->email != $member['email'])
			$user->setEmail($member['email']);		
    }

    // 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'] : '';

    ....
    
    return $data;
} 
 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Sat, 2010-11-27 06:45
 
woozy

Joined: 2010-11-13
Posts: 32
Posted: Sat, 2010-11-27 07:09

Okay. I think I know whats going on.

I need to use GalleryEmbed::updateUser($uid, array('full_name'=>$real_full_name, 'email'=>$real_name));

*and* I need to do it *before* I handle the request.

*but* I don't know how to get the user's fullname, email, or anything from either the external or internal id, when the user isn't *yet* the active user.

So after much headache I've got this code that works!!!!!

Here it is (minus error handling):

function runGallery($member) {
    require_once('./gallery2/embed.php');
    $data = array();
    $uid = ($member) ? $member['id'] : '';
    $init_array = array('g2Uri' => '/photos/gallery2/', 
			'embedUri' => '/photos/main.php',
			'activeUserId' => $uid,
			'loginRedirect' => "/login/LogIn.php");

    $ret = GalleryEmbed::init($init_array);
    if ($member){
	//update user
	//since I don't know how to get user->fullName 
      //from neither the external or internal id, 
      //I'll *always* update

      GalleryEmbed::updateUser($uid, 
         array('fullname' => $member['full_name'],
	  'email' => $member['email']));

    }

   $g2moddata = GalleryEmbed::handleRequest();
   $data['bodyHtml'] = isset($g2moddata['bodyHtml']) ? $g2moddata['bodyHtml'] : '';
   return $data;
} 


The only thing is I really *don't* like requesting user update *every* time *any* user views *any* page just because I don't know how to check what a user's info is.

So, how do I get user info from an id either external or internal when the user is not the active user yet?

Oh, and thanks for your help. It's been a learning experience.

 
woozy

Joined: 2010-11-13
Posts: 32
Posted: Sat, 2010-11-27 07:13
suprsidr wrote:
Did you call GalleryEmbed::done(); after?

-s
FlashYourWeb and Your Gallery with The E2 XML Media Player for Gallery2

No. Where should I do that?