How to use SpacePermissionContext

I’m trying to set the permission on a space, using the non-deprecated method for doing so, but I´m unsure how to do so.

I create a SpacePermission object which works fine, when using the deprecated method, if I try to use the same SpacePermission together with a default SpacePermissionContext object I get an error (Cannot invoke method savePermission() on null object) which leads me to believe that I´m not creating the SpacePermissionContext correctly.

What am I doing wrong?

import com.atlassian.confluence.internal.security.SpacePermissionContext;
import com.atlassian.confluence.internal.security.SpacePermissionManagerInternal
import com.atlassian.confluence.security.SpacePermissionManager
import com.atlassian.confluence.internal.security.SpacePermissionSaverInternal
import com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.security.SpacePermissionSaver
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.confluence.security.SpacePermission
SpacePermissionManager spacePermissionManager = ComponentLocator.getComponent(SpacePermissionManager)
SpacePermissionSaverInternal spacePermissionSaveManagerInternal = ComponentLocator.getComponent(SpacePermissionSaverInternal)
SpacePermissionSaver spacePermissionSaveManager = ComponentLocator.getComponent(SpacePermissionSaver)
SpaceManager spaceManager = ComponentLocator.getComponent(SpaceManager)

Space space = spaceManager.getSpace("GATTS") 

SpacePermission spacePM = new SpacePermission("COMMENT", space, "all-internal-users")
//spacePM.setSpace(spaceManager.getSpace("GATTS"))
//spacePM = spacePM.createGroupSpacePermission("COMMENT", spaceManager.getSpace("GATTS") , "all-internal-users")

SpacePermissionContext context = SpacePermissionContext.createDefault()


spacePermissionSaveManagerInternal.savePermission(spacePM, context) //Cannot invoke method savePermission() on null object
//spacePermissionManager.savePermission(spacePM) //this works



1 Like

Hello @anders.lantz,

In creating SpacePermissionContext, try this one if it works for you

SpacePermissionContext context = SpacePermissionContext.builder().build()

Cheers,
Ian

Hi @anders.lantz,

I got the same problem, and it’s ComponentLocator.getComponent(SpacePermissionManager) that is returning null. Use space.addPermission instead and it will work.

1 Like

This post says that the Internal classes are not available to people outside of Atlassian.
However, the documentation says the interface is public.
I’m so confused. :confused:

1 Like

I tried to use space.addPermission, but since the com.atlassian.confluence.spaces.SpaceManager’s getSpace methods are deprecated, this runs into the same issue of trying to use a method that’s not deprecated.

You’re supposed to use com.atlassian.confluence.api.service.content.SpaceService now to get spaces, although it’s much more cumbersome and less straightforward.

The spaces for SpaceManager and SpaceService are totally different objects and do not mix. :frowning: