User Specific Login Redirect

Your code modifications and patches you want to share with others.
User avatar
barnett
Joomla! Intern
Joomla! Intern
Posts: 76
Joined: Fri Aug 19, 2005 12:52 pm
Location: Nyack, NY

User Specific Login Redirect

Post by barnett » Wed Dec 20, 2006 4:59 pm

I wanted the ability to redirect specific users to a section or page of my site when they logged in.  I created this simple hack so that the administrator can add a specific Login and Logout Rediect for each user in the User Manager.  If no redirect is defined for the user then the login/logout checks to see if you've defined a rediect in the module.  This simple hack works like a charm on Joomla 1.0.11 with OpenSEF installled.  I've not tested it in conjunction with community builder.

Here is the Hack:

First Backup Your Files!

Step 1)

open file: /administrator/components/com_users/users.xml

after line 13 where you see this:

Code: Select all

<param name="editor" type="editor_list" default="" label="User Editor" description="WYSYWYG Editor for this User" />
add this:

Code: Select all

<param name="login" type="text" default="" label="Login Redirection URL" description="What page will the login redirect to after user login, if let blank will load module default" />
<param name="logout" type="text" default="" label="Logout Redirection URL" description="What page will the login redirect to after user logout, if let blank will load module default" />
Save the file.


Step 2)

open file index.php

around line #139 replace:

Code: Select all

$mainframe->login();
with this:

Code: Select all

        $mainframe->login();

        // Per User Redirection
        $temp_my = $mainframe->getUser();
        $params = new mosParameters( $temp_my->params );
        $userRedirect = $params->get( 'login', '' );
        if ($userRedirect) {
        	$return = sefRelToAbs( $userRedirect );
        }
        $temp_my = '';

around line #168 replace this:

Code: Select all

$mainframe->logout();
with this:

Code: Select all

         // Per User Redirection
        $temp_my = $mainframe->getUser();
        $params = new mosParameters( $temp_my->params );
        $userRedirect = $params->get( 'logout', '' );
        if ($userRedirect) {
	     $return = sefRelToAbs( $userRedirect );
        }
        $temp_my = '';

        $mainframe->logout();

Save the File.
And you're done!

Notes:
Now you can login to the Administrator end and set the individual redirects in the user manager.

Warning:
If you don't want the frontend users to be able to edit these redirects then you need to set the Frontend User Params: to NO in your Global Configuration.

Enjoy
-barnett
http://www.contemplatedesign.com/ - Joomla Development & Design

sa247
Joomla! Explorer
Joomla! Explorer
Posts: 271
Joined: Sat Sep 16, 2006 8:50 pm
Location: San Antonio, Texas
Contact:

Re: User Specific Login Redirect

Post by sa247 » Mon Feb 19, 2007 9:31 pm

just what i was looking for...

works like a charm thanks!  :)

just on another note any ideas how i would get this to work with CB community Builders Login?? Thanks again! :)
Last edited by sa247 on Mon Feb 19, 2007 9:43 pm, edited 1 time in total.

Klaas Koopman
Joomla! Intern
Joomla! Intern
Posts: 92
Joined: Sun Aug 28, 2005 11:27 pm

Re: User Specific Login Redirect

Post by Klaas Koopman » Wed Mar 21, 2007 2:38 pm

I also would like to know how to do this with the community builder login!

Cause what I would like to do is to have them automatically go to their profile when they login.

If anyone knows how to do this,  please let me know!


Thanks AHEAD!
Last edited by Klaas Koopman on Wed Mar 21, 2007 2:41 pm, edited 1 time in total.

mdmen
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Fri Dec 22, 2006 3:04 pm

Re: User Specific Login Redirect

Post by mdmen » Wed Mar 28, 2007 11:02 am

Check out the parameters for the log-in module - there is a place to specify a redirect on log-in or log-out.

mdmen
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Fri Dec 22, 2006 3:04 pm

Re: User Specific Login Redirect

Post by mdmen » Thu Sep 27, 2007 11:49 pm

This used to work like a charm. Now that I've upgraded to 1.0.13 it no longer functions.

Do you have an update to this?

=============RESOLVED===============================

