Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Bulk change group permissions for all spaces in confluence

DORIAN KERSCH
Contributor
November 7, 2018

Hi,

I added a comment here , but I thought I would ask a specific question for script runner.

 

Does anyone have a snippet that will allow you to bulk change group permissions across the confluence instance?

 

A use case would be:

 

For all spaces, check if group has "confluence-users" as a group and if it does then apply "new-group" to that space.

 

A similar use case would be for deletion:

For all spaces, check if group has "foo-bar-users" as a group and if it does then remove "foo-bar-users" from that space.

 

1 answer

0 votes
Josh Kronick
Contributor
June 14, 2023

https://library.adaptavist.com/entity/remove-space-permissions-for-a-specified-group-on-all-spaces

 


 

import com.atlassian.confluence.internal.security.SpacePermissionManagerInternal
import com.atlassian.confluence.security.SpacePermission
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.confluence.security.SpacePermissionManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.api.model.content.Space
import com.atlassian.confluence.api.model.content.SpaceType
import com.atlassian.confluence.api.model.Expansion
import com.atlassian.confluence.api.model.pagination.SimplePageRequest
import com.atlassian.confluence.api.service.content.SpaceService
import com.atlassian.crowd.embedded.api.Group
import com.onresolve.scriptrunner.parameters.annotation.GroupPicker
import com.atlassian.confluence.internal.security.SpacePermissionContext
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

@GroupPicker(label = 'Group', description = 'Select the group to remove space permission from')
Group selectedGroup
assert selectedGroup

def spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)
def spaceManager = ComponentLocator.getComponent(SpaceManager)
def spacePermissionManager = ComponentLocator.getComponent(SpacePermissionManager) as SpacePermissionManagerInternal

def spaceContext = SpacePermissionContext.createDefault()

def allSpaces = [] as List<Space>
def permissionsToRemove = [] as List<SpacePermission>

def start = 0
def limit = 1000
def hasMore = true

while (hasMore) {
def spaceFinder = spaceService.find(new Expansion('name'))
.withType(SpaceType.GLOBAL)
.fetchMany(new SimplePageRequest(start, limit))

allSpaces.addAll(spaceFinder.results)

start = start + limit
hasMore = spaceFinder.hasMore()
}

allSpaces.each { space ->
def spacePermissions = spaceManager.getSpace(space.key).permissions

spacePermissions.each { permission ->
// This condition will check if the permission's group matched with the user's selected group.
if (permission.group == selectedGroup.name) {
permissionsToRemove.addAll(permission)
}
}
}

permissionsToRemove.each { permission ->
spacePermissionManager.removePermission(permission, spaceContext)
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events