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

Listen

0 comments