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

Applying multiple groups and permissions to a space on Space Create Event with Scriptrunner

Dom July 11, 2022

Scenario: We have five default groups made for every space with the space key as a prefix. These groups all have specific permissions on the space to provide non-exclusive membership combinations.

I have made an Event Listener for new space creation and attached a script to it, so that it pulls the new space key, creates the five groups correctly, and then applies each with their specific permissions.

This far, my script has matured to the point where I have gotten it to make the groups, but when I tried to apply only one of the groups to the space, it fails.

I had already defined the permission combinations for each role, so ideally a resolution would cycle through the different roles and apply each accordingly.

That said, I'd love to know why I'm getting the error I note under the script and how to resolve that as well.

The Script this far:


Disclaimer - I'm only good enough at Groovy to be dangerous, so I apologize in advance to actual programmers who see this. ;)

import com.atlassian.confluence.security.SpacePermissionManager
import
com.atlassian.confluence.event.events.space.SpaceCreateEvent
import com.atlassian.confluence.security.SpacePermission
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.user.GroupManager

def spaceManager = ComponentLocator.getComponent(SpaceManager)
def spacePermissionManager = ComponentLocator.getComponent(SpacePermissionManager)
def groupManager = ComponentLocator.getComponent(GroupManager)
def event = event as SpaceCreateEvent
def key = event.space.getLowerKey()

def SpaceAdminsName = "${key} -!space admins"
def SpaceAdminsPermissionsList = [SpacePermission.VIEWSPACE_PERMISSION,
    SpacePermission.ADMINISTER_SPACE_PERMISSION]

def SpaceOwnersName = "${key} -!space owners"
def SpaceOwnersPermissionsList = [SpacePermission.VIEWSPACE_PERMISSION,
    SpacePermission.REMOVE_OWN_CONTENT_PERMISSION,
    SpacePermission.EDITBLOG_PERMISSION]

def ContributorsName = "${key} -contributors"
def ContributorsPermissionsList = [SpacePermission.VIEWSPACE_PERMISSION,
    SpacePermission.REMOVE_OWN_CONTENT_PERMISSION,
    SpacePermission.CREATEEDIT_PAGE_PERMISSION,
    SpacePermission.CREATE_ATTACHMENT_PERMISSION,
    SpacePermission.COMMENT_PERMISSION]

def ManagersName = "${key} -!managers"
def ManagersPermissionsList = [SpacePermission.VIEWSPACE_PERMISSION,
    SpacePermission.REMOVE_PAGE_PERMISSION,
    SpacePermission.REMOVE_BLOG_PERMISSION,
    SpacePermission.REMOVE_ATTACHMENT_PERMISSION,
  SpacePermission.REMOVE_COMMENT_PERMISSION,
    SpacePermission.SET_PAGE_PERMISSIONS_PERMISSION]

def VisitorsName = "${key} -visitors"
def VisitorsPermissionsList = [SpacePermission.VIEWSPACE_PERMISSION,
    SpacePermission.REMOVE_OWN_CONTENT_PERMISSION,
    SpacePermission.COMMENT_PERMISSION]

log.info("Default Groups generated for ${key}")
groupManager.createGroup(SpaceAdminsName)
groupManager.createGroup(SpaceOwnersName)
groupManager.createGroup(ContributorsName)
groupManager.createGroup(ManagersName)
groupManager.createGroup(VisitorsName)

def spacePermission
SpaceAdminsPermissionsList.each { SpacePermission ->
    spacePermissionToSave = SpacePermission.createGroupSpacePermission(SpacePermission, key, SpaceAdminsName.toString())
    spacePermissionManager.savePermission(spacePermissionToSave)
}

The createGroups work, but when it gets to this last part to apply the group that was just generated, I get the following error:

 

2022-07-11 22:55:22,689 ERROR [events.DynamicEventListenerInvoker]: *************************************************************************************
2022-07-11 22:55:22,690 ERROR [events.DynamicEventListenerInvoker]: Event listener failed: event: com.atlassian.confluence.event.events.space.SpaceCreateEvent, id: 9de31dd6-e02a-41a8-82b4-4210d8f8804b, script notes: Create and Apply Default Groups to a new Space
groovy.lang.MissingMethodException: No signature of method: java.lang.String.createGroupSpacePermission() is applicable for argument types: (String, String, String) values: [VIEWSPACE, aaa, aaa -!space admins]
at Auto_Make_Groups$_run_closure1.doCall(Auto Make Groups.groovy:70)
at Auto_Make_Groups.run(Auto Make Groups.groovy:69)

Prior to this I had essentially used ${key}" -visitors" as the group names, and that had given me this. I made them variables so that I could get rid of the ${} reference. This showed up as "org.codehaus.groovy.runtime.GStringImpl", so I added .toString() and got the error above.

Don't rightly know what No signature of method relates to for createGroupSpacePermission()... providing all strings appears to be correct when I check the API documentation. :(  That may just be my hacking level of experience showing, though.

Hoping someone can help clarify.

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events