J3.6.4 JUser data discrepancy in front/back-end in the user plugin group

69 views
Skip to first unread message

Georgios Ntampitzias

unread,
Dec 7, 2016, 12:25:55 PM12/7/16
to Joomla! CMS Development
Use case:
A J3.6.4 user plugin that accesses the JUser object in both onUserBeforeSave and onUserAfterSave (both call JFactory::getUser()).

Problem:
When the user email is changed from the front-end profile-edit page, the email value of the JUser email in onUserAfterSave is incorrect (the old value). Even if 'incorrect' is a misuse of a term (because that might be normal), then there's a discrepancy with what is being reported when the same change is done from the back-end user manager.


Sample plugin code:
public function onUserBeforeSave($user, $isnew, $data){ JFactory::getUser($user['id']);  }   //Wouldn't work for new users I guess
public function onUserAfterSave($user, $isnew, $success, $msg){ $this_is_the_discrepancy = JFactory::getUser($user['id'])->get('email'); }

Observations:
Removing the JFactory::getUser() in the onBeforeUserSave corrects this issue. So looks like it has to do with how/when the JUser static var value gets updated upon user modification: Back and front end disagree on the issue.

George Wilson

unread,
Dec 8, 2016, 6:07:26 PM12/8/16
to Joomla! CMS Development
JFactory::getUser() will only get updated on page reload I think. What you need to use is the $user property being injected into both methods to access the new data :)

Kind Regards,
George

Georgios Ntampitzias

unread,
Dec 9, 2016, 8:13:33 AM12/9/16
to Joomla! CMS Development
Without looking into the relevant code bits:
It seems that J does clear the relevant static-"cached" object. That's evident from the fact that you can access the correctly-valued object in the onUserAfterSave method in "most" of the cases.
It's just that when the front-end does this process, we're given the opportunity to initialize the static-"cache" when we shouldn't (via a getUser() call).

Arguably, one could say "don't use ::getUser(), look at what the vanilla J user plugin does with $this->_getUser()". I could refactor my code around this, no problem (why, hello there mr. JTableUser, you look handsome tonight). But there's two pretty serious points that would still be pending:
1) getUser() is no longer doing exactly what it says. It does that "on most places". And suddenly, we have to be really careful on what we call within the user plugin group in case that somehow cascades down to a getUser call.
2) There's still a behavior discrepancy between front and back-end that shouldn't be there.

I could go on about #1 and how there seems to be mass confusion regarding all things user-update (I've looked into the CB and JomSocial code, and it's not pretty). But this would be beyond the point of this post.
Reply all
Reply to author
Forward
0 new messages