Using Gallery users/authentication in another app

notgoddess
notgoddess's picture

Joined: 2005-04-03
Posts: 178
Posted: Thu, 2005-07-28 17:54

Forgive me if this is an age-old question, but I've read numerous posts and docs and I guess I'm just missing the right one.

Instead of integrating G2 into another app, I'd like to use Gallery's authentication for the admin page of a chatbox script I've written-not embed it in Gallery as a module (tho it might come to that later) but just use the same authentication-that way I can have one admin user for the site w/o having to re-invent the wheel.

If anyone could point me in the right direction, or better :) provide some lines to pop in my PHP script, I'd be a joyful person.

Ng

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2005-07-28 19:37

no, this question wasn't asked before.

so you'd like to have a login box in your chat, and your php code in the chat authenticates the user against G2.

or do you want to login in G2 and when you login in G2, it notifies your chat script that the user is logged in such that the chat can create a cookie or so?

See modules/core/UserLogin.inc

 
notgoddess
notgoddess's picture

Joined: 2005-04-03
Posts: 178
Posted: Fri, 2005-07-29 01:31

Well, eventually I'd like to tie the user end in (so instead of typing a name your Gallery ID is prepended automagically if you are logged in), but for now, something (I hope) simpler.

In Gallery1, you could include gallery's init file and do $gallery->isAdmin() (not exact wording but I'm w/o the reference now).
By doing that you could verify that the session belonged to a Gallery admin and so let them do, well, adminnish (adminesque ?) actions in your own application.

For now, I want to do something similar-if this person/session is a gallery admin, let them into the chat admin page. Else kick them back to the main page (or be nice and display a message telling them to log into Gallery first, yada yada).

Your file reference looks like it has the calls I'll need to use, but what is the best way to access/tie into Gallery's functions?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-07-29 01:59

so i assume the chat script is in the same directory as g2's main.php? or in a subdirectory.

else, you have a problem, because the browser doesn't send the g2 session cookie to your chat script. if the chat script is in another directory or higher in the directory tree, you can change G2's cookie path in site admin, e.g. to '/'.

in your chat script,
require_once('embed.php');
$ret = GalleryEmbed::init(array('fullInit' => true));
if ($ret->isError()) {
print $ret->getAsHtml();
exit;
}

list ($ret, $isSitedmin) = GalleryCoreApi::isUserInSiteAdminGroup();
if ($ret->isError()) {
print $ret->getAsHtml();
exit;
}

if ( $isSitedmin) {
print "is admin";
} else {
print "is not admin";
}

 
notgoddess
notgoddess's picture

Joined: 2005-04-03
Posts: 178
Posted: Fri, 2005-07-29 05:13

Thank you greatly for your help.
This is what I came up with, in case it might be of use to others, or can be improved.

I did end up changing the cookie path to '/' as the site is also using a blog that I would like eventually to tie into Gallery as well, so there is a common user base, and that would facilitate it should I get to that point.

I put this code in a file in Gallery2's root, then included it in my admin page (again, so I can reuse it as needed). Of course all the print stuff is in the chat page, but here for consistency.

Thanks again,
NotGoddess

<?php
//
// By (10%) NotGoddess, and (90%) Valiant @ Gallery forums
// A somewhat hacked tie-in to easily allow access to Gallery2 user data.
// For use by the ChatBox script.
//

require_once('embed.php');
$ret = GalleryEmbed::init(array('fullInit' => true));
if ($ret->isError()) {
print $ret->getAsHtml();
exit;
}

list ($ret, $isSiteAdmin) = GalleryCoreApi::isUserInSiteAdminGroup();
if ($ret->isError()) {
print $ret->getAsHtml();
exit;
}
$user = $gallery->getActiveUser();
$who = $user->getfullName();

?>
<p>Welcome, <?php echo $who; ?></p>
<?php
if (!$isSiteAdmin) {
?>
<p>In order to access ChatBox Administration, you must first 
<a href="http://mydomain.com/gallery2/main.php?g2_view=core.UserAdmin&g2_subView=core.UserLogin&g2_return=http://mydomain.com/chat/admin.php" title="Login to Gallery">Login</a>
to Gallery as an administrator.</p>
<?php
} else {
?>
   <p>As a ChatBox Administrator, you have the following commands at your disposal [...] yada yada [...] </p>
<?php
}
?>