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

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

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

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
Community showcase
Published in Apps & Integrations

Marketplace Partner Spotlight: Fine Software

Happy New Year, everybody! What better way to kick off 2022 than with a new Marketplace Partner Spotlight ✨ This month, we're getting to know Daniel Franz, co-founder of Fine Software. Fine Software ...

429 views 2 15
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