Groovy: Field Level Permission - Field only unlocked when open Edited

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

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

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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

130 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you