[Solved] If user is already logged in, redirect to main.php?

asorgius

Joined: 2006-02-13
Posts: 6
Posted: Tue, 2006-02-14 13:26

If I direct a user to the login page from the main page of my website (using gallery2/main.php?g2_view=core.UserAdmin&g2_subView=core.UserLogin), is there any way to just have them automatically redirected to their album of the main.php page if they are already logged in?

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Thu, 2006-02-16 06:30

you can modify that (sub)view to check the current user and redirect.
look for code that uses things like:
$gallery->getActiveUserId()
GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
return array(null, array('redirect' => array('view' => .....

 
asorgius

Joined: 2006-02-13
Posts: 6
Posted: Thu, 2006-02-16 14:43

Ok, I found the code below in login.inc. Do I need to place it somehow into UserLogin.tpl, or someplace else? And if so, do I have to wrap it in <div class=...> tags? Sorry, I'm new at all this.

if ($gallery->getActiveUserId() != $anonymousUserId) {
/* No need to log in */
return array(GalleryStatus::success(),
array('redirect' => array('view' => 'publishxp.SelectAlbum')));
}

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Thu, 2006-02-16 15:44

you can't redirect from a tpl.. at that point it is already building a page for the browser.
you're looking at publishxp/Login.inc.. this is for publishxp, not normal login. but, it does sound like you found some good example code! copy the code there that loads the value for $anonymousUserId and then the code you pasted above.. put that code in modules/core/UserLogin.inc, class UserLoginView, just after "global $gallery" in function loadTemplate. then just change the redirect from 'view' => 'publishxp.SelectAlbum' to 'controller' => 'useralbum.UserAlbum'

 
asorgius

Joined: 2006-02-13
Posts: 6
Posted: Thu, 2006-02-16 17:10

Sweet! Works like a charm. I'll post the entire code change I made here so anyone else with this need can see it easily. Like mindless said add the below section of code to modules/core/UserLogin.inc directly after the "global $gallery".

Thanks mindless!!!!

	list ($ret, $anonymousUserId) =
	    GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	if ($gallery->getActiveUserId() != $anonymousUserId) {
		/* No need to log in */
		return array(GalleryStatus::success(),
		array('redirect' => array( 'controller' => 'useralbum.UserAlbum')));
	}
 
swixxx

Joined: 2005-11-08
Posts: 12
Posted: Mon, 2006-06-19 22:09

Hey, I tried this code with version Gallery 2.1.1 but got an error when excuting. I was wondering if this code is valid for 2.1.1 and if not, are there any quick fixes to port it over?

Thanks,
Shawn

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Tue, 2006-06-20 04:17

if ($ret->isError()) becomes if ($ret)
GalleryStatus::success() becomes null

 
swixxx

Joined: 2005-11-08
Posts: 12
Posted: Tue, 2006-06-20 04:41

Thanks for the help but I got a security error when logging in for the first time, here it is:
========
Security Violation
The action you attempted is not permitted.

Back to the Gallery

Error Detail -
Error (ERROR_BAD_PARAMETER) : D:\inetpub\gallery\modules\core\classes\helpers/../../../../modules/useralbum/module.inc
in modules\core\classes\helpers\GalleryPluginHelper_simple.class at line 108 (gallerycoreapi::error)
in modules\core\classes\helpers\GalleryPluginHelper_simple.class at line 102 (gallerypluginhelper_simple::loadplugin)
in modules\core\classes\GalleryCoreApi.class at line 200 (gallerypluginhelper_simple::loadplugin)
in modules\core\classes\GalleryController.class at line 138 (gallerycoreapi::loadplugin)
in main.php at line 170 (gallerycontroller::loadcontroller)
in main.php at line 87
in main.php at line 80
System Information
Gallery version 2.1.1
PHP version 4.3.9 apache2handler
Webserver Apache/2.0.52 (Win32) PHP/4.3.9
Database mysql 4.1.7-nt
Toolkits ImageMagick
Operating system Windows NT ATHLON 5.1 build 2600
Browser Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
==============

Here is the new LoadTemplate function in the UserLoginView class in UserLogin.Inc, which includes your changes. Were they implemented properly?
==============
function loadTemplate(&$template, &$form) {
global $gallery;

/* Added by Shawn ---------------------------*/
list ($ret, $anonymousUserId) =
GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
if ($ret) {
return array($ret->wrap(__FILE__, __LINE__), null);
}
if ($gallery->getActiveUserId() != $anonymousUserId) {
/* No need to log in */
return array(null,
array('redirect' => array( 'controller' => 'useralbum.UserAlbum')));
}
/* End Added by Shawn -------------------------*/

if ($form['formName'] != 'UserLogin') {
$form['formName'] = 'UserLogin';
$form['username'] = '';

/*
* When logging in we don't have a session yet, thus no navigation history / a place
* to store the returnUrl. Thus store the returnUrl in the login form
*/
$returnUrl = GalleryUtilities::getRequestVariables('return');
if (!empty($returnUrl)) {
$form['returnUrl'] = $returnUrl;
}
}

$template->setVariable('controller', 'core.UserLogin');
return array(null, array('body' => 'modules/core/templates/UserLogin.tpl'));
}