• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

assertForumValidAndViewable

Valhalla

Well-known member
#1
Does anyone know if there is a similar function to assertForumValidAndViewable? I have had a look, but to no avail.

I was hoping to use it to check the status of a forum, but it throws an error if the forum is not valid and viewable, rather than returning nothing.

Ideally, it would *only* return an array if the forum was, and otherwise be null, but this is not the case.

My code doesn't function as expected because the error is generated as per the function, rather than giving a return of null.

I don't really want to copy and modify an entire function when it so nearly meets my needs.
 

Chris D

XenForo developer
Staff member
#2
That function:

1) Gets a forum from node name or ID if it exists, else error.
2) Gets a forum if you have permisison to view it, else error.
3) Returns the forum data.

Am I right you basically want to do:

1) Gets a forum from node name or ID if it exists, else nothing.
2) Returns the forum data.

Is that more or less right? Does it need to check permissions?
 

Valhalla

Well-known member
#3
Yes, I'm trying to check a user has permission to access the forum.

I don't actually need to return the data, however.

(It's actually to check if they have permission to view the threads in the forum.)

For threads themselves, I'm using "permissionCombinationId", which works fine to return only viewable threads, but this problem is when checking access to a forum.

To be specific, I'm using the code below to account for any changes in forum permissions once the data has been already (previously) committed to database. I'm trying to remove any entries that might be "superfluous" now if the permissions have changed - if that makes sense.

PHP:
$list = $multiviewModel->getForumsAdded($userId);
  
foreach ($list AS $key => $item)
{
    $ftpHelper = $this->getHelper('ForumThreadPost');
    $forum = $ftpHelper->assertForumValidAndViewable($item['node_id']);

    if (!$forum)
    {
        unset($list[$key]);
    }
}
$list is just: node_id, title, description, message_count and discussion_count.
 
Last edited:

Chris D

XenForo developer
Staff member
#4
Yeah, I would forget the helper, it's not necessary.

The code is simple enough:

PHP:
            $fetchOptions += array('permissionCombinationId' => $visitor->permission_combination_id);

            $forum = $forumModel->getForumById(
                $forumIdOrName, $fetchOptions
            );

            $nodePermissions = XenForo_Permission::unserializePermissions($forum['node_permission_cache'])

            if ($forumModel->canViewForum($forum, $null, $nodePermissions))
            {
                // User can view forum
            }
That's the basic premise. You will need to define a few of the variables yourself, of course, and it assumes you already have an array of $fetchOptions configured how you like, but hopefully you get the idea.
 

AndyB

Well-known member
#5
Another way is to modify your model by adding a $whereclause to the query. Here's how you create the $whereclause:

PHP:
		// get node list
		$viewableNodes = $this->getModelFromCache('XenForo_Model_Node')->getViewableNodeList();
		
		// get $nodeIds
		foreach ($viewableNodes as $node)
		{
			$nodeIds[] = $node['node_id'];
		}
		
		// create whereclause of viewable nodes
		$whereclause = 'AND (xf_thread.node_id = ' . implode(' OR xf_thread.node_id = ', $nodeIds);
		$whereclause = $whereclause . ')';