Check a user is admin in Joomla 2.5.x

Check a user is admin in Joomla 2.5.x

If you are modifying the edit form for Joomla 2.5.x or other form that requires a need to test if this is a Super Administrator just use the following. Handy if you need to block out certain functionality for different user groups but you still need full access to those options as an administrator.

Joomla 1.5.x world:

>
1
2
3
4
5
6
$user =& JFactory::getUser();
if($user->usertype == "Super Administrator" || $user->usertype == "Administrator") {
  echo 'You are an Administrator';
} else {
  echo 'You are just an ordinary user';
}

Joomla 2.5.x world (this will only check for Super Admins):

>
1
2
3
4
5
6
$user = JFactory::getUser();
$isAdmin = $user->get('isRoot');

if ($isAdmin) {
  // code here
}

We really shouldn’t do it that way though, a check for authorization is better because we can’t really determine if a user has that access anyway, so do this:

>
1
2
3
4
5
6
$user = JFactory::getUser();
if($user->authorise('core.edit', 'com_contact')) {
  echo "Yes, I can edit contacts!";
} else {
  echo "No, I can't edit contacts";<
}