Groovy: Field Level Permission - Field only unlocked when open

Karrie Dash
Contributor
May 22, 2017

Using Groovy Scriptrunner, is it possible to make the following happen:

The DESCRIPTION field is locked when:
1. User is in PROJECT ROLE ABC
AND
2. Issue status is NOT Open

In other words, users in the project role should only be able to edit the Description field when the status is Open. Once it moves past open, the user can no longer edit the field.

Below are settings that I have tried but the DESCRIPTION field is not locking, regardless of the status. The behavior is mapped to all projects & issue types.

a1.PNGa2.PNG

I also attempted to lock the DESCRIPTION field when the status is OPEN (regardless of the project role). This did not work - the add-on did not lock the field.

1 answer

0 votes
Thanos Batagiannis [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 29, 2017

Hi Karrie, 

The except condition is when you are in a transition screen, but I understand you care about the status of the issue. Therefore in your case your will need to add a servier side script that will look like

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

import static com.atlassian.jira.issue.IssueFieldConstants.*

@BaseScript FieldBehaviours fieldBehaviours

def description = getFieldById(DESCRIPTION)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def devProjectRole = projectRoleManager.getProjectRole("Developers")

if (projectRoleManager.isUserInProjectRole(currentUser, devProjectRole, issueContext.getProjectObject()) && underlyingIssue?.status?.name != "Open") {
    description.setReadOnly(true)
} else {
    description.setReadOnly(false)
}

PS. That script checks tha name of the status and not the name of the status category, if the later is your case then use 

underlyingIssue?.status?.statusCategory?.name != "Open"

Hope that does the trick.

Regards, Thanos

Karrie Dash
Contributor
May 30, 2017

Thanks! I will give that a try today.

You mentioned that this is a server script, but I am not entirely sure where to save the code.

Capture.JPG

Karrie Dash
Contributor
May 30, 2017

Under SCRIPT CONSOLE, I changed the project role from "Developer" to "Test" (it is a valid role) and got the following error message

 

Error: Cannot get property 'pid' on null object

java.lang.NullPointerException: Cannot get property 'pid' on null object
at com.onresolve.jira.groovy.user.FormField.getFormValue(FormField.groovy:168)
at com.onresolve.jira.groovy.user.FieldBehaviours.getIssueContext(FieldBehaviours.groovy:182)
at Script12.run(Script12.groovy:15)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events