Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: [SOLVED] HOW TO Set Custom Session after login

[SOLVED] HOW TO Set Custom Session after login 17 Oct 2012 08:14 #4615

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi fellow cooks!

I know how to set a custom session variable using
$myComponentSession = $session->set('my_component_session');
& retrieve it using
$myComponentSession = $session->get('my_component_session');
however, the part I'm missing is HOW DO I AUTOMATICALLY SET THIS WHEN THE USERS LOGIN?

Scenario
Basically, I need to run a db query to check if the newly logged in user is a project user, look up their user_id in my project_users table then iterate through a project_tasks, project_events and so on, building an associative session array. I need this so that I can check if a user has incomplete tasks and redirect them accordingly and to let them know of upcoming tasks, + to get feedback on events that they've attended... This part is easy! I'm going to do this in a function, let's say createProjectUserSession() where all of the necessary queries are run.

My problem is knowing exactly how to invoke this createProjectUserSession as soon as the user log's in. I know in my component I can call it in the main controller by always redirecting them to my component after login however, I don't want admins of the component to have to set it up that way. The idea is that if a user has incomplete tasks/overdue tasks, they will not be able to go anywhere else on the site until it's done - a flag in the session!

Can I and if so, somehow bind this function to an onAfterLogin even???

Any help with this would be most appreciated!!!

Many thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 27 Oct 2012 11:44 by admin.
The administrator has disabled public write access.

Re: HOW TO Set Custom Session after login 24 Oct 2012 11:57 #4714

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
****EDIT**********

THIS IS NOW TRUE AND CORRECT - updated to actually work in J2.5

*********************

OK, finally got an hour to look a bit further into this!

for anyone needing to do this, here's how...

Basics
It seems that the best, if not, only way to go is using a custom plugin to do the job. From my research into this, Joomla has numerous plugins/event triggers to allow us to hook on to events and run code when these events occur. There are a number of plugin groups to allow extension developers access to these core events in addition to the ability to create one's own plugin group to allow 3rd party integration into your component.

SCENARIO (recap)
In my case, I need to be able to be able to automatically change the prices across my store according to the type of customer a user is and present them with different store/purchasing options accordingly. I also need to be able to check the user/customer type immediately upon successful login so that no matter if the user has entered my component or not, all modules and component pages reflect options specific to them.

WORKFLOW
    When a user logs-in, I want to;
  1. check user records in my component to find out what kind of customer they are - private customer or trade customer
  2. get the user's session
  3. set some custom session data relating to their customer type (billing address, currency, overall discount rate for trade accounts and so on)
  4. setup/display the cart for shoppers to add items to purchase
  5. display the component and any related modules customised to the customer type (changing the prices for traders for example)
TO DO:
  1. Create a custom plugin group for your extension by defining group attribute of your plugin's root element
    <extension version="2.5" type="plugin" group="your_custom_group_name">
    This step is not required but a good idea to allow further 3rd party development of your extension.
  2. Define your plugin's xml manifest file according to the plugin standards found here on the Joomla Wiki. Your file should look something like this:
    <?xml version="1.0" encoding="utf-8"?>
    <extension version="2.5" type="plugin" group="[Your Plugin Group Name]">
    	<name>[Component Name] - [Plugin Name]</name>
    	<author>[Your Name]</author>
    	<creationDate>October, 2012</creationDate>
    	<copyright>Copyright (C) 2011</copyright>
    	<license>GNU General Public License version 2 or later</license>
    	<authorEmail>me@mydomain.foo</authorEmail>
    	<authorUrl>www.mydomain.foo</authorUrl>
    	<version>1.0</version>
    	<description>Description of plugin and it's purpose goes here</description>
    
    	<files>
    		<filename plugin="[Plugin Name]">[plugin_name].php</filename>
    		<filename>index.html</filename>
    	</files>
    
    	<config />
    
    </extension>
  3. Define your plugin code inside the file [plugin_name].php
    <?php
    defined('_JEXEC') or die;
    
    class plg[Plugingroupname][Pluginname] extends JPlugin //Replace group and plugin values in the Classname
    {	
    	//Invoke the plugin upon user login
    	//This was onLoginUser in J1.5
    	public function onUserLogin($user, $options) // parametise the function as required
    	{
    
    	// populate
    	$field1 = $this->getOtherRelatedData($user);
    	$username = $this->getOtherRelatedData($user);
    
    	$myCustomSession = array();
    
    	$myCustomSession['name_of_array'][] = array('value1' => $field1, 'username' => $username);
    	$myCustomSession['other_data'][] = array('other_val1' => 98765, 'other_val2' => 'Some other information');
    
    	$session->set('myCustomSession', $myCustomSession);
    	
    	return true;
    
    	}
    
    	// Get some data that you only want called from the plugin itself
    	
    	private function getRelatedData($user)
    	{
    		$db = JFactory::getDBO();
    
    		$query = $db->getQuery(true);
    
    		$query->select('field1');
    		$query->from('#__mycomponent_table');
    		$query->where('field1 = ' . $user);
    
    		$db->setQuery($query);
    		return $db->loadResult();
    	}
    
    	// Get some data that you may want to call anywhere in your component - E.G. the user's username
    	public function getSomeOtherRelatedData($user)
    	{
    		$db = JFactory::getDBO();
    
    		$query = $db->getQuery(true);
    
    		$query->select('username');
    		$query->from('#__users');
    		$query->where('id = ' . $user);
    
    		$db->setQuery($query);
    		return $db->loadResult();
    	}
    }
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 27 Oct 2012 11:31 by JoomGuy. Reason: CORRECTED to align with J2.5
The administrator has disabled public write access.