My bad  :-[ - it works just fine, I was looking in the wrong place!
Last edited by mdmen on Sun Sep 30, 2007 11:41 am, edited 1 time in total.

geefin
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Sat Nov 10, 2007 7:26 pm

Re: User Specific Login Redirect

Post by geefin » Sun Nov 11, 2007 10:58 am

Excellent hack, I had a client who didn't understand the urls for Joomla so hacked a little more to get them a drop down list to select each page from. This was accomplished by changing the

type="text"

to

type="page_list" in the xml file.

adding in a method/function to the user.class.php -

function _form_page_list( $name, $value, &$node, $control_name ) {
global $database;

// compile list of the pages
$query = "SELECT id AS value, title AS text FROM jos_content"
;
$database->setQuery( $query );
$editors = $database->loadObjectList();

array_unshift( $editors, mosHTML::makeOption( '', '- Select Page -' ) );

return mosHTML::selectList( $editors, ''. $control_name .'['. $name .']', 'class="inputbox"', 'value', 'text', $value );
}

and then changing the redirects to

$return = sefRelToAbs( "index.php?option=com_content&task=view&id=".$userRedirect."&Itemid=".$userRedirect );

Sorted it for them. It's limited in that you can't pick menus, however, for their site it was ideal. I've documented the hack here -

http://www.econcepts.co.uk/joomla-speci ... c-user.php

ivan8r
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Feb 22, 2006 6:41 pm

Re: User Specific Login Redirect

Post by ivan8r » Tue Nov 20, 2007 1:56 am

I tried to modify this hack to allow selection of menu items, and I'm hung up on a small technicality. The problem is in the $url variable. When I comment out ."&Itemid=".$page (leaving only $redirect;) it "almost" works, but in order to load the right page, it needs the Itemid.

Code: Select all

function _form_page_list( $name, $value, &$node, $control_name ) {
global $database;
// compile list of the pages
$query = "SELECT link AS value, name AS text, id AS page FROM quiz_menu WHERE published=1 AND type='wrapper'";
$database->setQuery( $query );
$redirect = $database->loadObjectList();
array_unshift( $redirect, mosHTML::makeOption( '', '- Select Page -' ) );
$url = $redirect."&Itemid=".$page;
return mosHTML::selectList( $url, ''. $control_name .'['. $name .']', 'class="inputbox"', 'value', 'text', $value );
}
In index.php, I adjusted line 136 to say

Code: Select all

$return = sefRelToAbs( $userRedirect );
I'm sure my problem has to do with my lack of knowledge about mosHTML:selectList. Can anybody think of a way to make this work?

ivan8r
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Feb 22, 2006 6:41 pm

Re: User Specific Login Redirect

Post by ivan8r » Wed Nov 21, 2007 11:40 pm

Yeah! Got it working.

Changed the query a little bit

Code: Select all

$query = "SELECT id AS value, name AS text FROM quiz_menu WHERE published=1 AND type='wrapper'";
and changed index.php a little bit

Code: Select all

$return = sefRelToAbs( "index.php?option=com_wrapper&Itemid=".$userRedirect );
And now it's doing what I wanted.  ;D

datchison
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Oct 10, 2006 12:19 pm

Re: User Specific Login Redirect

Post by datchison » Tue Jun 24, 2008 3:03 am

Does anyone know if this hack works with 1.0.15?

keith_hatfield
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Wed Jul 30, 2008 2:27 pm

Re: User Specific Login Redirect

Post by keith_hatfield » Wed Jul 30, 2008 2:31 pm

I don't know if this hack works with 1.0.15, but if you are using Community Builder, there is a more 'elegant' solution.

I recently wrote a Joomla! component/CB Plugin that will let you redirect a user upon login based on their user group.

You can download the Documentation/Install package at KeithsCode.com.

Using the component, you can setup different redirections for different user groups, set expirations for the redirection, choose to only redirect the user once (if you have a special announcement or something), and set the priority for the redirection in relation to other redirections that have been setup ...

This way you won't have to make modifications to any of the core files, and you can specify redirects for groups of users rather than each user individually.

Hope This Helps ...

videoIT
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Wed Aug 06, 2008 3:24 pm

Re: User Specific Login Redirect

Post by videoIT » Wed Aug 20, 2008 2:53 pm

Anything like this for 1.5.6? Or will this work as is?

I am looking for a user specific redirect on login facility. Not really interested in redirecting according to user group, although Keith's offering looks good.

Thanks,

Ian . . .

rapereira
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Wed Jun 25, 2008 2:24 pm

Re: User Specific Login Redirect

Post by rapereira » Thu Oct 23, 2008 3:01 pm

This is so close to what I need, and what I need seems so logical, but I can not find a way to do it. Maybe you can help, or maybe this will do it but I can't determine how.

When a user clicks on an article link and is directed to the CB Login I would like them to be sent to the article they were attempting to view upon successful login. Is there a way to accomplish this?

I am running Joomla 1.5.6 - so if your add on will help me I will be anxiously awaiting the next version.

videoIT
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Wed Aug 06, 2008 3:24 pm

Re: User Specific Login Redirect

Post by videoIT » Fri Oct 24, 2008 6:42 am

I haven't been able to find a solution so I have now added a menu item that links to a Simple Machines Forum which I have customised to meet my requirements. Clients have their own unique board that only they can see when they use the SMF login.

Not ideal, but it gets me 75% of what I wanted. I would have preferred to remain in Joomla, though. :(

User avatar
astrobot
Joomla! Apprentice
Joomla! Apprentice
Posts: 34
Joined: Mon Oct 06, 2008 3:45 pm

Re: User Specific Login Redirect

Post by astrobot » Sat Nov 08, 2008 10:19 pm

This looks like what i want too for Joomla 1.5.6, but dont really know where to look for files with the right code to start with this in the new version of Joomla.

The files are different than what was first posted in this topic by barnett..

The first xml file to change seems now to be:
/administrator/components/com_users/models/user.xml

as for the index.php i have no idea... can somebody help out? And by the way, would this code still be valid for the new Joomla version?

thanks

User avatar
indrajitch
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Wed Dec 03, 2008 9:25 am

Re: User Specific Login Redirect

Post by indrajitch » Wed Dec 24, 2008 4:42 am

Couldn't find any solution for this with extensions, modules or hack for 1.5.x

anybody - any luck?

videoIT
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Wed Aug 06, 2008 3:24 pm

Re: User Specific Login Redirect

Post by videoIT » Wed Dec 24, 2008 8:43 am

Am I right in thinking that this is going to be a feature of 1.6? If so, I imagine there wouldn't be many developers wanting to spend time putting together an extension that would only have a short shelf life.

Qwomg
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Sun Jan 25, 2009 7:09 pm

Re: User Specific Login Redirect

Post by Qwomg » Sun Jan 25, 2009 7:58 pm

Instructions for Joomla 1.5(.8 ) (with credit @barnett):

1. On the files
administrator/components/com_users/models/author.xml
administrator/components/com_users/models/registered.xml
administrator/components/com_users/models/user.xml
right before </params> add the following lines:

Code: Select all

<param name="login" type="menuitem" default="" label="Login Redirection URL" description="What page will the login redirect to after user login, if let blank will load module default" />
<param name="logout" type="menuitem" default="" label="Logout Redirection URL" description="What page will the login redirect to after user logout, if let blank will load module default" />
2. Go to components/com_user/controller.php and after line ~134:

Code: Select all

$error = $mainframe->login($credentials, $options);
add the following code:

Code: Select all

$user =& JFactory::getUser();
$menu =& JSite::getMenu();
$userRedirect = $user->getParam('login');
if ($userRedirect) {
        $item = $menu->getItem($userRedirect);
	$url = $item->link;
        $return = $url;
}
3. One the same file (controller.php) after line ~160:

Code: Select all

	function logout()
	{
		global $mainframe;
add:

Code: Select all

$user =& JFactory::getUser();
$menu =& JSite::getMenu();
$userRedirect = $user->getParam('logout');
if ($userRedirect) {
        $item = $menu->getItem($userRedirect);
	$url = $item->link;
        $return = $url;
}
then a couple of lines later, after

Code: Select all

		if(!JError::isError($error))
		{
add:

Code: Select all

if (!$return)
one line before the next if.

4. Set up individual user login and logout redirections from the user manager.

QuBic
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Jan 29, 2009 11:49 pm
Location: UK
Contact:

Re: User Specific Login Redirect

Post by QuBic » Fri Jan 30, 2009 12:00 am

Qwomg, if that works (and I will try tonight) then thank you!
videoIT wrote:Am I right in thinking that this is going to be a feature of 1.6? If so, I imagine there wouldn't be many developers wanting to spend time putting together an extension that would only have a short shelf life.
I hope it will be a feature, its seems like such a simple thing (to someone that knows very little about programming :-[ ) but, it's something I personally want to use on my sites.

apikas
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Thu Feb 12, 2009 9:45 am

Re: User Specific Login Redirect

Post by apikas » Thu Mar 05, 2009 5:43 am

Thanks Qwomg and @barnett!

Your modifications did almost exactly what I wanted. But I would like the menu to expand as well, and have the breadcrumbs reflect the menu choice. I added an Itemid to the url, and that seems to work. See below.
Qwomg wrote:Instructions for Joomla 1.5(.8 ) (with credit @barnett):
...
2. Go to components/com_user/controller.php and after line ~134:

Code: Select all

$error = $mainframe->login($credentials, $options);
add the following code:

Code: Select all

$user =& JFactory::getUser();
$menu =& JSite::getMenu();
$userRedirect = $user->getParam('login');
if ($userRedirect) {
        $item = $menu->getItem($userRedirect);
	$url = $item->link; // <---- Here
        $return = $url;
}
...
Itemid to make the menu expand:

Code: Select all

	$url = $item->link [b]. "&Itemid=" . $userRedirect[/b];
Same thing with the logout.

JoshuaLewis
Joomla! Apprentice
Joomla! Apprentice
Posts: 34
Joined: Sat Feb 14, 2009 10:42 pm
Location: Houston TX, USA
Contact:

Re: User Specific Login Redirect

Post by JoshuaLewis » Sat Mar 28, 2009 6:09 am

I used Qwomg's instructions to set this up in Joomla! 1.5.9 and it seems to be working. I still need to do some more testing though.

components/com_user/controller.php appears to have had some changes made since v1.5.8 and this has changed the line numbers that Qwomg referenced.
I found the contents indicated for line 134 are now at 144 and line 160 at 167.

My thanks to barnett and everyone else who has contributed.

JoshuaLewis
Joomla! Apprentice
Joomla! Apprentice
Posts: 34
Joined: Sat Feb 14, 2009 10:42 pm
Location: Houston TX, USA
Contact:

Re: User Specific Login Redirect

Post by JoshuaLewis » Sun Mar 29, 2009 4:00 am

Note: In components/com_user/controller.php on lines 184 to 186 (for Joomla! 1.5.9) the code that executes the user redirect on logout seems to reject any target url with 'com_user' as one of the variables.

Code: Select all

if ( $return && !( strpos( $return, 'com_user' )) ) {
$mainframe->redirect( $return );}
This had me scratching my head for a little while.

mtlhead
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Sun Mar 16, 2008 11:25 pm

Re: User Specific Login Redirect

Post by mtlhead » Sun Apr 12, 2009 11:41 pm

This is a truly fantastic thread. I have got everything working as described. I would like to add more though.

Where are the default settings for login/logout redirection? Is it possible to add selection boxes to the global configuration page for defaults, just like we added them for the individual users?

Jason

JoshuaLewis
Joomla! Apprentice
Joomla! Apprentice
Posts: 34
Joined: Sat Feb 14, 2009 10:42 pm
Location: Houston TX, USA
Contact:

Re: User Specific Login Redirect

Post by JoshuaLewis » Mon Apr 13, 2009 4:30 am

mtlhead wrote:Where are the default settings for login/logout redirection?
Good idea, lucky for you I came across a docs page recently that should help, lucky for me you thought of it because it solves a usability issue that's been nagging at the back of my brain.

http://docs.joomla.org/Tutorial:Templat ... meter_type

Just find the item ID of the menu item you want and add it to the parameters that you created in the administrator/com_users/models files, they should already contain default=""
mtlhead wrote:Is it possible to add selection boxes to the global configuration page for defaults, just like we added them for the individual users?
I'm not sure how to accomplish this.

cyberken13
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Sun May 06, 2007 9:40 pm

Re: User Specific Login Redirect

Post by cyberken13 » Mon Apr 13, 2009 6:24 pm

Here's an interesting observation. In Joomla 1.5.9 if you want to set the redirection after login from 'Default Login Layout - parameters' do not use the full URL string. Example:

The full string doesn't work
- http://www.yourdomain.com/index.php?opt ... 7&Itemid=2

Just use the internal link within 'Login Redirection URL'
- index.php?option=com_content&view=article&id=47&Itemid=2


The login redirection will now work. If your using login SSL the redirection page will still show HTTPS until you click away to a menu item with SSL 'off'.

mtlhead
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Sun Mar 16, 2008 11:25 pm

Re: User Specific Login Redirect

Post by mtlhead » Thu Apr 16, 2009 10:29 pm

I've given up on my additions. I was able to get a frame on the config page but no listboxes. It seemed to be getting heavy into core files. Rather I will set my defaults in the code.

I will also probably prepare a whitepaper suggestion to the Joomla architects for a future release.

ruralIndia
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Sat May 30, 2009 7:13 am

Re: User Specific Login Redirect

Post by ruralIndia » Sat May 30, 2009 7:15 am

after login --- to redirect the user back to the page which the user was trying to access --
--------------------------------------------------------------------
file 1: components/com_login/login.html.php
<input name="redirect" type="hidden" value="<?php
$url= $_SERVER['QUERY_STRING'];
$url = str_replace('option=com_login&', 'f', $url);
$url = str_replace('prop=admin', '', $url); // replace the name of the module with what you have
$url = substr_replace($url, 'index.php?', 0, 0);
echo $url ?>"/>


file 2:
if you have some user integration module in your component
inside the login handler
$_POST['redirect'] = $_POST['redirect'];
before calling $mainframe->login( )

AND finally file 3:
in includes/joomla.php - in login function
find the following 2 lines
$username = stripslashes( strval( mosGetParam( $_POST, 'username', '' ) ) );
$passwd = stripslashes( strval( mosGetParam( $_POST, 'passwd', '' ) ) );
and add this 3rd one
$redirect = stripslashes( strval( mosGetParam( $_POST, 'redirect', '' ) ) );


and then towards the bottom of that function
find the following line
mosCache::cleanCache();
add the following line after that
mosRedirect($redirect);

and thats it.

So now when you go to any page in your component that needs user login --- after login the user will be sent back to the correct page

mtlhead
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Sun Mar 16, 2008 11:25 pm

Re: User Specific Login Redirect

Post by mtlhead » Tue Jun 02, 2009 11:20 pm

Thanks for the post ruralIndia. However:

file1 - I do not have
file3 - I have but only contains an include function for the application.php file. The application.php file does not have the lines specified

Are you using version 1.0 or 1.5?

sac5735
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Wed Jul 22, 2009 4:09 pm

Re: User Specific Login Redirect

Post by sac5735 » Wed Aug 05, 2009 6:30 am

Hi,

I am running Joomla 1.5.11. I have tried to insert this code. When I put specific redirects in the user manager, it still directs me to the wrong page. The screen when you setup the default login still shows the fields for login/out redirects. I am assuming it is still calling those fields. I can't seem to find what I need to change... I could really use some help! It would be greatly appreciated!

Thanks

sraj49
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Mon Jul 06, 2009 8:45 pm

Re: User Specific Login Redirect

Post by sraj49 » Wed Aug 05, 2009 4:42 pm

I had gone through this issue and finally found a solution with the help of Olaf. Are you looking for redirecting specific users to specific urls? then it is easy.

1 create field for the url........For this you need to change the following codes:

a) admin/component/com_user/models/user.xml ...add this at the end....

<param name="url" type="text" default="" label="url" description="url" />

b) admin/components/com_user/models/registered.xml...add this at the end...

<param name="url" type="text" default="" label="url" description="url" />

2 After this go to components/com_user/controller.php...after line 146???

if(!JError::isError($error))

change the code like this...

{
$user =& JFactory::getUser();
$user->getParameters();
$return = $user->getParam('url');

// Redirect if the return url is not registration or login
if ( ! $return ) {
$return = 'index.php?option=com_user';
}

$mainframe->redirect( $return );
}
else

The only changehere is that you have added

$user =& JFactory::getUser();
$user->getParameters();
$return = $user->getParam('url');

Now when you create a user using User Manager , specify the url in the ' url ' field. If you want to edit later you can do that also. once you do this when the user logins it automtically redirects the user to the specific 'url' in the user profile.

May be I have been more elaborate than normal...

Raj

iamwaggle
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Tue Sep 01, 2009 8:05 am

Re: User Specific Login Redirect

Post by iamwaggle » Tue Sep 01, 2009 8:11 am

Hey sraj49!

Your hack works great! Here's the added redirect hack for the backend login:

/administrator/components/com_login/admin.login.php

Approximately Line 66???

Change this:

if (!JError::isError($result)) {
$mainframe->redirect('index.php');
}

To this:

$user =& JFactory::getUser();
$user->getParameters();
$return = $user->getParam('url');

if (!JError::isError($result) && ! $return) {
$mainframe->redirect('index.php');
}else if (!JError::isError($result)) {
$mainframe->redirect($return);
}
Last edited by iamwaggle on Tue Sep 01, 2009 4:06 pm, edited 1 time in total.


Locked

Return to “Core Hacks and Patches”