Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Get all user groups except confluence user group for space view permission?

Luis October 27, 2021 edited

My goal in Confluence is to add all user groups that the user belongs to except any group that contains or begins with "confluence", to the space view permission during space creation.

I have found this script in our community (@Tony Gough _Adaptavist) that I would like to leverage in scriptrunner's space create event listener.

I'm unfamiliar on how to call out for the users permissions (except confluence group) instead of using the one provided in the script below.

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

 Thanks!

1 answer

0 votes
Luis November 4, 2021

Adaptavist (@HelmyIbrahim) was able to help me out and after using their approach, we now have this working:

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
AUG Leaders

Upcoming Apps & Integrations Events