Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Codegeist '21 Customer Choice Awards - Vote Now!

Last quarter, 290 developers participated in Atlassian’s annual hackathon, Codegeist, for a chance to win thousands of dollars with a new Marketplace app. We announced winners selected by Atlassian...

910 views 4 16
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you