First login redirect plugin

This forum is for general questions about extensions for Joomla! 2.5.

Moderators: pe7er, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
combrains
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu Jan 26, 2012 12:11 am

First login redirect plugin

Post by combrains » Sat Feb 18, 2012 10:31 am

Hi All,

I'm in the midst of hacking a plugin called plg_FirstLoginRedirct. Originally written for J1.5, I'm attempting to make it 2.5 compatible, however, my knowledge of PHP and the Joomla API is limited. I think I am nearly there, I just need a little help to get over the line. This is a function which I believe is highly sought after. The plugin is GPL'd and I'd be happy to add your name to the credits and re-publish a new version if you lend me a hand.

The original plugin can be found here http://construct-framework.com/redirect ... gin-plugin

Below is my modified code. The plugin simply checks the user's last login date and if it is not set (all 0's) sets it and redirects appropriately.

It currently causes my local Apache install to drop the connection when logging in while the plugin is active. Any comments/code help/pointers would be much appreciated.

Cheers

Code: Select all

// no direct access
defined('_JEXEC') or die;

jimport( 'joomla.plugin.plugin' );

class plgUserRedirectFirstLogin extends JPlugin
{
	
	function onUserLogin($user, $options)
	{			
		$app =& JFactory::getApplication();
		$user =& JFactory::getUser();		

		if ($user->get('guest') != 1 && $user->lastvisitDate == "0000-00-00 00:00:00")
		{
			$user->setLastVisit();
			$date = JFactory::getDate();
			$user->lastvisitDate = $date->toMySQL();			
			$app->redirect($this->params->get('destination'));
         return true;
		}
	}
}
Last edited by combrains on Sat Feb 18, 2012 9:08 pm, edited 2 times in total.

User avatar
NachoBrito
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Tue Sep 21, 2010 3:35 pm
Location: Madrid (Spain)
Contact:

Re: First login redirect plugin

Post by NachoBrito » Sat Feb 18, 2012 6:24 pm

onUserLogin is a method to handle login, so it is supposed to return true or false to indicate the login status. You have no return statement in your function, so the framework treats it as a null/false response and then cancels login.

Your code should work fine by just adding "return true;" at the end.

HTH.
Soporte técnico Joomla en Español: http://www.expertosjoomla.es

combrains
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu Jan 26, 2012 12:11 am

Re: First login redirect plugin

Post by combrains » Sat Feb 18, 2012 9:16 pm

Thanks for the tip Nacho, login now works. I have updated the code snippet above to reflect my changes.

However, the redirect still does not. If I reset a user's login date to all 0's in the DB, it should trigger the plugin and redirect. I have tried to hard-code the redirect URL and this does not work either. The system seems to always redirect to option=com_content&view=featured&Itemid=21 on login.

I'm using the standard com_user component and the mod_login module to handle logins. could either of these be overriding my redirect? Or is there something wrong with my redirect code? Is there a way I can debug my code and see what it is doing, and any errors it is producing?

Sorry for all the questions but I'm quite new to all this :)

Cheers
Last edited by combrains on Sat Feb 18, 2012 9:40 pm, edited 1 time in total.

User avatar
NachoBrito
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Tue Sep 21, 2010 3:35 pm
Location: Madrid (Spain)
Contact:

Re: First login redirect plugin

Post by NachoBrito » Sat Feb 18, 2012 9:23 pm

Well, If you are working in a local web server you can use your IDE to debug Joomla. I personally use Netbeans + MAMP on Mac OSX and works quite well.
Soporte técnico Joomla en Español: http://www.expertosjoomla.es

combrains
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu Jan 26, 2012 12:11 am

Re: First login redirect plugin

Post by combrains » Sat Feb 18, 2012 9:52 pm

Thanks for the quick reply.

I'm downloading NetBeans as I type.

Any ideas why the system would be ignoring my redirect and instead be redirecting all logins to option=com_content&view=featured&Itemid=21

I have tried to hard code the redirect as

Code: Select all

$app->redirect('index.php?Itemid=178&option=com_civicrm');
however, the system still redirects to the com_content link. Should I be redirecting using the JRouter method instead?

Cheers

User avatar
NachoBrito
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Tue Sep 21, 2010 3:35 pm
Location: Madrid (Spain)
Contact:

Re: First login redirect plugin

Post by NachoBrito » Sun Feb 19, 2012 10:00 am

