MyBB Community Forums

Full Version: Function for checking against a users group and additional groups.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Here is a small but very useful function for checking against a users usergroup and additionalgroup. Great for exemptions or inclusive settings.

function checkgroup($groups)
 {
   global $mybb;
	if($mybb->user['additionalgroups'])
		$grouparray = explode(",",$mybb->user['additionalgroups']);
    $grouparray[] = $mybb->user['usergroup'];

    foreach ($groups as $group) { 
        if ( in_array($group, $grouparray) ) { 
            return true; 
        } 
    } 
    return false; 
 }
 

You would use it in the case of a setting. It returns a true for a match or false if no match.

Example:
Groups Exempt: 3,4,6

In your plugin you would do this:

$exempts = explode(",", $mybb->settings['gexempt']);
if (checkgroup($exempts))
{
//do action that is allowed
}

Essentially this makes plugins more efficient for group checking. The traditional method only checks for usergroup and not additionalgroups. I don't know why MyBB doesn't have something like this already built in somewhere but I never found it.

Hope this helps some plugin authors.

Enjoy.
Damn, this comes directly to the right time!
I just got a request for a plugin where I do need additional group implementation and was about to look how they are done and you did it for me - thanks a lot Smile
You have no idea how long I needed something like this and I finally spent the time to do it and get it right. Glad it can be of some use. I am going to share more of my tricks. If I leave the community I hope to at least pass on what I know so other authors can improve their plugins. We should have some standards here. I see too many crap plugins being released.
There is a way in MyBB to check against a user's displaygroup and usergroup, just not just in the same function like yours.

The usergroup_permissions function can provide the permissions for a usergroup. The usergroup_displaygroup function can be used to grab permissions as a displaygroup. This function has a cool feature. For example, if you need just a few fields to check against, you can define them in an array to query. The array needs to be called $displaygroupfields, as it's used as a global variable. Example usage (to fix your usereputationsystem bug):

// Grab the following fields from the user's displaygroup
$displaygroupfields = array(
	"title"
	"usertitle"
	"stars"
	"starimage"
	"image"
	"usereputationsystem"
);
$displaygroup = usergroup_displaygroup($memprofile['displaygroup']);

if($displaygroup['usereputationsystem'] == 0)
{
	// User's displaygroup can't use the reputation system
}

I guess this is the way the MyBB functions would cirum the problem. (Apologies if you already knew this, but you didn't point it out Toungue)
Yes but that doesn't check if a user is in a group. For that to work you would need to assign a new group permission. TBH I am not even sure how to go about that.

Other than the admin side that function is only used in inc/sessions.php. I can't see how it would be used to check based on a setting parameter.

Hmm...I will look into adding an actual Group Permission though. It would imho be better than the currently used method of a setting seperated by commas. At least if it's doable.

If you have the code to add that let me know. It would make all our lives easier. Looks like a hook into admin_user_groups_begin would be required with key added for $usergroup_permissions array. I would have to write something and do testing.

Thanks Tomm.
(2009-08-30, 09:13 PM)labrocca Wrote: [ -> ]You have no idea how long I needed something like this and I finally spent the time to do it and get it right. Glad it can be of some use. I am going to share more of my tricks. If I leave the community I hope to at least pass on what I know so other authors can improve their plugins. We should have some standards here. I see too many crap plugins being released.

You do plan to leave? That would be sad.
Thanks for sharing your stuff, I think MyBB staff should post some of them into the wiki as additional info or move your threads to the tutorial section - they might get lost here in development otherwise.

Best regards to you Jesse
Lennart Sauter
i was thinking that too if jesse will leave mybb world! Sad we hope we can get in touch in you in the future if we or anyone need your support for any mybb tweaks you can share with us.. Smile im thanking jesse too..
labrocca Wrote:If I leave the community

Sad . We miss you really much.
This is what I use:
/* Checks if the primary or any of the additional groups of the current user are in the groups list passed as a parameter
 * @param String group ids seperated by a comma
 * @return true if the user has permissions, false if not
*/
function pluginname_check_permissions($groups_comma)
{
	global $mybb;
	
	if ($groups_comma == '')
		return false;
	
	$groups = explode(",", $groups_comma);
	$add_groups = explode(",", $mybb->user['additionalgroups']);
	
	if (!in_array($mybb->user['usergroup'], $groups)) { // primary user group not allowed
		// check additional groups
		if ($add_groups) {
			if (count(array_intersect($add_groups, $groups)) == 0)
				return false;
			else
				return true;
		}
		else 
			return false;
	}
	else
		return true;
}
Nice one aswell...
Pages: 1 2