XF 2.1 Thread Finder using Guest Permissions

nrep

Well-known member
I'm coding a similar threads caching addon, but I'm struggling with a way to use the XF:Thread finder with guest permissions.

I want to use this: $finder = \XF::finder('XF:Thread'), but only return threads that would be viewable to guests. Is there a way I can do this? At the moment, the finder returns whatever the user viewing the page can see.
 
So far, the relevant part of the code looks like this:

PHP:
                $finder = \XF::finder('XF:Thread');
                $similarThreads = $finder
                    ->whereOr($conditions)
                    ->with('Forum.Node.Permissions|' . $visitor->permission_combination_id) #... Should this line check permissions?
                    ->order('post_date', 'DESC')
                    ->fetch();

From taking a look at other parts of XF, it looks like permission_combination_id can be used to check view permissions. I added ->with('Forum.Node.Permissions|' . $visitor->permission_combination_id) to the finder, as shown above, but it still returns threads that guests don't have permission to view.
 
That like just gets the permissions -- you still need to filter to viewable threads by calling filterViewable on the collection of results. That will always run in the context of the visitor though, so you'd need to change that to guarantee that it's a guest.

This is where \XF::asVisitor() comes into play. This temporarily changes the visitor to the specified one. You'd do the fetching and filtering within this function, returning the set of filtered threads. I'd recommend searching the core code for this to find some examples of it in use. The only other significant piece of the puzzle is getting the guest user record, and that's done via the User repository's getGuestUser method.
 
Top Bottom