Well, AFAIK, it is in the controller where you set the next url by calling $this->setRedirect(...). Then, the dispatch method calls $app->redirect($this->redirect).

So, I'd start debugging your controller action to see what happens after your code ends.

Cheers,
Soporte técnico Joomla en Español: http://www.expertosjoomla.es

Liora
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed May 09, 2012 9:12 am

Re: First login redirect plugin

Post by Liora » Sun May 20, 2012 8:10 pm

Hello Combraines,
I am totaly new to joomla,
and in need for this modification for the 2.5 joomla version of this module.
I was wondering whether you could publish the entire code you developed,
and some explanation on how exactly to do this modification (i am working on a redHat webserver).
OR- if a version of this module for 2.5 has been published (i did not find one at all)- that's good too:)
Thank you very much,
Liora

Drizzt99
Joomla! Apprentice
Joomla! Apprentice
Posts: 22
Joined: Wed Aug 20, 2008 12:48 pm

Re: First login redirect plugin

Post by Drizzt99 » Tue May 22, 2012 2:21 pm

Hi Liora,
Here's the code that's working for me:

Code: Select all

// no direct access
defined('_JEXEC') or die;

jimport( 'joomla.plugin.plugin' );

class plgUserRedirectFirstLogin extends JPlugin
{
	
	function onUserLogin($user, $options = array())
	{			
		$app =& JFactory::getApplication();
      		
		$id = intval(JUserHelper::getUserId($user['username']));
		$user =& JFactory::getUser($id); 
                
		if(!$user->guest && $user->lastvisitDate == "0000-00-00 00:00:00")
		{
			$user->setLastVisit();
			$date = JFactory::getDate();
			$user->lastvisitDate = $date->toMySQL();
			$app->redirect($this->params->get('destination'));

			return true;         
		}else{
			//Do whatever you want
		}
	}
}
Hope this helps in any way.
The problem with the code above is, that getUser() is not yet giving the user that logged in, but the function gets his username. So all I did was to use the username to get the id, and then get the user's details.

Joomla 2.5 docs are still a little bit messed up (to me at least), so there might be a nicer way to do this...

Liora
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed May 09, 2012 9:12 am

Re: First login redirect plugin

Post by Liora » Wed May 23, 2012 8:05 am

Thank you!
a few more questions-
where did you insert this code- in one of the plugin files is presume.?
and another thing- did you hard coded the 'destination' as you showed above-
'index.php?Itemid=178&option=com_civicrm'?

Thanks again!
Liora

Drizzt99
Joomla! Apprentice
Joomla! Apprentice
Posts: 22
Joined: Wed Aug 20, 2008 12:48 pm

Re: First login redirect plugin

Post by Drizzt99 » Wed May 23, 2012 9:09 am

Hi,
there's only one file to edit:
/plugins/user/
you will find firstlogin.php there or inside the firstlogin directory. I don't have access from where I am now.

About hard coding: I did not hard code the URL. You put it in the plugin from backend.

gurukatre
Joomla! Intern
Joomla! Intern
Posts: 50
Joined: Tue Apr 28, 2009 7:37 am
Location: pune
Contact:

Re: First login redirect plugin

Post by gurukatre » Mon Aug 13, 2012 8:16 pm

Hi Combrains,

you don't need to hack any file.
you can create user plugin for joomla2.5 and check lastvisitDate on users table if last visit date is 0000-00-00 00:00:00 you can redirect where you want to.
Thanks
Kind Regards

pvassalli
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Wed Aug 29, 2012 10:24 am

Re: First login redirect plugin

Post by pvassalli » Sat Nov 17, 2012 8:34 pm

Dear combrains,
I'm trying to get the plugin you have developed for joomla 1.5 but the link:
http://construct-framework.com/redirect ... gin-plugin
is broken.
Can you provide me a new working link?
Thanks in advance.
Best regards,
Pietro

gurukatre
Joomla! Intern
Joomla! Intern
Posts: 50
Joined: Tue Apr 28, 2009 7:37 am
Location: pune
Contact:

Re: First login redirect plugin

Post by gurukatre » Sun Jan 13, 2013 2:00 pm

it seems for joomla 1.6 or grater version.

check this one may be this will help

http://extensions.joomla.org/extensions ... rect/13961
Thanks
Kind Regards


Locked

Return to “Extensions for Joomla! 2.5”