Overview

There may come a day where you need to script the migration of permissions from one Confluence Space to another.

The permissions of a Confluence Space can be retrieved and treated as collection of objects.  This allows us to easily pass them on to a source page as a new set of permissions.

The Code

We’re using the Soap Service to affect change in the permissions of a Space.  After using the ComponentLocator to declare the SpaceSoapService, we retrieve the source Space as an object.  We do the same for the destination source.

The permissions of the source Space are then extracted.  This is not a single object, but rather a collection of objects.

We iterate through each of the permission objects.  Each object is a collection of attributes.  We need to determine if the permissions object relates to a single user, or a group.  Every type of permission gets it’s own object.

Permissions objects associated with a group look like so:

[CREATEATTACHMENT,89948111,confluence-space-admins,null,null]

The first attribute is the permission type.  The second is the permission ID. The third element is the group with which this permission is associated, and this is where the format of the permissions object differs in comparison to the user-oriented permissions object.

[REMOVEBLOG,89948111,null,JSMITH,null]

As you can see, if the permissions object is for a user, the username appears as the fourth element.

We need to account for this in our code.

It’s easy enough to test for a group or user permission; if the username or group is null, the object is clearly for a group or user respectively.

This matters because we need to know whether to retrieve the username or the group name when setting the permission on the destination page.

Whether we detect a group or user permission, the rest of the process is straightforward. We declare the three elements required to add permissions to a Confluence Space: permission type, user or group name, and target Space Key.   The addPermissionsToSpace method of the  SpacesSoapService is invoked, and the three elements are provided to it. The loop iterates through each permission retrieved from the Source Space.

 

import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.rpc.soap.services.SpacesSoapService
import com.atlassian.sal.api.component.ComponentLocator

def addSpacePermission = ComponentLocator.getComponent(SpacesSoapService)
//Define the soap service, with which we will add the permissions to the target Space

def String[] permissions = [""] 
def String remoteEntity = ""
def String spaceKey = ""
//Define the three strings that hold the permissions attributes

def sourceSpace = ComponentLocator.getComponent(SpaceManager).getSpace("<SpaceKey>")
//Define a source Space object

def destinationSpace = ComponentLocator.getComponent(SpaceManager).getSpace("<SpaceKey>")
//Define a destination Space object

def sourcePermissions = sourceSpace.getPermissions()
//Grab the permissions from the source Space, so that we can apply them to the destination Space

sourcePermissions.each{perms -> 
//For each permissions object that the source space contains   
    
    if(perms.getGroup() == null){
        //If the group name field in the permissions object is null, this permission must be for a user
                     
        permissions = [perms.getType()] 
        remoteEntity = perms.getUserSubject().getName()
        spaceKey = destinationSpace.getKey()
        //Define the three permissions attributes that must be passed to the destination Space
        
        addSpacePermission.addPermissionsToSpace(permissions, remoteEntity, spaceKey)
        //Add the permission to the destination Space
        
        
    }else if(perms.getUserSubject() == null){
        //If the user name field in the permissions object is null, this permission must be for a group
                
        permissions = [perms.getType()] 
        remoteEntity = perms.getGroup()
        spaceKey = destinationSpace.getKey()
        //Define the three permissions attributes that must be passed to the destination Space
        //Note that the remoteEntity has changed, as we're now fetching the group name instead of the user name
        
        addSpacePermission.addPermissionsToSpace(permissions, remoteEntity, spaceKey)
        //Add the permission to the destination Space
                
    }
}
    
    

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>