Error when setting Permission to Pages in Scriptrunner

Emil Martens
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 28, 2024

I am trying to set replace group permissions on all pages from "confluence-k-m" to "confluence-v-m" with sciptrunner. 

 

script:

 

import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.core.ContentPermissionManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.spaces.SpaceManager
import static com.atlassian.confluence.security.ContentPermission.EDIT_PERMISSION
import static com.atlassian.confluence.security.ContentPermission.VIEW_PERMISSION
import com.atlassian.confluence.security.ContentPermission

def ignoreSpaces = ['~lierse', '~martens_e', 'GF']
def oldGroup = "confluence-k-m"
def newGroup = "confluence-v-m"


def pageManager = ComponentLocator.getComponent(PageManager)
def spaceManager = ComponentLocator.getComponent(SpaceManager)
def contentPermissionManager = ComponentLocator.getComponent(ContentPermissionManager)


def spaces = spaceManager.getAllSpaces()
log.warn(spaces)

for (int j = 0; j < spaces.size(); j++){
    log.warn(spaces[j])
    def space = spaces[j]
    def allPagesFromSpace = pageManager.getPages(space, false)
    for (def page : allPagesFromSpace) {
        log.warn('############################################')
        def pageTitle = page.getTitle()
        log.warn(page.getSpace())
        log.warn(pageTitle)
        def latestPage = page.getLatestVersion()
        log.warn(page.isDeleted())

        def permissionSet = contentPermissionManager.getContentPermissionSets(latestPage, EDIT_PERMISSION)
        log.warn(permissionSet)

        def perm_edit = []
        permissionSet.toArray().each { permission ->
            def newPermission = new ContentPermission()
            permission.userNames.each {
                name ->
                    perm_edit.add(newPermission.createUserPermission(EDIT_PERMISSION, name))
            }
            permission.groupNames.each{
                name ->
                    if (name != oldGroup){
                        perm_edit.add(newPermission.createGroupPermission(EDIT_PERMISSION, name))
                    } else {
                        perm_edit.add(newPermission.createGroupPermission(EDIT_PERMISSION, newGroup))
                    }
            }
        }


        def permissionSet2 = contentPermissionManager.getContentPermissionSets(latestPage, VIEW_PERMISSION)
        log.warn(permissionSet2)

        def perm_view = []
        permissionSet2.toArray().each { permission ->
            def newPermission = new ContentPermission()
            permission.userNames.each {
                name ->
                    perm_view.add(newPermission.createUserPermission(VIEW_PERMISSION, name))
            }
            permission.groupNames.each{
                name ->
                    if (name != oldGroup){
                        perm_view.add(newPermission.createGroupPermission(VIEW_PERMISSION, name))
                    } else {
                        perm_view.add(newPermission.createGroupPermission(VIEW_PERMISSION, newGroup))
                    }
            }
        }

        //if (pageTitle !in ignore){
            def permissionsToSet = [:]
            if (perm_edit) {
                permissionsToSet[EDIT_PERMISSION] = perm_edit
            }
            if (perm_view) {
                permissionsToSet[VIEW_PERMISSION] = perm_view
            }
            log.warn(latestPage.getTitle() != "")

            if (!permissionsToSet.isEmpty() && !space.isPersonal() && space.getKey()!in ignoreSpaces && pageTitle != "" && !latestPage.isDeleted() && !latestPage.isDraft()) {
                log.warn("Setting permissions for page: ${latestPage.getTitle()}")
                contentPermissionManager.setContentPermissions(permissionsToSet, latestPage)
            } else {
                log.error("No valid permissions to set for page: ${latestPage.getTitle()}")
            }
    }

}


The Problem I have is that it runs perfectly on 100+ pages. But at some pages i ll get this error:


 

2024-08-28 09:05:00,770 WARN [com.onresolve.scriptrunner.runner.ScriptBindingsManager]: Setting permissions for page: Vorlage Protokoll (bitte kopieren und separat speichern) 2024-08-28 09:05:00,773 WARN [com.atlassian.confluence.impl.hibernate.ConfluenceHibernateTransactionManager]: Performing rollback. Transactions: ->[com.atlassian.confluence.core.DefaultContentPermissionManager.setContentPermissions]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT (Session #473264041) 2024-08-28 09:05:00,775 ERROR [com.onresolve.scriptrunner.runner.rest.common.UserScriptEndpoint]: ************************************************************************************* 2024-08-28 09:05:00,775 ERROR [com.onresolve.scriptrunner.runner.rest.common.UserScriptEndpoint]: Script console script failed: java.lang.NullPointerException


The error is happening in many pages. I can't find a rule why its happening. It happens on personal pages without restrictions, global pages with and without restrictions...

Thanks for help!

1 answer

0 votes
John Funk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 28, 2024

Hi Emil - Welcome to the Atlassian Community!

You probably want to contact the vendor directly for assistance. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events