MyBB Community Forums

Full Version: Forum permissions table improvement.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm currently writing a plugin where I had to fetch the permissions of the groups from forumpermissions table and do particular action only if they had access to that forum.

I'm coming to know that permissions for each usergroup per each forum isn't saved there by default (meaning no custom perms). If so is there any way still I can do this? Because I don't think so that without saving permissions of each usergroup for each forum separately, I would get this to work.

So my idea (if this cannot be done by any odd 'hack') was to add group permissions for each usergroup for each forum on a fresh install/upgrade and when creating a new forum, because that would be great.

Here I cannot check/verify if a user has access to that forum.
Pretty sure permissions are stored in the cache?
Yes, but I have to fetch and look for custom permissions as well and it makes one simple thing too much complex.

So it would be great if all was stored in one place, custom & default, both perms.

Right now I need to figure some other way to do it or leave it as is.
(2012-10-06, 07:49 PM)euantor Wrote: [ -> ]Pretty sure permissions are stored in the cache?

What he said.

You should use stored cache. MyBB will take the forumpermissions table and turn it into the cache. If you know what you're doing you can achieve a great deal from that. You're asking for improvements on what is already an excellent system when used by someone who knows how.
Ah, as I said I didn't know much of this, will take a look at forum perm's cache (as I thought custom perms weren't cached), thank you.
just use the built-in functions

usergroup_permissions( ) and fetch_forum_permissions( )

these dont use queries and together will let you get what you need.

here is an example of how I use those functions

		$forums2send = explode(",", $mybb->settings['mybbpublisher_forums']);
		$groupperms = usergroup_permissions('1,2');
		$forumcache = $cache->read('forums');
		
		$forums_can_publish = array();
		foreach($forumcache as $fid => $forum)
		{
			if($forum['type'] == 'f' && $forum['open'] == 1 && $forum['active'] == 1 && $forum['password'] == '')
			{
				$forumpermissions = fetch_forum_permissions($fid, '1,2', $groupperms);
				if($forumpermissions['canview'] == 1 && $forumpermissions['canviewthreads'] == 1)
				{
					if($mybb->settings['mybbpublisher_forums'] != "0" && $mybb->settings['mybbpublisher_forums'] != "")
					{
						if((in_array($fid, $forums2send) && $mybb->settings['mybbpublisher_how'] == 'include') || (!in_array($fid, $forums2send) && $mybb->settings['mybbpublisher_how'] == 'exclude'))
						{
							$forums_can_publish[] = $fid;
						}
					}
					else
					{
						if($mybb->settings['mybbpublisher_how'] == 'include')
						{
							$forums_can_publish[] = $fid;
						}
					}
				}
			}
		}
Okay I may need some help.

Before explaining, mods can you please move to plugin dev forum? As this is not a suggestion anymore.

The problem is, I can fetch permissions now as I came to know how it functions using something like this:

$forumpermissions = forum_permissions();
$fpermissions = $forumpermissions[$fid];

And then I fetch perms for individual columns like:

$canview = $fpermissions['canview'];

Correct me if this would be wrong.

But then, what I do is (the same plugin that I was asking for PM uids assistance) first query the plugin table of mine, fetch number of uids from it.

Then I make them array, like:

$uids[] = $person['uid'];

And then comes the part where I need to check forum permissions for each uid/user.

Again, not a tricky one. But I'm stuck on how would I build a list of new users (like $uids[] array) after checking forum perms for them to do something only if those users has access to forum.

Help is appreciated.
If I understand correctly you will need to mass get the forum permissions:
$query = $db->simple_select('users', 'username, uid, usergroup, additionalgroups', "uid IN (".implode(',', array_unique(array_map('intval', $uids))).")");
$user_cache = $new_uids = array();
$forumcache = $cache->read('forums');
while($user = $db->fetch_array($query))
{
	if(!isset($user_cache[$user['uid']]))
	{
		$user_cache[$user['uid']]['groups'] = $user['usergroup'].($user['additionalgroups'] ? $user['additionalgroups'] : '');
		$user_cache[$user['uid']]['groupperms'] = usergroup_permissions($user_cache[$user['uid']]['groups']);
		$user_cache[$user['uid']]['forumpermissions'] = fetch_forum_permissions($FID, $user_cache[$user['uid']]['groups'], $user_cache[$user['uid']]['groupperms']);
	}
	if(isset($user_cache[$user['uid']]['forumpermissions']['canview']) && $user_cache[$user['uid']]['forumpermissions']['canview'] && isset($user_cache[$user['uid']]['forumpermissions']['canviewthreads']) && $user_cache[$user['uid']]['forumpermissions']['canviewthreads'])
	{
		//user can view forum and can view threads, do something.
		$new_uids[(int)$user['uid']] = $user['username'];
	}
}
unset($user_cache);

foreach($new_uids as $uid => $username)
{
	send_pm($uid, 'Hi '.$username);
}

The send_pm(); is not a real function.