Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,369,738
Community Members
 
Community Events
168
Community Groups

Add/Remove group from space confluence with script runner

Edited

Hi,

i would like to add or remove groups from spaces in confluence with scriptrunner.

do you have a solution?

thanks

 

for Tony Gough [Adaptavist] , i don't see your answer in my browser (only in my email).

3 answers

2 accepted

Hi laumain,

I've written an example script which shows you how to add a group to a space with view permissions on that space.

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
import com.atlassian.confluence.core.ContentPermissionManager
import com.atlassian.confluence.internal.security.SpacePermissionContext

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

def targetSpace = spaceManager.getSpace("SPACEKEY")
def targetGroup = groupManager.getGroup("groupname")

//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)
}

The two main constructs involved in this are SpacePermission and SpacePermissionManager. The documentation describes various other methods to achieve additional functionality:

https://docs.atlassian.com/ConfluenceServer/javadoc/6.14.0/com/atlassian/confluence/security/SpacePermission.html

https://docs.atlassian.com/atlassian-confluence/6.2.1/com/atlassian/confluence/security/SpacePermissionManager.html 

thanks for your reponse. it's OK :)

do you know how i remove group in permission?

i'm looking for removeGroupSpacePermission , but i don't see that :(

Hi laumain,

To remove a permission, you must load it into a variable, then use the SpacePermissionManager to remove it. The following is an example:

def perms = spacePermissionManager.getAllPermissionsForGroup("groupname")
spacePermissionManager.removePermission(perms[0])

I hope this helps!

Kind regards,
Tony

Hi tony,

thanks a lot,

but where do you specify spaces?

should I understand, all permissions are removed from all spaces?

 

Jérôme

Hi laumain,

In the example above I selected the first permission from the list of permissions for the group 'groupname'. There are various ways of selecting a specific permission - you could use the getGroupsWithPermissions method to get a list of all groups which have permissions on the space, for example, then use getAllPermissionsForGroup to retrieve the object with the group's permissions. Once you have the object, you can iterate through it to find the permission you wish to remove, then remove it with the removePermission method from the spacePermissionsManager.

Kind regards,
Tony

perfect tony , you are a chief!!

thanks

Hi,

Thanks for alerting me to that, not sure why my original comment did not appear. I'll repost a link to the script here:

https://bitbucket.org/snippets/Adaptavist/de5p7X

The important constructs to note are the SpacePermission and SpacePermissionManager from the API:

https://docs.atlassian.com/ConfluenceServer/javadoc/6.14.0/com/atlassian/confluence/security/SpacePermission.html

https://docs.atlassian.com/ConfluenceServer/javadoc/6.14.0/com/atlassian/confluence/security/SpacePermissionManager.html

The documentation describes further methods you can use to achieve different functionality with these.

Kind regards,
Tony

Excellent work @Tony Gough _Adaptavist_!

Can you help with the appropriate script just to update the space view permissions using the Confluence groups that the space creator belongs too?

Acceptance Criteria: Once a user creates a Confluence space, all groups that the space creator belongs to will be added to space view permissions, except the "confluence-users" group.

I planning on using scriptrunner's space create event listener to trigger the script that will fetch all Confluence groups of the space creator and add them to space view permissions.

Appreciating your insight!

Resolved! - Here is the working script in case anyone has my same scenario. 

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

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

def targetSpace = event.getSpace()
def loggedInUser = AuthenticatedUserThreadLocal.get()

// Get all groups for loggedInUser and exclude group that contains 'confluence'def groups = groupManager.getGroups(loggedInUser).currentPage.findAll { !it.name.contains('confluence') }

groups.each { group ->
def spacePermission = SpacePermission.createGroupSpacePermission(SpacePermission.VIEWSPACE_PERMISSION, targetSpace, group.getName())
spacePermissionManager.savePermission(spacePermission)
}

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events