joomlauser

Posted by & filed under Joomla, Tips and Tricks.

In Joomla1.5.x we can detect user type easily or check if a user is admin type.

    $isadmin = false;
    $user           =&JFactory::getUser();
    if($user->usertype == "Super Administrator" || $user->usertype == "Administrator"){
        $isadmin = true;
    } 

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

$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);
    }

thanks

One Response to “Detect User type in joomla 1.6 or later”

  1. Jim Brooking

    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.

Leave a Reply

  • (will not be published)