Error with contentLib's setPermissions

Enonic version: 6.7.1
OS: OSX

Hey guys, Idk if this is an error or something that requires a bit more documentation, but I see myself unable to set my content’s permission via contentLib.setPermission. It’s returning true, but nothing happens. If I don’t publish the content afterwards it even set it as Out of date.

Also, even if I pass a wrong principal’s key, it returns true. So, something shouldn’t be right there.

Below is my function call:

contentLib.setPermissions({
key: id,
inheritPermissions: false,
overwriteChildPermissions: true
permissions: [{
principal: key,
allow: [‘CREATE’, ‘MODIFY’, ‘PUBLISH’],
deny: []
}]
});

Where id is my content id, and key is my group, like: group:testidprovider:content_1241 (also tested with users and roles from the system user store, not working as well).

Before calling the function I’ properly logged in to the system, via context, and it shouldnt be a credentials kind of problem.

Thanks!

A post was split to a new topic: Removing userstore with groups doesn’t work

Hi

There is a bug, when the principal is not found it still returns true as you say. So this make it more difficult to debug. We will fix this.

I did a similar test, and the permissions were updated for me:

    var content = contentLib.setPermissions({
        key: '025a6688-f8b1-4e86-a17b-df7c8fd13fcb',
        inheritPermissions: false,
        overwriteChildPermissions: true,
        permissions: [{
            principal: 'role:cms.admin',
            allow: ['CREATE', 'MODIFY', 'PUBLISH'],
            deny: []
        }]
    });

Can you double-check that the id variable has the expected value?

So, like I said, after it returns true, the content is set to out-of-date, so it is processed and something happens at it. But no permission is ever updated. I can update the object, set new data to it, but I can’t change its permissions.

I tried changing only the inheritPermissions and overwriteChildPermissions values, passing only the permissions array, adding a role, adding a user, adding a group, and nothing seems to work.

Ok. I think the problem is that you are doing the update (setPermission) on the master branch directly. While what you see in Content Studio is the draft branch.

It shows with status “out-of-date” because the version in master is newer than the one in draft. You should do the setPermissions in draft branch, and then publish the content changes to master.

I don’ see in the documentation the setpermission accepting any parameter to target a specific branch

http://repo.enonic.com/public/com/enonic/xp/docs/6.7.1/docs-6.7.1-libdoc.zip!/module-lib_xp_content.html#.setPermissions

That is true, setPermissions does not have a branch parameter like other content functions. But it will apply the changes in the current context branch, like the other content functions when branch is not specified.

You can set the context branch by wrapping the setPermissions call in a contextLib.run call:

var contentLib = require('/lib/xp/content');
var contextLib = require('/lib/xp/context');

var result = contextLib.run({
    branch: 'draft'
}, function () {

    return contentLib.setPermissions({
        key: '025a6688-f8b1-4e86-a17b-df7c8fd13fcb',
        inheritPermissions: false,
        overwriteChildPermissions: true,
        permissions: [{
            principal: 'role:cms.admin',
            allow: ['CREATE', 'MODIFY', 'PUBLISH'],
            deny: []
        }]
    });

});

If you don’t explicitly set the branch in context, it will depend on the URL where the controller is invoked.

http://repo.enonic.com/public/com/enonic/xp/docs/6.7.1/docs-6.7.1-libdoc.zip!/module-lib_xp_context.html#.run

Ok, I could make it work that way, thanks! But, do you see this wasn’t straightforward right? There’s no mentioning to anything in documentation, then maybe this could receive a branch directly? Or, I don’t know.

Also, would there be a way to just append a new permission? SetPermission completely replaces everything, and that’s not my intended behaviour. I don’t see why when having inheritPermissions set to true, it blocks me from having new permissions set to that object. So it isn’t exactly an inherit thing happening, because I’m unable to extend it

Yes, I see it is not obvious how it should be used. We will look into maybe adding the branch parameter and/or improve the documentation.

As it is now if you want to add or remove permissions you have to first call getPermissions, modify the list and call setPermissions again. We can also consider addPermission, removePermission functions.

2 Likes

Thanks for the response! Keep up the good work folks, really appreciated here.