How to automatically add restrictions to a page based on the page's custom field value?

Elliot Huang May 23, 2024

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.

1 answer

0 votes
Matt Gudenas May 23, 2024

Hi @黄舒榕! 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

Elliot Huang May 23, 2024

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. 

 

Matt Gudenas May 23, 2024

Hi @黄舒榕

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.

 

Elliot Huang May 23, 2024

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?

Elliot Huang May 23, 2024

@Matt Gudenas Please help to look into the question upstairs, thank you.

Elliot Huang May 23, 2024

@Matt Gudenas 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.pngscreenshot_20240524144405.png

Matt Gudenas May 24, 2024

@黄舒榕 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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
confluence8.9.0
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events