Change Permissions with ConfluenceScriptRunner for a specific page

I want to copy the permission from one page to another.
I’m running this script in the script console. Later I want to use it after page creation with a Listiner.
The error I’m getting when running the script is following.
I’ve tried adding @Transactional but the import is throwing an error, that Transactional was not found.

import org.springframework.transaction.annotation.Transactional

@Transactional(readOnly = false)
def copyPagePermissions() {

error:
Something went wrong: InvalidDataAccessApiUsageException

Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove ‘readOnly’ marker from transaction definition.

org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove ‘readOnly’ marker from transaction definition.
at org.springframework.orm.hibernate5.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1047)
at org.springframework.orm.hibernate5.HibernateTemplate.lambda$saveOrUpdate$15(HibernateTemplate.java:666)
at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:367)
at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:334)
at org.springframework.orm.hibernate5.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:665)
at com.atlassian.confluence.core.persistence.hibernate.HibernateObjectDao.saveRaw(HibernateObjectDao.java:250)
at com.atlassian.confluence.core.persistence.hibernate.HibernateObjectDao.save(HibernateObjectDao.java:210)
at com.atlassian.confluence.core.DefaultContentPermissionManager.doAddContentPermission(DefaultContentPermissionManager.java:305)
at com.atlassian.confluence.core.DefaultContentPermissionManager.addContentPermission(DefaultContentPermissionManager.java:293)
at com.atlassian.confluence.core.DefaultContentPermissionManager.copyContentPermissionSet(DefaultContentPermissionManager.java:579)
at com.atlassian.confluence.core.DefaultContentPermissionManager.lambda$copyContentPermissions$5(DefaultContentPermissionManager.java:524)
at com.atlassian.confluence.impl.audit.DefaultAuditingContext.onlyAuditFor(DefaultAuditingContext.java:22)
at com.atlassian.confluence.impl.audit.DefaultAuditingContext.executeWithoutAuditing(DefaultAuditingContext.java:16)
at com.atlassian.confluence.core.DefaultContentPermissionManager.copyContentPermissions(DefaultContentPermissionManager.java:522)
at com.atlassian.confluence.core.DefaultContentPermissionManager.copyContentPermissions(DefaultContentPermissionManager.java:517)
at com.atlassian.confluence.core.ContentPermissionManager$copyContentPermissions.call(Unknown Source)
at Script17.run(Script17.groovy:35)

script:
import com.atlassian.confluence.pages. PageManager
import com.atlassian.confluence.spaces. SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.security.ContentPermission
import com.atlassian.confluence.core.DefaultContentPermissionManager
import com.atlassian.confluence.security.SpacePermission
import com.atlassian.confluence.security.Permission
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.confluence.security.SpacePermissionManager

def userAccessor = ComponentLocator.getComponent(UserAccessor)

def spacePermissionManager = ComponentLocator.getComponent(SpacePermissionManager)

def spaceManager = ComponentLocator.getComponent(SpaceManager)

def pageManager = ComponentLocator.getComponent(PageManager)

def permissionManager = ComponentLocator.getComponent(DefaultContentPermissionManager)

def targetSpace = spaceManager.getSpace(“~SPACE”)

def indexPage = pageManager.getPage(ID)

def permissions = indexPage.getPermissions()

def group = userAccessor.getGroup(‘GROUP’)

def copyToPage = pageManager.getPage(targetSpace.key, ‘SITE’)

permissionManager.copyContentPermissions(indexPage, copyToPage)

return copyToPage.getPermissions()

2 Likes

Try to change from

import com.atlassian.confluence.core.DefaultContentPermissionManager

def permissionManager = ComponentLocator.getComponent(DefaultContentPermissionManager)

to

import com.atlassian.confluence.core.ContentPermissionManager

def permissionManager = ComponentLocator.getComponent(ContentPermissionManager)

I had the same trouble with the Default... manager implementation.