How do you correctly add permissions to a page using Scriptrunner for Confluence
Asked by Terje Lund?n:

Hellooo,  

I'm trying to write a script that applies all the edit-permission of one page onto all of it's descendants.  
I have 2 problems when running my script:

1. The permissions get applied only to the lowest level of pages in the hierarchy
2. When removing a permission from the original page after running the script I'm unable to save the change and after a while the page get's corrupted and I can no longer view it. I only receive the following error:  

   ### Cause {#toc-hId-1563777814}

   org.hibernate.ObjectNotFoundException: No row with the given identifier exists: \[com.atlassian.confluence.security.ContentPermission#31391756\]  
   at org.hibernate.boot.internal.StandardEntityNotFoundDelegate.handleEntityNotFound(StandardEntityNotFoundDelegate.java:28)

   #### Stack Trace:\[hide\] {#toc-hId--1184892652}

   ```
   org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.atlassian.confluence.security.ContentPermission#31391756]	at org.hibernate.boot.internal.StandardEntityNotFoundDelegate.handleEntityNotFound(StandardEntityNotFoundDelegate.java:28)	at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227)	at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:262)	at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
   ...
   ```

Following is my code:

```
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.sal.api.component.ComponentLocator
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonOutput
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response


@BaseScript CustomEndpointDelegate delegate

copyRestrictionsToDescendants() { MultivaluedMap queryParams, String body ->
    def pageId = queryParams.getFirst("pageId") as long// use the pageId to retrieve this page
    def pageManager = ComponentLocator.getComponent(PageManager)
    def permissionManager = ComponentLocator.getComponent(ContentPermissionManager)

    def currentPage = pageManager.getPage(pageId)
    def editPermissions = currentPage.getContentPermissionSet(ContentPermission.EDIT_PERMISSION)

    def addPermissions

    addPermissions = { List<Page> pages ->
        for (Page child : pages) {
            for (ContentPermission permission in editPermissions) {
                permissionManager.addContentPermission(permission, child)
            }
            addPermissions(child.children)
        }
    }

    addPermissions(currentPage.children)

    def flag = [
            type : 'success',
            title: "Requirement bar added",
            close: 'auto',
            body : "The requirement bar has been added."
    ]

    Response.ok(JsonOutput.toJson(flag)).build()
}
```


Answers:
author: Stephen Cheesley [Adaptavist]
body: Hey Terje,

In ScriptRunner for Confluence we have a built-in script that allows you to do just this! If you navigate to "Space Tools \> Advanced Space Functionality \> Inherit Restrictions for Pages" and you can select which pages and spaces you would like to have child pages inherit root parent permissions.

The documentation for this feature is available from [here](https://scriptrunner.adaptavist.com/5.2.12/confluence/builtin-scripts.html#_inherit_restrictions_for_pages).

I hope this helps!

Steve

comments: author: Cinnober Admins
body: Hello Stephen,

The solution works for pages created **after** running the script. We're interested in inheriting edit rights for old pages as well. Will this be possible in future releases?

Thanks

Hosam


author: IT Einkauf
body: I have exactly the same question. As it is, this feature is not worth much. Users expect some consistency, so permissions inheritance would have to work in all cases:

* Creating a new page
* Changing permissions on parent page
* Changing permission on child page (if we take inheritance seriously, setting conflicting permissions on a child page would have to be suppressed)
* Moving a page (inside a space or between spaces)
* (maybe there are more cases that I didn't think of)

We'd definitely buy ScriptRunner if this would be implemented.


Tags: scriptrunner