25 Euros for J2.5 onLoginUser Plugin Help! 25 Oct 2012 15:05 #4740

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
OMG!!!!! :sick:

I was writing this topic as I was learning about plugin development/triggering core events and was sure that this was going to work... However, this was not to be!!! Damn it!

Has anyone any experience of calling a plugin upon a user login?

Can anybody see why the above code construct wouldn't work?

Is there a better way of handling this?

Anyone up for a 25 euro challenge? Would be more than happy to pay anyone 25 euros to demonstrate/provide the working code to invoke this simple onLoginUser event by redirecting the browser or anything that proves that the function is actually being called...

BTW, I'm using j2.5... Docs on plugins for this are very limited. :evil: Also, as I know how to setup the session variables themselves, I'm currently only trying to invoke the plugin by calling a redirect inside it immediately that the user has logged in successfully... Here is the specific code I'm trying to run to test but I'm getting nothing...
<?php
defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

class plgJstoreTest extends JPlugin
{
	public function onLoginUser($user, $options=array())
	{
		header('Location: http://localhost/dev1/test/');
		return true;
	}

}

Many thanks!!!

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 25 Oct 2012 15:59 by JoomGuy.
The administrator has disabled public write access.

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 07:53 #4761

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Can you try to dump the params in your onLoginUser() function ?

Is it entering in when user log in ?

If it is entering, then the problem is in your code.

It it is not entering, here what you must do :
First, wich 'group' are you using ?

How it works ?
Joomla do not always load all plugins. In the caller, you must load the group of plugin with :
JPluginHelper::importPlugin( 'myplugingroup' );

BUT :
You will not hack com_users in your case. I was just explaining when you use the plugins in a new component, with a new group.

So, com_users is loading, at least 'system' (always) and 'user' groups. (and maybe others...)

Did you specified the group 'user' ?
Is your plugin located in the plugins/user/ dir after install ?

This is the first thing to check.
Hope it helps
Coding is now a piece of cake
Last Edit: 27 Oct 2012 07:55 by admin.
The administrator has disabled public write access.

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 07:59 #4762

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi admin,

Yes it's a user plugin, located in the plugins/user directory.

The XML is set accordingly, referencing only the 2 files - index.html & test.php

The problem is that it just isn't doing anything. I'm only setting a redirect in the function as a way of proving it works.

Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 08:10 #4765

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Maybe it works.

A redirection will not be applyied because it will be overrided by the com_user component.

Try :
exit('Cool');

Then you are sure it entered.
If not you have another problem.

Let's check this first.
Coding is now a piece of cake
The administrator has disabled public write access.

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 08:12 #4766

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
OK, thanks!

I did try a print_r() on an array with a die() after it already but that didn't work.

Will give an exit() a go.

Will be right back,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 08:28 #4768

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64

File Attachment:

File Name: test.zip
File Size:1 KB

Hi @admin,

Unfortunately, I'm getting nothing!

Please see code attached - as you can see, it's dead simple so I must be missing something here.

I've tried with and without the first line after JEXEC - jimport('joomla.plugin.plugin'); although I'm sure that it is not required... Also, it has no effect either way.

Many thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 27 Oct 2012 08:33 by JoomGuy. Reason: added ZIP file
The administrator has disabled public write access.

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 08:37 #4769

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Is your plugin activated in the plugins management list ?
Coding is now a piece of cake
The administrator has disabled public write access.

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 08:40 #4770

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Yes, that's what's so weird...

Is there anything wrong with the code?

Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 08:41 #4771

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
You are right. There it seems to be a Joomla issue here.

Try to play with another event.

I cannot trigger 'onUserLogin' too.

It is strange and I cannot help you for the moment, it seems to be complex.

Did you asked on joomla.org forum ?

EDIT : Solved : It is not a Joomla issue.
Coding is now a piece of cake
Last Edit: 27 Oct 2012 11:44 by admin.
The administrator has disabled public write access.

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 08:43 #4772

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
By the way, I tell you where this trigger is raised :

libraries/joomla/application/application.php

login() function

You can try there to hack (temporally) to do your tests, and you can see it is triggering nothing.
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 08:49 #4773

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
I've looked in the forum at forum.joomla.org but there's very little about it.

What other event would you suggest? I need to make sure that the user is already logged in (authenticated)... It's for a shopping cart that only logged in users can use. Therefore I need to check if the user is a trade or regular customer before any of my modules for the cart are displayed.

Using the onAuthenticateUser seems to be more complicated to achieve this as the user has not been authenticated yet - i.e. I'd also have to check they were authenticated, right?

I'll try as you suggest in the meantime too!

Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 09:06 #4774

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
@admin,

OK, I managed to throw an ext("Hello") in right before login() function in application.php returns true. Than works fine...

Are these lines correct?:
// LINES 707 - 711
// Import the user plugin group.
JPluginHelper::importPlugin('user');

// OK, the credentials are authenticated and user is authorised.  Lets fire the onLogin event.
$results = $this->triggerEvent('onUserLogin', array((array) $response, $options));
The array((array) $response, options)) just doesn't seem right to me...?

Any thoughts on my last post?

Many thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: 25 Euros for J2.5 onLoginUser Plugin Help! 27 Oct 2012 09:12 #4775

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Oh yes...

Maybe the class name.

Change :
class plgJstoreTest extends JPlugin
{
}
to
class plgUserJstoreTest extends JPlugin
{
}
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy
  • Page:
  • 1
  • 2
Time to create page: 0.145 seconds

Get Started