Overview

This piece of code does several things. It returns all of the Keys for all of the Spaces in Confluence.  For each Space, it retrieves the associated categories (labels). For those Spaces with a certain category or label, it then performs some permissions management.

 

Space Key Retrieval

Let’s start with retrieving all of the Keys.  This actually starts with retrieving all of the information about all of the Spaces, with spaceManager.getAllSpaces().  Of course before we do that, we need to build the structure of the program.  Here’s the bare minimum required to work with getAllSpaces():

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.spaces.SpaceManager

def spaceManager = ComponentLocator.getComponent(SpaceManager)

def spaceKeys = spaceManager.getAllSpaces()

spaceKeys.each{ space ->
return space.key
}

As always, we need to start by telling the Component Manager what to fetch for us.   We then define a collection of data about all of the Spaces in Confluence. Finally, we can do something with that information.  If I wanted to do something with the Key of each Space, I would work with space.key.

 

Working With SPACE Keys

Now that we have a list of Keys, we can do something with that information. In this case we’re searching for Spaces with a specific label/category.

As you can see in the code below, we take the list of Space Keys.  For each Space Key, we retrieve the labels/categories associated with it.  If the category matches the target string, we can do something to that space.

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.confluence.labels.Label 
import com.atlassian.confluence.labels.LabelManager

def spaceManager = ComponentLocator.getComponent(SpaceManager)
def spaceKeys = spaceManager.getAllSpaces()

spaceKeys.each{ space ->
//For each Space in Confluence

labelManager.getTeamLabelsForSpace(space.key).each{ 
permission ->
//Get each label associated with the Space

if(permission.name.toString() == "application"){
//If the name of the label matches "application"

//Do something to that space

}

 

Adding or Removing Permissions From a CONFLUENCE Space

Adding or removing permissions from a Space is quite simple.   One important difference to note is that addPermissionsToSpace() accepts an array of Strings as it’s first input; removePermissionFromSpace() only accepts a single string.   That means that when adding permissions, you can feed the method an array.  When removing permissions, you’ll need to iterate through the array with an each statement.

Otherwise the arguments that the two methods take are very similar. They take: permission type, user or group, and Space Key.

For example, if I wanted to grant VIEW permissions to the group INTERNAL_STAFF on Space TESTSPACE, I would assemble the statement like so:

addSpacePermission.addPermissionsToSpace("VIEW", "INTERNAL_STAFF", "TESTSPACE")
Of course we rarely directly reference the desired values. Instead what we’d usually do is reference a list, or the results of a previous function. In order to add or remove permissions to the Spaces we identified earlier, we just add the statement into the each loop:
import com.atlassian.sal.api.component.ComponentLocator 
import com.atlassian.confluence.spaces.SpaceManager 
import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.labels.LabelManager

def spaceManager = ComponentLocator.getComponent(SpaceManager) 
def spaceKeys = spaceManager.getAllSpaces() 

spaceKeys.each{ space ->
//For each Space in Confluence 

labelManager.getTeamLabelsForSpace(space.key).each{ permission -> 
//Get each label associated with the Space 

if(permission.name.toString() == "application"){ 
//If the name of the label matches "application" 

addSpacePermission.addPermissionsToSpace(tier1PermissionsList, "<User or group>", space.key)
//Add the permissions to that Space
 }



Notice that instead of directly referencing values, we’re feeding variables to addPermissionsToSpace.  It accepts the full array of permissions called tier1PermissionsList  as the first arguement, a direct reference to a user or group as the second reference (this may also be a variable instead), and the Key of the current Space as the final arguement.

 

Putting it All Together

Below is the full script that we’ve been working toward. It does the things we’ve discussed: it locates Space Keys, retrieves labels, adds and removes permissions.

Worth noting again is that removing more than one permission type requires that you iterate through the list with an each, rather than feeding the entire array of strings to the method.

 

import com.atlassian.confluence.pages.Page
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.labels.LabelManager
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.confluence.rpc.soap.services.SpacesSoapService
import com.atlassian.sal.api.component.ComponentLocator

def addSpacePermission = ComponentLocator.getComponent(SpacesSoapService)
def labelManager = ComponentLocator.getComponent(LabelManager)
def spaceManager = ComponentLocator.getComponent(SpaceManager)



def String[] permissionsList = ["VIEWSPACE","EDITSPACE","EXPORTPAGE","SETPAGEPERMISSIONS","REMOVEPAGE","EDITBLOG",
"REMOVEBLOG","COMMENT","REMOVECOMMENT","CREATEATTACHMENT","REMOVEATTACHMENT","REMOVEMAIL","EXPORTSPACE",
"SETSPACEPERMISSIONS"]
//We need the full list of permissions, as the removePermissions method only accepts one 
type of permission at a time

def String[] internalStaffPermissions = ["REMOVECOMMENT", "VIEW", "COMMENT"]
//Internal staff have only basic permissions

def String[] tier1PermissionsList = ["VIEWSPACE","EDITSPACE","EXPORTPAGE","SETPAGEPERMISSIONS","REMOVEPAGE",
"EDITBLOG","REMOVEBLOG","COMMENT","REMOVECOMMENT","CREATEATTACHMENT","REMOVEATTACHMENT","REMOVEMAIL",
"EXPORTSPACE","SETSPACEPERMISSIONS"]
//This group gets everything except admin rights (SETSPACEPERMISSIONS

def spaceKeys = spaceManager.getAllSpaces()
//Get details for all the Spaces in the instance

spaceKeys.each{
//Iterate through the keys
    space -> 
    labelManager.getTeamLabelsForSpace(space.key).each{ permission ->
       //For each key value, get the labels for that space
        if(permission.name.toString() == "application"){
          //If the space has a label that matches the target
          //Do something to that space
            
            addSpacePermission.addPermissionsToSpace(permissionsList, "<User or group>", space.key)
            //Grant ALL permissions to a group
            
            addSpacePermission.addPermissionsToSpace(internalStaffPermissions, "<User or group>", space.key)
            //Grant limited permissions to a group
                                
            addSpacePermission.addPermissionsToSpace(tier1PermissionsList, "<User or group>", space.key)
            //Grant limited permissions to a group
            
             addSpacePermission.addPermissionsToSpace(tier1PermissionsList, "<User or group>", space.key)
            //Grant limited permissions to a group
            
            
             addSpacePermission.addPermissionsToSpace(tier1PermissionsList, "<User or group>", space.key)
            //Grant limited permissions to a group
            
                       
            permissionsList.each{ perms ->
          		addSpacePermission.removePermissionFromSpace(perms, "<User or group>", space.key)
                //We have to iterate through the total list of permissions because we can't feed a string array
 to removePermissions, like we did with addPermissions
	   }
	}
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

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>