23

To determine whether the visitor is a guest, we use something like this :

if ($user->guest) {
echo 'Hello, guest!';
}

Now, if i want to determine whether the visitor is a registered SuperUser (Administrator) or not, what would be the code for that?

I have some external PHP files which i don't want to be accessible by anyone except the SuperUsers of my Joomla Website. Just trying to secure the files by importing Joomla CMS.

2
  • 1
    Funny I needed this last night and just googled till I found a solution. In my case I was overriding the output from RSFiles so the tool bar only shows up for admins.
    – Brian Peat
    Jun 6, 2014 at 16:33
  • 1
    Actually $user->get('isRoot') which is in the accepted answer, should not be used. Most likely in latest Joomla versions (3.6 and later) it won't work when called like this outside of the JUser::authorize(). Use $user->authorize('your params here') instead, like dev-m's answer.
    – FFrewin
    Oct 8, 2016 at 16:19

7 Answers 7

31

these codes will determine if current logged in user is super user or not -

$user = JFactory::getUser();
$isroot = $user->authorise('core.admin');

that code will check what is mapped to the "Super User" permission in the global configuration and not in only component level. Check it yourself comparing with other ways.

5
  • 2
    The explanation in this answer is a bit weak, but I think core.admin is a better permission to check than core.edit.
    – Brilliand
    Jun 6, 2014 at 16:07
  • 1
    I disagree. core.admin is the permission uses to control access to the global config, so it is the appropriate way to check if a user is the super admin. If you check for the group 8 like the accepted answer, if Joomla! ever decides to randomize it for security sake, your script will break. Always code with change in mind. Unless you never intend to upgrade. Jun 8, 2014 at 15:06
  • This is clearly the best match for a SuperUser check. It will check both for the failsafe, and for the SuperUser permission. github.com/joomla/joomla-cms/blob/staging/libraries/joomla/user/… The other alternative is $user->get('isRoot') which assumes that authorise has already been used to populate the isRoot state. The isRoot can be used directly as it's a protected property. github.com/joomla/joomla-cms/blob/staging/administrator/… Jun 10, 2014 at 4:53
  • 1
    Just to clear any doubts on whether this code is correct or not: this code is used in Joomla's core (Joomla 3.5.1) to check whether the user is a super admin or not. You can find it in line 747 of the libraries/joomla/user/user.php file. It is this one: $iAmSuperAdmin = $my->authorise('core.admin');
    – itoctopus
    Jun 21, 2016 at 4:51
  • In Joomla 4 and later use: $user = Factory::getApplication()->getIdentity(); $isAdmin = $user->authorise('core.admin');
    – Marco
    Apr 11 at 17:33
8

To determine the "Super User" status you have to check $user->isRoot. This property is set after the first authorise check is done and the user is detected to be super admin.

However usually you indeed better want to check if the user has certain permissions like described on the other answers.

1
  • 3
    This assumes $user->authorise() has already been called by something else to populate the isRoot property. Also note that the isRoot property is protected so needs to be access via $user->get('isRoot'). Jun 10, 2014 at 4:54
6

Try this,

$user  = JFactory::getUser();

$user_groups = $user->groups;

print_r($user_groups);

This will return an array, By default Joomla Admin users Super admin (Groups 8) and Administrator (Group 7).

You can check the array have any of these values then it should be Admin user , If you're created Custom User group then you have to check #__usergroups parent Id too.

Hope it helps..

6

You can use a condition to refuse anywho is not in a specific group.

$user = JFactory::getUser();
$groups = $user->groups;

if (in_array(8, $groups)):

 //only enter if the user is in the group 8 (group 8 = Super-Administrator)

endif;
2
  • 1
    Magic number aren't a great solution. You can give Super Admin permissions to any group and/or delete the default Super Admin group. Jun 24, 2016 at 4:11
  • I used something like if(in_array(8, $user->groups)) and it worked for me
    – Linga
    Oct 10, 2016 at 23:32
4

Try below code for checking if the login user is super admin or other users...

$user = JFactory::getUser();
$isAdmin = $user->get('isRoot');
if ($isAdmin) {
echo 'You are an Administrator';
}
else  {
echo 'You are not Administrator';
}

OR

function isSuperAdmin()
{
  $user = JFactory::getUser();
  return $user->get('isRoot');
}    

if (isSuperAdmin()) 
{
  echo 'You are an Administrator';
}
else  {
  echo 'You are not Administrator';
}
3
  • 1
    Actually $user->get('isRoot') should not be used. Most likely in latest Joomla versions (3.6 and later) it won't work at when called like this outside of the JUser::authorize(). Use $user->authorize('your params here') instead, like dev-m's answer.
    – FFrewin
    Oct 8, 2016 at 16:18
  • I will test it first, thanks for pointing out. :)
    – saibbyweb
    Oct 9, 2016 at 16:28
  • 1
    $user->get('isRoot'); not working for J3.6.2.
    – saibbyweb
    Nov 14, 2016 at 11:51
2

Joomla @since 3.2, field UserGroupList uses this to check:

$isSuperUser = JFactory::getUser()->authorise('core.admin');
3
  • This answer is very similar to @dev-m's answer.
    – Farahmand
    Oct 29, 2017 at 7:17
  • @Farahmand you are right, I just added to confirm what is Joomla preferred method and that it is still valid for Joomla version 3+
    – PLBM
    Oct 29, 2017 at 14:55
  • Okay. Anyway, the question has already been tagged with joomla-3.x.
    – Farahmand
    Oct 29, 2017 at 19:05
1

For Joomla 4 and later use:

$user = Factory::getApplication()->getIdentity();
$isAdmin = $user->authorise('core.admin');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.