Snippets

Adaptavist Add group to Confluence space

Created by Tony Gough last modified
/**
* A script that should be run in the Script Console of ScriptRunner for Confluence.
* This script adds a group to a Confluence space if the group does not already have permissions there.
*/

import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.security.SpacePermissionManager
import com.atlassian.confluence.security.SpacePermission
import com.atlassian.user.GroupManager

def spaceManager = ComponentLocator.getComponent(SpaceManager)
def spacePermissionManager = ComponentLocator.getComponent(SpacePermissionManager)
def groupManager = ComponentLocator.getComponent(GroupManager)

def targetSpace = spaceManager.getSpace("TSPAC")     //Space key
def targetGroup = groupManager.getGroup("newgroup")  //Group name

//Ensure the space doesn't have the group already
if (!spacePermissionManager.getGroupsWithPermissions(targetSpace).contains(targetGroup)) {    
    //Add the group to the space with, with view permissions
	def spacePermission = SpacePermission.createGroupSpacePermission(SpacePermission.VIEWSPACE_PERMISSION, targetSpace, targetGroup.getName())
	spacePermissionManager.savePermission(spacePermission)
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.