Snippets

Adaptavist Confluence Scripted Event Handler - Moved Page Inherits New Parent Restrictions

Created by Kristian Walker
/* The code below should be added to a Scripted Event Handler which listens to the PageMoveCompletedEvent.
 * This script ensures that whenever a page is moved in Confluence, it will inherit the permissions of its new parent page.
 * All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner 
 * license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed
 * or amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
 * @author: Tony Gough
 */

import com.atlassian.confluence.security.ContentPermissionSet
import com.atlassian.confluence.core.ContentPermissionManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.security.ContentPermission
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.spring.container.ContainerManager
import com.atlassian.confluence.event.events.content.page.PageMoveCompletedEvent

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

def event = event as PageMoveCompletedEvent
def movedPages = event.getMovedPageList()

//For each moved page
movedPages.each { Page movedPage -> 
    //Get the parent
    def parent = movedPage.getParent()
    [ContentPermission.VIEW_PERMISSION, ContentPermission.EDIT_PERMISSION].each { permissionType ->
        //Get the parent's permissions
        def parentPermissions = contentPermissionManager.getContentPermissionSets(parent, permissionType).findAll {
            it.owningContent == parent
        }
        //Apply the permissions to the moved page
        contentPermissionManager.setContentPermissions(contentPermissionsFromSet(parentPermissions), movedPage, permissionType)
    }
}

//Resolve permissions
List<ContentPermission> contentPermissionsFromSet(List<ContentPermissionSet> contentPermissionSets) {
    if (!contentPermissionSets) {
        return []
    }
    
    def contentPermissions = contentPermissionSets.collect { contentPermissionSet ->
        def contentPermissions = contentPermissionSet.getAllExcept(Collections.emptyList())
        contentPermissions.collect { contentPermission ->
            if (contentPermission.isUserPermission()) {
                ContentPermission.createUserPermission(contentPermission.getType(), contentPermission.getUserSubject())
            } else if (contentPermission.isGroupPermission()) {
                ContentPermission.createGroupPermission(contentPermission.getType(), contentPermission.getGroupName())
            }
            
        }
    }.flatten()
    
    contentPermissions.removeAll([null])
    contentPermissions as List<ContentPermission>
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.