XF 2.0 hasNodePermission bug or I am doing something wrong ?

XFA

Well-known member
Hello,

So in one of the add-on I am developing, I have extended the class XF\Entity\Category to add a new function :
PHP:
    public function canViewSeoExtension()
    {
        return \XF::visitor()->hasNodePermission($this->node_id, 'viewSeoExtension');
    }

Whatever the permission value set globally or for the corresponding category, this function always return 0 when called from a template in the category_view page.

So I tried and did something similar for forum entity and called it on forum_view page, no more luck.

I did some digging/debug and tracked down in xF code where the issue could be.
My findings is that on those pages, the contentPerms array in XF\PermissionCache only contains the "view" permission index but that's all.
While it should in my opinion contain more than that. I tried digging further by couldn't figure out/

I am tending to think it might be a bug but wanted to see first if I am not doing something wrong, @Chris D or @Mike you might be the more accurate to answer.

Clément
 
Looks like this does not work on categories. From User Entity:
PHP:
    public function hasNodePermission($contentId, $permission)
    {
        return $this->PermissionSet->hasContentPermission('node', $contentId, $permission); // 'node' is the important part
    }
I'm not sure, though, why you would want to do that. AFAIK categories are just like headings?
 
Categories are a type of node though, so I would think passing through the node ID of the category to that method should work as expected.
 
Doesn't seem to be the case. It's either intentional because, as I said, categories are just like headings (right?) or it's like as @XFA said and it's a bug because category permissions never get cached, thus never have any impact, although you can set them.
Would need to track down where permission cache actually is built.
 
Well yes they surely are like headings but my customer needs modifications to those page and permission based :)

Anyways yes for it seems like a bug and as I said had same issue on the forum view page.

Hence why I asked @Chris D and @Mike to have a design advice before I open a bug report.
 
Ah I see what you mean, I suppose they would be taken into account when building the permission cache for their children but I guess there's not much of a reason to build them for the categories themselves. Not sure then.
 
Just had a closer look, each node type is tied to a specific permission group. Permissions in the 'forum' group will only be set for forum nodes. You'd have to create a permission in the 'category' group for it to be set on category nodes. If you want it to be available for all nodes, create a permission in the 'general' group and extend \XF\Permission\NodePermissions::isValidPermission() with something like:

PHP:
if (
    ($permission->permission_group_id == 'general')
    && ($permission->permission_id == 'yourPermission')
) {
    return true;
}

return parent::isValidPermission($permission);
 
Just had a closer look, each node type is tied to a specific permission group. Permissions in the 'forum' group will only be set for forum nodes. You'd have to create a permission in the 'category' group for it to be set on category nodes. If you want it to be available for all nodes, create a permission in the 'general' group and extend \XF\Permission\NodePermissions::isValidPermission() with something like:

PHP:
if (
    ($permission->permission_group_id == 'general')
    && ($permission->permission_id == 'yourPermission')
) {
    return true;
}

return parent::isValidPermission($permission);

The permission would not be customizable per forum if doing so.
 
In fact yes you might be right, I looked at the code further and found out that the forum category doesn't apply to category or page node type.

But it's troubling as it display when you edit node permissions...

Will try your suggestion, seems to be ther right way
 
It worked, though extension is not handy as they did not provide way to easily add a permission similarly to the view one.
 
But it's troubling as it display when you edit node permissions...
Forum permissions display when editing categories so that you can apply the permissions to all forum children of the category (since permissions cascade down the tree).

It worked, though extension is not handy as they did not provide way to easily add a permission similarly to the view one.
The view one is actually included in exactly the same way (as a special case inside that method). It might be a little nicer if there was just a method that returns an array of common permissions to include ($group => $permissions) for extensibility purposes, but I suppose it's probably a fairly rare thing and extending that method isn't too bad.
 
  • Like
Reactions: XFA
Back
Top Bottom