Take the 2-minute tour ×
Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. It's 100% free, no registration required.

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.

share|improve this question
    
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 at 16:33

4 Answers 4

up vote 1 down vote accepted

You can use the following:

$user = JFactory::getUser();

if ($user->authorise('core.edit', 'com_content'))
{
     echo "<p>You are an admin</p>";
}
else
{
    echo "<p>Permission denied</p>";
}

Please note that this doesn't only detect super users. It detects groups that have privileges to edit everything which is set in the com_content Options. Unfortunately $user->usertype has been deprecated since Joomla 1.6 so you can't use this.

OR

You can use this. By default the super user group has a value of 8 so you can detect this number when the user is logged in:

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

if ($groups[8])
{
     echo "<p>You are an admin</p>";
}
else
{
    echo "<p>Permission denied</p>";
}

Hope this helps

share|improve this answer
    
Thanks Lod! It worked as always, can i know what's the group value of Registered users? –  Saibbyweb Jun 6 at 9:43
    
You're welcome. The group ID for registered users is 2. –  Lodder Jun 6 at 9:47
    
by default the Registered Group has id 2. But you can always find the group id from the groups manager in the backend. –  FFrewin Jun 6 at 9:47
    
@downvoters, please care to explain? –  Lodder Jun 9 at 9:04
    
Simple, as other have said, it's because any group can have core.edit permission on com_content not just Super Users. Also new groups with Super User permissions may not have an id of 8, or maybe it's because there is a defined flag for checking if a $user->isRoot. Joomla itself determines root users (i.e. Super Users) and sets the flag by checking a the array of groups a user belongs ($identities) against the root asset record in the #__assets table e.g. if (JAccess::getAssetRules(1)->allow('core.admin', $identities)) { $this->isRoot = true; return true; } –  cppl Jun 11 at 22:19

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.

share|improve this answer
    
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 at 16:07
    
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 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 at 4:53

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.

share|improve this answer
1  
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 at 4:54

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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