Add/Remove group from space confluence with script runner

laumain jerome March 19, 2019

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

1 vote
Answer accepted
Tony Gough [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 28, 2019

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 

laumain jerome March 29, 2019

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 :(

Tony Gough [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 2, 2019

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

laumain jerome April 2, 2019

Hi tony,

thanks a lot,

but where do you specify spaces?

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

 

Jérôme

Tony Gough [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 3, 2019

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

laumain jerome April 3, 2019

perfect tony , you are a chief!!

thanks

0 votes
Answer accepted
Tony Gough [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 29, 2019

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

Luis October 18, 2021

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!

Luis Tellado February 8, 2022

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)
}
ahmed November 23, 2022

I'm new to confluence APIs.

Please, can you tell me which dependencies to use?
to resolve this.

import com.atlassian.confluence.spaces.SpaceManager

I included most of the confluence libs but, they never got resolved.
I need the Gradle dependency line like this or maven XML of course.

 implementation group: 'com.atlassian.confluence', name: 'confluence-rest-client', version: '8.0.0-m012'
0 votes
Luis October 18, 2021

Whoops :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events