19

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.

  • 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 '14 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 '16 at 16:19
27

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.

  • 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 '14 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. – Mathew Lenning Jun 8 '14 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/… – Peter Wiseman Jun 10 '14 at 4:53
  • 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 '16 at 4:51
7

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.

  • 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'). – Peter Wiseman Jun 10 '14 at 4:54
5

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..

5

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;
  • 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. – David Hayes Jun 24 '16 at 4:11
  • I used something like if(in_array(8, $user->groups)) and it worked for me – Linga Oct 10 '16 at 23:32
3

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';
}
  • 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 '16 at 16:18
  • I will test it first, thanks for pointing out. :) – saibbyweb Oct 9 '16 at 16:28
  • 1
    $user->get('isRoot'); not working for J3.6.2. – saibbyweb Nov 14 '16 at 11:51
0

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

$isSuperUser = JFactory::getUser()->authorise('core.admin');
  • This answer is very similar to @dev-m's answer. – Farahmand Oct 29 '17 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+ – Pedro Bicudo Maschio Oct 29 '17 at 14:55
  • Okay. Anyway, the question has already been tagged with joomla-3.x. – Farahmand Oct 29 '17 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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