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).
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:
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 :(
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi tony,
thanks a lot,
but where do you specify spaces?
should I understand, all permissions are removed from all spaces?
Jérôme
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
The documentation describes further methods you can use to achieve different functionality with these.
Kind regards,
Tony
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.