How to automatically add restrictions to a page based on the page's custom field value?
Asked by Elliot Huang:

Hi there,

We are now using confluence DC,?and??we hope to implement stricter page permission control, such as setting a custom field for file confidentiality in the page. When the field value is "to be kept confidential", it will automatically trigger the addition of restrictions to the page.

We would like to know whether it can be achieved using script runner for confluence, and whether there are some reference cases. Thank you for your help.


Answers:
author: Matt Gudenas
body: Hi [@???](/t5/user/viewprofilepage/user-id/5436659)! I am not sure if you are asking about Jira, since custom fields are used there, you can achieve what you are looking for in Confluence in other ways, for example, using Labels or setting a specific title. You can create an Event Listener with ScriptRunner by going to ScriptRunner -\> Listeners and then choosing a Custom event listener. You can then provide a custom script that should meet your requirements. Here is an example to get you started:  

```
import com.atlassian.confluence.event.events.content.page.PageCreateEvent

import com.atlassian.confluence.pages.Page

import com.atlassian.confluence.security.ContentPermission

import com.atlassian.confluence.user.AuthenticatedUserThreadLocal

import com.atlassian.sal.api.component.ComponentLocator

import com.atlassian.confluence.core.ContentPermissionManager

import com.atlassian.user.GroupManager

import static com.atlassian.confluence.security.ContentPermission.EDIT_PERMISSION

import static com.atlassian.confluence.security.ContentPermission.VIEW_PERMISSION

import static com.atlassian.confluence.security.ContentPermission.createGroupPermission

void addEditPermission( Page page, String group ) {

  addPermission(page, group, EDIT_PERMISSION)

}

void addViewPermission( Page page, String group ) {

  addPermission(page, group, VIEW_PERMISSION)

}

void addPermission( Page page, String group, String permissionType ) {

def contentPermissionManager = ComponentLocator.getComponent(ContentPermissionManager)

  Collection<ContentPermission> permissions = []

  permissions.add(createGroupPermission(permissionType, group))

  contentPermissionManager.setContentPermissions(permissions, page, permissionType)

}

// Update with any event on which you want this script to run.

def createEvent = event as PageCreateEvent

def currentUser = AuthenticatedUserThreadLocal.get()

def groupManager = ComponentLocator.getComponent(GroupManager)

def groups = groupManager.getGroups(currentUser)

def page = createEvent.page

def labels = page.getLabels()

// Update with the name of the group that you want to have access

def groupName = 'admin-users'

// Update with the label that you want to use for restricting access

if (labels.contains("top-secret")) {

  groups.each { group ->

    if ( group.toString() == groupName ) {

      addEditPermission(page, group.toString())

      addViewPermission(page, group.toString())

    }

  }

}
```

You can read more about event listeners here - <https://docs.adaptavist.com/sr4c/latest/features/event-listeners>  

If you have any more questions, we'll be happy to help.

Matt

comments: author: Elliot Huang
body: Hi Matt,

Thanks for your reply, and we are trying to implement this goal in Confluence, not Jira, and the custom field may be some certain text in the page, or in the page title, etc.

We wander whether the listener can be triggered under these condition, and how can this listener combine with the later job, which aims to set up the page restriction automatically.


author: Matt Gudenas
body: Hi [@???](/t5/user/viewprofilepage/user-id/5436659)

I think the code provided should do the job for you. If you want, you can adjust it to check the page title instead of the label, but labels might be cleaner.

Let me know if you need help with it.


author: Elliot Huang
body: Hi Matt,

Got it, we will have a try.

BTW, is it possible to use script runner to automatically add a mandatory custom field to the page when creating the page (the field value can be multiple-selected). This field needs to be filled in before the page can be saved?


author: Elliot Huang
body: [@Matt Gudenas](/t5/user/viewprofilepage/user-id/5456514) Please help to look into the question upstairs, thank you.


author: Elliot Huang
body: [@Matt Gudenas](/t5/user/viewprofilepage/user-id/5456514) I've tried the code and modify the group \& label, it seems to run normally, but the page restriction wasn't added to the page, as you can see in below srceenshot, please help with this, thank you.![screenshot_20240524144135.png](https://community.atlassian.com/t5/image/serverpage/image-id/325767i6AD4F929CE470902/image-size/large?v=v2&px=999 "screenshot_20240524144135.png")![screenshot_20240524144405.png](https://community.atlassian.com/t5/image/serverpage/image-id/325771iC1B44BCFC113D093/image-size/large?v=v2&px=999 "screenshot_20240524144405.png")


author: Matt Gudenas
body: [@???](/t5/user/viewprofilepage/user-id/5436659) Would you mind opening a ticket through ScriptRunner support?   

<https://www.scriptrunnerhq.com/help/support>  

We want to ensure that you receive continuous support on this issue until its resolution as I will be out of office for a few days.

Thank you,

Matt


Tags: confluence-data-center

data-center

scriptrunner