• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Check usergroup membership outside templates

Mouth

Well-known member
#1
Hi,

Trying to check if a user is a member of a usergroup, and perform an action (deny access) if they are not, within PHP outside of XF templates. Here's the code ....

PHP:
$mouthUser = $userModel->getUserById($userId);
$mouthAllow = array(3,4,17,29);
$mouthErrorMessage = "Sorry, only specific usergroups are able to access";
//if(!in_array($mouthUser['user_group_id'], $mouthAllow))
if(!$mouthUser->isMemberOf($mouthAllow))
return xmlresperror($mouthErrorMessage);
You can see I commented out
PHP:
if(!in_array($mouthUser['user_group_id'], $mouthAllow))
because it only checked on the users primary usergroup, and not also any secondary usergroups.

PHP:
if(!$mouthUser->isMemberOf($mouthAllow))
throws an error and does not work.

Would appreciate some assistance with what I should be doing here?

Thanks.
 
A

account8226

Guest
#2
PHP:
$visitor = XenForo_Visitor::getInstance(); //get the actula visitor instance, that's appropriate
 
$user_group_id = array(3,14,17,29); //usergroups you want to allow
if(!$visitor->isMemberOf($user_group_id)) {
  [...] //return error
}
This should do the job. By default, the isMemberOf method has a secondary argument (boolean, true by default) that allow secondary usergroups checking.

So using :
PHP:
$visitor->isMemberOf($user_group_id, false)
Would only check primary usergroups.
 

Mouth

Well-known member
#3
This should do the job.
Thanks, I also tried that a couple of days ago but it didn't work either.

As of last night, here's what seems to be working ..

PHP:
$mouthUser = $userModel->getUserById($userId);
$mouthAllow = array(3,4,17,29);
$allow_in = false;
if($mouthUser['secondary_group_ids'])
{
    $secondary_groups = explode(",", $mouthUser['secondary_group_ids']);
    foreach($secondary_groups as $secondary_group_id)
        if(in_array($secondary_group_id, $mouthAllow))
            $allow_in = true;
}
$mouthErrorMessage = "Sorry, only specific user groups are allowed to access";
if(!$allow_in) {
        return xmlresperror($mouthErrorMessage);
}
 

Chris D

XenForo developer
Staff member
#4
Wouldn't it be so much easier if you could just use {xen} tag syntax in PHP?

Well, you can... sort of...

PHP:
		$mouthUser = $userModel->getUserById($userId);
		$mouthAllow = array(3,4,17,29);
				
		$memberOf = XenForo_Template_Helper_Core::helperIsMemberOf($mouthUser, $mouthAllow);
		
		if (!$memberOf)
		{
			return xmlresperror("Sorry, only specific user groups are allowed to access");
		}
Now, of course, there's multiple ways of doing things. But if you're familiar with XenForo template syntax then these helper functions are invaluable simply due to the fact they're so familiar :)
 

Mouth

Well-known member
#5
Wouldn't it be so much easier if you could just use {xen} tag syntax in PHP?

Yes, I'm sure.
Not even sure if the XenForo_Template_Helper_Core::helperIsMemberOf is available in the external php in use in this situation.
I am/was trying to assist and encourage a developer to put functionality I strongly desire into an addOn. Seems to be working as per my previous post, so I'm happy ;)
I'm sure developers will appreciate your post giving the easy way though if they are searching in the future :)

Thanks.