XF 2.2 Equivalent to canViewAsGuest?

Nulumia

Well-known member
Seems like this one is right under the nose but couldn't find anything exact.

I'm wondering if there's an in-built function to check whether a content (thread, post, resource) is visible to guests, while logged as another usergroup, or neutrally checking without using \XF::visitor. So canView() wouldn't be applicable.

The following works:
Code:
$guestPermissionSet = \XF::permissionCache()->getPermissionSet(\XF\Repository\User::$guestPermissionCombinationId);
        
return $guestPermissionSet->hasContentPermission('node', $node->node_id, 'view');

However this involves getting the container entity, not as elegant.

Thanks for ideas.
 
There is \XF::asVisitor:

PHP:
$userRepo = \XF::repository('XF:User');
$visible = \XF::asVisitor($userRepo->getGuestUser(), function () use ($thread)
{
    return $thread->canView();
});

Though be mindful of permission checks causing queries. You may wish to call some methods on the permission cache to eager-load permissions prior to using \XF::asVisitor, depending on your uage.
 
There is \XF::asVisitor:

PHP:
$userRepo = \XF::repository('XF:User');
$visible = \XF::asVisitor($userRepo->getGuestUser(), function () use ($thread)
{
    return $thread->canView();
});

Though be mindful of permission checks causing queries. You may wish to call some methods on the permission cache to eager-load permissions prior to using \XF::asVisitor, depending on your uage.
Looks like that should be perfect thank you! I will look at bringing in the permission cache as you stated (y)
 
Top Bottom