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

canViewForum based on unregistered users?

Discussion in 'XenForo Development Discussions' started by Jaxel, Jan 2, 2011.

  1. Jaxel

    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?
     
  2. Indigo

    Indigo Active Member

    Try calling canViewForum like so:
    PHP:
    $guest    $this->getModelFromCache('XenForo_Model_User')->getVisitingGuestUser();
    $viewable $this->getModelFromCache('XenForo_Model_Forum')
        ->
    canViewForum($forum$errorPhraseKeynull$guest);
    The fourth parameter allows you to pass an array for any user you want, in this case a guest.
     
    Shadab likes this.
  3. Jaxel

    Jaxel Well-Known Member

    Okay... thats cool... what is $errorPhraseKey?
     
  4. Indigo

    Indigo Active Member

    $errorPhraseKey will be filled with the key of an error phrase, should the permission check fail (or any other error, presumably).
     
  5. Jaxel

    Jaxel Well-Known Member

    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.
     
  6. Jaxel

    Jaxel Well-Known Member

    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.
     
  7. Indigo

    Indigo Active Member

    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.
     
  8. Jaxel

    Jaxel Well-Known Member

    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.
     
  9. Indigo

    Indigo Active Member

    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).
     

Share This Page