refresh JUser object without logout / login

1,080 views
Skip to first unread message

Joe028

unread,
Sep 8, 2012, 6:28:57 PM9/8/12
to joomla-de...@googlegroups.com
Paypal sends information to my script which processes a transaction. This changes the user ACL levels.  I have written my script to directly change the usergroups in the #__user_usergroup_map table.  The effect of the changes I make to the database do not take effect until the user logs out and then logs back in.

How can I refresh the user object so that the Authorized group levels are all correct based on the user session data... All without having to login / logout.

Any help appreciated thank you.

Joe

JSamir

unread,
Sep 10, 2012, 1:57:02 AM9/10/12
to joomla-de...@googlegroups.com
Look at JUser, there should be a method like bind() or smthn (not sure about the name).

JSamir

unread,
Sep 10, 2012, 2:00:31 AM9/10/12
to joomla-de...@googlegroups.com
ps: your "script" must run within joomla with the current user logged in for that to work. If your script is independent of the logged-in user, then you need another approach.

subtextproductions

unread,
Sep 10, 2012, 12:15:10 PM9/10/12
to joomla-de...@googlegroups.com
Joe,

I've run in to this same problem. It sounds to me that you are processing a payment, and then, for example, you want to change the user's access level based on receipt of payment. The user is logged in to the system and you update the database to reflect the change in user group. The problem is that the user object (JFactory::getUser) is still using all the values stored in the session and is not updated to reflect the new values set in the database.

So, the question is really: 

How do I refresh the user session with new data from the database?

Joe Patterson

unread,
Sep 10, 2012, 1:47:55 PM9/10/12
to joomla-de...@googlegroups.com

That is exactly correct. Any direction how you were able to accomplish that?  I didn't think bind() would work, like suggested above.

Swapnil Shah

unread,
Sep 10, 2012, 3:06:19 PM9/10/12
to joomla-de...@googlegroups.com
I believe that code will not work because when you use get user, it heck the session and if it doesn't have it there then it fetches it from the db. 

There is a method there, which will tell JFactory to fetch you a new user object for the current user. I will see if I can find it when I get home today. 

Just thought of this, you could set the Juser object in session to null, and then request a Juser object it will have to go to db to fetch that information. 

Regards, 


Neil
Sent from my iPhone

On Sep 10, 2012, at 2:36 PM, Fnatte <matt...@gmail.com> wrote:

I've used the code below before. Give it a try. 

// Force reload from database
$user = JFactory::getUser();
$session = JFactory::getSession();
$session->set('user', new JUser($user->id));

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/uYqjNXYuaMsJ.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

Joe028

unread,
Sep 10, 2012, 4:48:18 PM9/10/12
to joomla-de...@googlegroups.com
That seems to work correctly. When I tested, I logged in as a user, then went to my database and changed an ACL level (simulating my PDT coming in from PayPal). Then I ran this code in a View just to see the results.
// Force reload from database
$user = JFactory::getUser();
print_r($user);
$session = JFactory::getSession();
$session->set('user', new JUser($user->id));
$user = JFactory::getUser();
print_r($user);

 The first user object showed all the cached user information, and the second object reflected the changes I made in the database.  The only thing that was missing were things like the _authGroups:protected and _authLevels:protected

However, everything seemed to function properly in changing the access levels for this user when navigating the site. I'm sure that once some other element needed the ACL information it somewhere calls the user object again and regenerates everything it needs.

Thanks for taking it easy on me with my first post... and thanks for the help.

Joe

subtextproductions

unread,
Sep 14, 2012, 11:52:03 AM9/14/12
to joomla-de...@googlegroups.com
Thanks for taking the time to figure that out and post the results for the rest of us Joe. It's a problem I too have run into, but never had the time to sit down and figure out how to resolve it. I knew the answer had to be something similar to what you had posted, but I never imagined it could be so simple.

I'm glad we could point you in the right direction and that you had such a good experience with your first post here. Don't forget to pay it forward by coming back and answering someone else's question.
Reply all
Reply to author
Forward
0 new messages