Set Page Restrictions in Confluence macros

I create my macro and I try to set page permission.
During the macro insertion, the restrictions are set in the EDIT + VIEW (Why? I ONLY WANTED “VIEW”!!!), after the page is published I get an error.

Here is my simple code in the execute() method:

ContentEntityObject contentEntityObject = conversionContext.getEntity();
ContentPermission permission = ContentPermission
                .createGroupPermission(ContentPermission.VIEW_PERMISSION, "testspace_k3");
        contentPermissionManager.addContentPermission(permission, pageManager.getPage(contentEntityObject.getId()));

There are entries in the log:

– url: /display/testspace/test | page: 3080260 | traceId: 68dac98d21017ece | userName: admin | referer: http://localhost:8090/pages/resumedraft.action?draftId=3080260&draftShareId=006aa5fe-759e-4152-9813-451632c3f721& | action: viewpage
->[null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT (Session #1614232119)
2019-07-27 21:55:04,425 WARN [http-nio-8090-exec-6] [confluence.impl.hibernate.ConfluenceHibernateTransactionManager] doRollback Performing rollback. Transactions:
java.lang.NullPointerException
– url: /display/testspace/test | page: 3080260 | traceId: 68dac98d21017ece | userName: admin | referer: http://localhost:8090/pages/resumedraft.action?draftId=3080260&draftShareId=006aa5fe-759e-4152-9813-451632c3f721& | action: viewpage
2019-07-27 21:55:04,422 ERROR [http-nio-8090-exec-6] [xhtml.view.macro.ViewMacroMarshaller] handleMacroExecutionException Error rendering macro: Test Macro
– url: /display/testspace/test | page: 3080260 | traceId: 76be1a2499db2279 | userName: admin | referer: http://localhost:8090/pages/resumedraft.action?draftId=3080260&draftShareId=006aa5fe-759e-4152-9813-451632c3f721& | action: viewpage
->[null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT (Session #1309099359)
2019-07-27 21:55:03,710 WARN [http-nio-8090-exec-4] [confluence.impl.hibernate.ConfluenceHibernateTransactionManager] doRollback Performing rollback. Transactions:
java.lang.NullPointerException
– url: /display/testspace/test | page: 3080260 | traceId: 76be1a2499db2279 | userName: admin | referer: http://localhost:8090/pages/resumedraft.action?draftId=3080260&draftShareId=006aa5fe-759e-4152-9813-451632c3f721& | action: viewpage
2019-07-27 21:55:03,707 ERROR [http-nio-8090-exec-4] [xhtml.view.macro.ViewMacroMarshaller] handleMacroExecutionException Error rendering macro: Test Macro

it doesn’t work either:

activeObjects.executeInTransaction(() -> {
            ContentPermission permission = ContentPermission
                    .createGroupPermission(ContentPermission.VIEW_PERMISSION, "testspace_k3");
            contentPermissionManager.addContentPermission(permission, pageManager.getPage(contentEntityObject.getId()));
            return null;

        });

What is the correct way to set page restrictions programmatically?

Hi Alexey

did you get anywhere? We have a very similar issue…

Stacktrace Attached.
Part 1 occurs multiple times, ~ for every permission we try to set after the actual methods are executed
Part2 occurs just once right at the end

Does anyone have any advice in general how we set permissions using the APIs in Confluence (server/datacenter)

We try to do it like this (after we delete all current permissions on the page):

final ContentPermission adminViewPerm = ContentPermission.createGroupPermission(ContentPermission.VIEW_PERMISSION,
        PermissionsManager.ADMINS);
contentPermissionManager.addContentPermission(
        adminViewPerm,
        page);

Stack Trace Part 1 and 2 attached as PDF as Txt not allowed
StacktracePart1.pdf (181.3 KB) StacktracePart2.pdf (167.7 KB)

Hi,

Saving the page after clearing all permissions on that same page (before adding new permissions) solved this problem,

hope this helps.