MyBB Community Forums

Full Version: Having a bit of a problem with $post and permissions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Basically, I'm writing proper user permissions into my mystatus plugin and am having a problem when it comes to displaying statuses in posts. I have a permission that allows the blocking of using statuses at all on a usergroup basis. That's fine and it works everywhere else apart from in the postbit. What I want to do is hide the contents of $post['mystatus'] should the user's usergroup be "banned" from using statuses.

Here's my current code for this:

	$canusemystatus = $db->simple_select('usergroups', 'canusemystatus', 'gid = ' . $post['usergroup']);
	$canusemystatus = $db->fetch_field($canusemystatus, 'canusemystatus');
	
	if ($mybb->settings['mystatusenable'] == 1 && $canusemystatus == 1) {
		//grabbing the current status
		$currentstatus =  $db->query("SELECT `mystatus` FROM `".TABLE_PREFIX."users` WHERE `uid` = ".$post['uid'].";"); 
		$currentstatus = $db->fetch_field($currentstatus, "mystatus");
		

		$post['mystatus'] = '<span class="smalltext">'.$currentstatus.'</span>';

		//status editing
		if ($mybb->user['uid'] == $post['uid'] || $mybb->usergroup['canmodstatuses'] != 0) {
			$post['mystatus'] = '<span class="smalltext post-mystatus-'.$post['pid'].'">'.$currentstatus.' <a href="#" onclick="MyBB.popupWindow(\''.$mybb->settings['bburl'].'/process-mystatus.php?action=postbitedit&updateuid='.$post['uid'].'\', \'MyStatus Update\', 350, 115);">Edit</a></span>';
		}

		return $post;
	}	

The editing part works absolutely perfect, but the current status displays no matter what. However, "banning" a group from statuses whilst they have edit permissions hides the edit link... weird.

Anybody got any ideas what I'm doing wrong? it's probably something really simple, but I cant see it atm haha
Did you make $post as a global variable in your function?

FYI, you should have to use the function like this;

function FUNCION_NAME($post) {
global $db, $mybb, $templates;
// your code goes here
}
Yes, I did;

function mystatus_postbit($post)
{
	global $mybb, $theme, $templates, $currentstatus, $db;

As I said though, the status outputs fine and anything, my permission check just doesn't quite seem to be working as I expected.
Hang on a minute, you've got two queries for something that is already available to you?? If the user has 50 posts per page, like I do, that's going to be 100 totally unnecessary queries. I think will work fine:

<?php
function mystatus_postbit(&$post)
{
	global $mybb, $theme, $templates, $cache; 
	// reset it first
	// whatever $post['mystatus'] is, even if it's empty, will be referred to as $currentstatus for now
	$currentstatus = $post['mystatus'];
	$usergroups_cache = $cache->read("usergroups");
	if($mybb->settings['mystatusenable'] == 1 && $usergroups_cache[$post['usergroup']]['canusemystatus'] == 1)
	{
		// now set $post['mystatus'] to display the $currentstatus
		$post['mystatus'] = '<span class="smalltext">'.$currentstatus.'</span>';
		
		//status editing
		if($mybb->user['uid'] == $post['uid'] || $mybb->usergroup['canmodstatuses'] != 0)
		{
			// here we can just use $post['mystatus']
			$post['mystatus'] = '<span class="smalltext post-mystatus-'.$post['pid'].'">'.$post['mystatus'].' <a href="#" onclick="MyBB.popupWindow(\''.$mybb->settings['bburl'].'/process-mystatus.php?action=postbitedit&updateuid='.$post['uid'].'\', \'MyStatus Update\', 350, 115);">Edit</a></span>';
		}
	}
}
?>

Your use of $post['mystatus'] and $currentstatus didn't make entire sense to me, changed that around a bit, as you'll need to reset the value of it at the start each time.

$usergroups_cache may even already be available for you to globalise and use, I've not checked. Queries should never be performed in a function that is in a loop, like in the postbit, especially when one thing is already in the $post array and one can be checked via the cache.
Oh. Thanks matt, I didn't realise that at all Wink I didn't even stop and think about what $post would be returning - but it's obviously just everything haha. That also solves my query about why $post['mystatus'] wasnt empty.

One question, however, is how often do the usergroup caches get emptied/updated? They seem to take quite a while as I just tried toggling some settings and they haven't changed back haha