/* 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 */importcom.atlassian.confluence.security.ContentPermissionSetimportcom.atlassian.confluence.core.ContentPermissionManagerimportcom.atlassian.confluence.pages.Pageimportcom.atlassian.confluence.pages.PageManagerimportcom.atlassian.confluence.security.ContentPermissionimportcom.atlassian.confluence.spaces.SpaceManagerimportcom.atlassian.sal.api.component.ComponentLocatorimportcom.atlassian.spring.container.ContainerManagerimportcom.atlassian.confluence.event.events.content.page.PageMoveCompletedEventdefcontentPermissionManager=ComponentLocator.getComponent(ContentPermissionManager)asContentPermissionManagerdefpageManager=(PageManager)ContainerManager.getComponent("pageManager")defspaceManager=(SpaceManager)ContainerManager.getComponent("spaceManager")defevent=eventasPageMoveCompletedEventdefmovedPages=event.getMovedPageList()//For each moved pagemovedPages.each{PagemovedPage->//Get the parentdefparent=movedPage.getParent()[ContentPermission.VIEW_PERMISSION,ContentPermission.EDIT_PERMISSION].each{permissionType->//Get the parent's permissionsdefparentPermissions=contentPermissionManager.getContentPermissionSets(parent,permissionType).findAll{it.owningContent==parent}//Apply the permissions to the moved pagecontentPermissionManager.setContentPermissions(contentPermissionsFromSet(parentPermissions),movedPage,permissionType)}}//Resolve permissionsList<ContentPermission>contentPermissionsFromSet(List<ContentPermissionSet>contentPermissionSets){if(!contentPermissionSets){return[]}defcontentPermissions=contentPermissionSets.collect{contentPermissionSet->defcontentPermissions=contentPermissionSet.getAllExcept(Collections.emptyList())contentPermissions.collect{contentPermission->if(contentPermission.isUserPermission()){ContentPermission.createUserPermission(contentPermission.getType(),contentPermission.getUserSubject())}elseif(contentPermission.isGroupPermission()){ContentPermission.createGroupPermission(contentPermission.getType(),contentPermission.getGroupName())}}}.flatten()contentPermissions.removeAll([null])contentPermissionsasList<ContentPermission>}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.