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.
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@黄舒榕 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.