in Joomla, Tips and Tricks

Detect User type in joomla 1.6 or later

In Joomla1.5.x we can detect user type easily or check if a user is admin type.
[code language=”php”]
$isadmin = false;
$user =&JFactory::getUser();
if($user->usertype == "Super Administrator" || $user->usertype == "Administrator"){
$isadmin = true;
}
[/code]

But from joomla 1.6 as the user group architecture is changed the above way will not work.
From j1.6 we can do this in this way, here actually I was trying to detect if the user is super user or not like admin user in j1.5

[code language=”php”]
$isadmin = false;
$user =&JFactory::getUser();
$db = JFactory::getDbo();
//var_dump($user->getAuthorisedGroups());
$userid = intval($user->get( ‘id’ ));
if($userid > 0){
$query = $db->getQuery(true);
$query->select(‘g.title AS group_name’)
->from(‘#__usergroups AS g’)
->leftJoin(‘#__user_usergroup_map AS map ON map.group_id = g.id’)
->where(‘map.user_id = ‘.(int) $userid);
$db->setQuery($query);
$ugp = $db->loadObject();
$usertype = $ugp->group_name;
if(is_string($usertype)) $usertype = array($usertype);
if(in_array(‘Super Users’, $usertype)){
$isadmin = true;
}
//var_dump($usertype);
}
[/code]

thanks

  1. Unlike the response (now locked) on the Joomla Forum, this solution works, but I wonder if this method and the underlying reasoning is documented anywhere. Specifically, I have some users who are members of two different groups, and I’d like to find out if they are in a specific group before letting them modify content belonging to that group (i.e., the content with the group’s category). The $usertype array seems to reply with only one element, een if the logged-in user is a member of two or more groups.

    Thanks.

Comments are closed.