? Success

User tests: Successful: Unsuccessful:

avatar chivitli
chivitli
6 Mar 2014

I wanted to load user by username, like in Example 2 at http://docs.joomla.org/JFactory/getUser.

But, due to a bug in JFactory::getUser() that is not possible.

If we pass a username, we reach this condition:

elseif ($instance->id != $id)
{
$instance = JUser::getInstance($id);
}

But $instance->id will be 0 if person viewing the site is not logged in, and because $id is string and loose comparison is used, string will evaluate to 0 as well, condition won't be satisfied, and empty user object will be returned.

The fix is simple, just making it a strict comparison instead of loose.

To test, make sure nothing broke on the user pages or anywhere where you load User object

Tracker: http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=33434&start=0

avatar chivitli chivitli - open - 6 Mar 2014
avatar jurianeven
jurianeven - comment - 7 Mar 2014

FYI: The Travis CI build failed. I've no clue why it doesn't match though:

-'<input type="url" name="myTestName" id="myTestId" value="http://foobar.com" />'
+'<input type="url" name="myTestName" id="myTestId" value="http://<>"foobar.com" />'

avatar chivitli
chivitli - comment - 7 Mar 2014

@jurianeven

I noticed, but I don't think it has anything to do with this PR. Michael fixed it apparently (725aedc), but the Travis didn't run build for this again. Can it be triggered manually somehow, or I have to pretend I made another commit to force it run?

avatar jurianeven
jurianeven - comment - 7 Mar 2014

or I have to pretend I made another commit to force it run?

I think so. I don't know how to trigger it manually. I also think it has nothing to do with this PR.

avatar chivitli chivitli - change - 7 Mar 2014
Title
Impossible to load user by username when not logged in
[#33434] Impossible to load user by username when not logged in
avatar chivitli
chivitli - comment - 7 Mar 2014

@jurianeven

Done, Travis is happy again.

avatar mahagr
mahagr - comment - 9 Mar 2014

Side note:

Basically what I'm doing is to use JFactory::getUser() on only for the current user and JUser::getInstance() for everyone else.

Joomla is moving away from using JFactory class, so it makes no sense to use it unless really needed.

That said, your change looks good -- just to be sure, I'd test it also works with (string) $id, meaning that the id comes from the database.

PS. whatever we try to do, the function doesn't work with numeric usernames....

avatar brianteeman
brianteeman - comment - 8 Aug 2014

Closed as per the comment on the tracker

avatar brianteeman brianteeman - change - 8 Aug 2014
Status New Closed
Closed_Date 0000-00-00 00:00:00 2014-08-08 14:32:34
avatar brianteeman brianteeman - close - 8 Aug 2014

Add a Comment

Login with GitHub to post a comment