canViewForum based on unregistered users?

Jaxel

Well-known member
In my sitemap generator I am using the following in a CRONJOB:
Code:
	foreach ($forums AS $forum)
	{
		if ($this->getModelFromCache('XenForo_Model_Forum')->canViewForum($forum))
		{
			stuff...
		}
	}
I figured since this was in a cronjob, it would assume the viewing user would be an unregistered user. Unfortunately, it doesn't seem to work this way and I think it marks the viewing user as an administrator. Naturally I don't want to index private forums into my sitemap, because its pointless and search engines can't reach it anyways.

How would I check for canViewForum based on an unregistered user?
 
Try calling canViewForum like so:
PHP:
$guest    = $this->getModelFromCache('XenForo_Model_User')->getVisitingGuestUser();
$viewable = $this->getModelFromCache('XenForo_Model_Forum')
    ->canViewForum($forum, $errorPhraseKey, null, $guest);
The fourth parameter allows you to pass an array for any user you want, in this case a guest.
 
Try calling canViewForum like so:
PHP:
$guest    = $this->getModelFromCache('XenForo_Model_User')->getVisitingGuestUser();
$viewable = $this->getModelFromCache('XenForo_Model_Forum')
    ->canViewForum($forum, $errorPhraseKey, null, $guest);
The fourth parameter allows you to pass an array for any user you want, in this case a guest.
Okay... thats cool... what is $errorPhraseKey?
 
$errorPhraseKey will be filled with the key of an error phrase, should the permission check fail (or any other error, presumably).
 
Okay... I am running the following code:
Code:
$forums = $this->getModelFromCache('XenForo_Model_Forum')->getForums();
$guests = $this->getModelFromCache('XenForo_Model_User')->getVisitingGuestUser();

foreach ($forums AS $forum)
{
	if ($this->getModelFromCache('XenForo_Model_Forum')->canViewForum($forum, $errorPhraseKey, null, $guests))
	{
		echo XenForo_Link::buildPublicLink('canonical:forums', $forum)."<br />";
	}
}

Unfortunately, this is not working. No matter what it keeps printing out all of my forums, event the private ones.
 
Okay... I came up with another solution... I went back to my old canViewForum($forum) code...
and simply added the following line...
Code:
XenForo_Visitor::setup(0);
Adding that line simply ran the cronjob as if it was being run by a guest.
Then all guest permissions were AUTOMAGICALY applied.
 
I figured that would have worked fine, I just didn't want to suggest it since that overwrites the singleton instance of XenForo_Visitor with a guest. Not sure if there would be any side effects to that method when running a cron via the Admin CP.
 
I figured that would have worked fine, I just didn't want to suggest it since that overwrites the singleton instance of XenForo_Visitor with a guest. Not sure if there would be any side effects to that method when running a cron via the Admin CP.
Well I could understand it being an issue normally... but as a cron job, its being run by the server, not by a user... so it shouldn't affect anything.
 
Well, technically, it's not a "real" cron job, in the sense that the server is executing it via crond or something. The jobs are being executed in the context of the web server, either by running it manually through the Admin CP, or through an empty cron image gif served on the frontend (via cron.php).
 
Top Bottom