MyBB Community Forums

Full Version: Working with user groups
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!

Quick question, how can I create or delete a user group from a plugin and how do I check if the current user is in this group?

Thanks in advance!

Murra_B
You can add or delete a usergroup with a plugin by running a query against the mybb_usergroups table. Note that in your code replace mybb_ with the constant TABLE_PREFIX. After doing that, you will want to update the usergroups cache like this:
$cache->update_usergroups();

Make sure you globalize the object $cache or you will get an error.

For checking if a user is in a group you can use this:
$usergroups = $mybb->user['usergroup'];
if($mybb->user['additionalgroups'])
{
$usergroups .= "," . $mybb->user['additionalgroups'];
}
$exusergroups = explode(",", $usergroups);
$ingroup = false;
foreach($exusergroups as $usergroup)
{
if($usergroup == 4) // Replace 4 with the id of the group they need to be.
{
$ingroup = true;
continue;
}
}
if($ingroup)
{
// Do stuff here
}
Thank you, works great! We are now able to give special groups special rights from the normal admin panel, that makes our forum feel more like ONE piece of software.