MyBB Community Forums

Full Version: Adding function is_membergroup
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello there,

I think that a small function is missing in MyBB, I called it "is_membergroup". This is really similar to native is_member() but don't query for user, the user groups are arguments of the function.

why ? I made a plugin in which I list users and I just want the members of authorized groups. Each use of is_member() makes a query select * from mybb_users where uid=<id>. But as I already get the interresting datas about all users (including their groups), I think it's better to just compare what I have with what I want, without other queries.

/**
 * Checks if usergoup ou additionalgroups are in groups
 * @param array|int|string $groups A selection of groups (as array or comma separated) to check or -1 for any group
 * @param int $usergroup the main group of an user
 * @param string $additionalgroups A comma separated list of additional groups
 * @return array Matching groups
 */
function is_membergroup($groups, $usergroup=0, $additionalgroups='') {
 $memberships = array_map('intval', explode(',', $additionalgroups));
 $memberships[] = $usergroup;
 if(!is_array($groups)) {
        if((int)$groups == -1) {
            return $memberships;
        } else {
            if(is_string($groups)) {
                $groups = explode(',', $groups);
            } else {
                $groups = (array)$groups;
            }
        }
    }
    $groups = array_filter(array_map('intval', $groups));
    return array_intersect($groups, $memberships);
}
is_member($groups, array('usergroup' =>  $usergroup, 'additionalgroups' =>  $additionalgroups))

I even use it for forums:
is_member($forums, array('usergroup' =>  $fid))
I don't really understand what you mean. Can you explain a little bit more plz ?
(2020-06-12, 06:51 AM)Crazycat Wrote: [ -> ]This is really similar to native is_member() but don't query for user, the user groups are arguments of the function.

is_member() doesn't need to query for the user at all as you can pass the user data you want it to use.

What you are requesting is achievable with the code I shared above.
while($user = $db->fetch_array($query))
{
 if(!is_member($groups, $user))
 {
 continue;
 }
}

I use is_member() to check forum permissions as well.
https://github.com/Sama34/OUGC-Necro-Pos...e.php#L253
Oh wait... I understand my error, I was just using $uid as argument.
Reading better the code... if $user is an array containing usergroup and aditionnalgroups as keys, get_user() is no more called.

Thanks opening my eyes Smile