J4 method to get user info

pastvne

Bruce Decker
Hi All,
I had some eval code that was trying to load the current logged in user object. The tip in the F4 upgrade directions here: https://github.com/joomlahenk/fabrik/wiki/Upgrading-from-fabrik3.10-to-fabrik4

Include the following tip:

"...or calling it directly in you code such as $user = \Joomla\CMS\Factory::getUser();"

However, I did not have any success with that method or the other offered in the upgrade article. Instead, I found success with:
Code:
$user = Joomla\CMS\Factory::getApplication()->getIdentity();
Also, if you want to access the user's group membership, you can do this:
Code:
$user = Joomla\CMS\Factory::getApplication()->getIdentity();
$groups = $user->getAuthorisedGroups();
and if you need to access the user's authorized view levels, you can do this:
Code:
$user = Joomla\CMS\Factory::getApplication()->getIdentity();
$myViewLevels = $user->getAuthorisedViewLevels();

Passing this along in case others find it to be helpful.
 
Last edited:
You may be surprised, Joomla finally wrote some documentation. More info here:
https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Upgrading_to_Joomla4#Extension

Thank you for sharing this. I starred at that for an hour and still can't understand how to easily know which class a method is with or how to instantiate an object of that class using the new 'use' namespacing. I'm sure I'll get it eventually but, not being a professional programmer, I struggle without use examples. Unfortunately, stack overflow, where I can usually find practical examples, has very few J4 examples (yet).
 
TRhe use statements are very simple actually. In your above examples place a
Code:
use Joomla\CMS\Factory;
at the top of your code block. Then when you need Factory you simply write
Code:
Factory::getIdentity();
or other function,

A good place to get the use paths is in the Joomla API, google it and then choose the version 4 API.
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top