How to restrict users based on other fields

Hi Professionals,

I am having difficulties in set up the below condition in jIRA. I am veru new to JIRA so seeking for the help. Hope to get help from you.

I have two fields, customfield_1 with dropdown value A, and B and Customfield_2 with dropdown value X, and Y. Now when the value in either of the field is selected that bug should not be Assign to the user in Group.

Conditions are:

1) If I am creating bug with the 1st customfield as A OR 2nd customfield as Y, I should NOT be able to Assign the bug to the certain Group. The group name is SDLC_Test.

2) If I am creating bug with the 1st customfield as A OR 2nd customfield as X, I should NOT be able to Assign the bug to the certain Group. The group name is SDLC_Test.

3) If I am creating bug with the 1st customfield as B OR 2nd customfield as Y, I should NOT be able to Assign the bug to the certain Group. The group name is SDLC_Test.

4) If I am creating bug with the 1st customfield as B OR 2nd customfield as X, I SHOULD be able to Assign the bug to the certain Group. The group name is SDLC_Test.

Please help. Thanks.

1 answer

1 vote
Vasiliy Zverev Community Champion Dec 06, 2016

There are following options to do this:

1) reorganise you jira: create 4 diffenrent projects for each case. Create project role for assignable users and grant assignable users permission to this role/

2) Create a script validator. It colud be done with custom plugin or with Script validator provided by Script runner plugin. I could help you with script to validate.

Which option you select?

Thank you, Vasiliy!

I would like to go with Option #2. Please help me.

Thanks

Vasiliy Zverev Community Champion Dec 08, 2016

Here is code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.groups.GroupManager

CustomField customField_1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("customField_1")
CustomField customField_2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("customField_2")

Issue issue

String option1 = ((Option) issue?.getCustomFieldValue(customField_1)).getValue()
String option2 = ((Option) issue?.getCustomFieldValue(customField_2)).getValue()

def user = issue?.getAssignee();
GroupManager groupManager = ComponentAccessor.getGroupManager()

if(option1.equals("A")){
    if(option2.equals("Y")){
        return !groupManager.isUserInGroup(user, "SDLC_Test")
    }
    
    return !groupManager.isUserInGroup(user, "SDLC_Test")
}

if(option1.equals("B")){
    if(option2.equals("Y")){
        return !groupManager.isUserInGroup(user, "SDLC_Test")
    }

    return !groupManager.isUserInGroup(user, "SDLC_Test")
}

Thanks for the code.

BTw, where do I put this code? Will this be in POST function on each Transitions or in Behavior?

 

I am having issue. What is the mistake am I doing pls? I have y below code in Validator tab of Create transition.

 

import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.customfields.option.Option import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.security.groups.GroupManager

CustomField data = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sensative Data")

CustomField env = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Environment")

Issue issue

String option1 = ((Option) issue?.getCustomFieldValue(data)).getValue()

String option2 = ((Option) issue?.getCustomFieldValue(env)).getValue()

def user = issue?.getAssignee();

GroupManager groupManager = ComponentAccessor.getGroupManager()

 if(option1.equals("Yes")){    

      if(option2.equals("PROD")){        

              return !groupManager.isUserInGroup(user, "OFFSDLC_TEST_DEV")    

 }    

return !groupManager.isUserInGroup(user, "INSDLC_TEST_QA") }

 

Here, I am trying to do is: I have Sensative Data = Yes and ENV = Prod, when Both are selected as Yes and Prod or when One is selected - I should not be assign the defect to users in  OFFSDLC_TEST_DEV group. But should be able to assign to other group.  

Please help.

 

Hi Vasiliy Zverev,

I ran the avove code in Script Console and got the error message as below. Please help me.

 Error

Cannot invoke method getValue() on null object

java.lang.NullPointerException: Cannot invoke method getValue() on null object at Script79.run(Script79.groovy:12)

 Error

Cannot invoke method getValue() on null object

java.lang.NullPointerException: Cannot invoke method getValue() on null object at Script79.run(Script79.groovy:12)
2016-12-12 11:56:09,355 WARN [managers.DefaultCustomFieldManager]: Warning: returning 1 of 12 custom fields named 'Environment' 2016-12-12 11:56:09,358 WARN [common.UserScriptEndpoint]: Script console script failed: java.lang.NullPointerException: Cannot invoke method getValue() on null object at Script79.run(Script79.groovy:12)

 

Vasiliy Zverev Community Champion Dec 12, 2016

It mean that one if custom field values are null. There are two options:

  • Make this fields mandatory via field scheme configuration (good one)
  • add check on null values.

Which one is for you?

 

Hi Vasiliy Zverev,

Thank you so much for your continuous support.

Below is my update.

 

Both fields are made made Required and both fields have dropdown values. I would need both fields to use. So 1) if my 1st field = Yes and/or 2nd Field = PROD, this defect should NOT be able to assign to the user of Groups. Groups example, SDLC_Test, SDLC_Dev.

2) if my 1st Field = Yes and/or 2nd field = Test, then this is still same as above case #1.

3) if my 1st field = NO and/or 2nd field = Prod, then it should be same as above case #1.

4) if my 1st field = NO, and 2nd Field = Test, then this defect Should be able to assign to users in any group. Groups are SDLC_Test, SDLC_Dev, SDLC_INT, SDLC_UAT.

 

Please help.

 

Vasiliy Zverev Community Champion Dec 13, 2016

Lets try this way

           PROD             Test

_________________________________

Yes  |-SDLC_Test   |   -SDLC_Test  

       | - SDLC_Dev   |     - SDLC_Dev

_________________________________

NO  |  -SDLC_Test   |  + SDLC_Test, +SDLC_Dev, +SDLC_INT, +SDLC_UAT

       | SDLC_Dev   |

Hi Vasiliy Zverev,

I'm sorry. where do I add abbove given information by you?

Vasiliy Zverev Community Champion Dec 15, 2016

Try this one

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.groups.GroupManager

CustomField customField_1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("customField_1")
CustomField customField_2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("customField_2")

Issue issue

String option1 = ((Option) issue?.getCustomFieldValue(customField_1)).getValue()
String option2 = ((Option) issue?.getCustomFieldValue(customField_2)).getValue()

def user = issue?.getAssignee();
GroupManager groupManager = ComponentAccessor.getGroupManager()

if( (option1.equals("PROD")) || (option2.equals("Yes")) ){

        if (    (groupManager.isUserInGroup(user, "SDLC_Test"))
            &&  (groupManager.isUserInGroup(user, "SDLC_Dev"))
        ) {
            return false
        }
        else
            return true
}
else if(    (groupManager.isUserInGroup(user, "SDLC_Test"))
        &&  (groupManager.isUserInGroup(user, "SDLC_Dev"))
        &&  (groupManager.isUserInGroup(user, "SDLC_INT"))
        &&  (groupManager.isUserInGroup(user, "SDLC_UAT"))){
        return true
    }   
    else return false

Hi Vasiliy Zverev,

My challange being new to JIR and Groovy scrip is that, I am not sure where to put this script in my project? Will this script goes in Create transition Postfunction or Validator or condition? or In a Behavior of Customefield_1 or customefield_2? or somewhere else? Please advice.

I put above code in Create Transition and got the following error.

Time (on server): Fri Dec 16 2016 11:42:49 GMT-0600 (Central Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

Time (on server): Fri Dec 16 2016 11:42:49 GMT-0600 (Central Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2016-12-16 12:42:49,656 WARN [managers.DefaultCustomFieldManager]: Warning: returning 1 of 12 custom fields named 'Environment'
2016-12-16 12:42:49,659 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-12-16 12:42:49,659 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getValue() on null object
	at Script77.run(Script77.groovy:12)

 

 

Note: There are Multiple fields Named as Environment. That might be causing issue where it says Null value because Not all Environment fields has the sameValues listed.

Is there a way to change: ObjectByName to ObjectByID?

CustomField customField_1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("customField_1")

to

CustomField customField_1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByID("customField_1")

Vasiliy Zverev Community Champion Dec 17, 2016

Both methods return the same result - custom field.

It seems that there is no value for this field. Make sure of it.

 

Hi Vasiliy Zverev,

Thanks again for your continious effor on this item.

Meantime, I removed the conditiona dn tried to do only with Customfield and I still have the same issue, meaning the I am not getting the expected result.

Also, the environment Fiedl which I have in my project has the  correct value in its dropdown but, it throwing error message as it doesnot have dropdown value.

Thus, I tested your code with another field, and Im still at roadblock.

Thanks Again!

Vasiliy Zverev Community Champion Dec 19, 2016

Sorry, I forgot to detele line with issue variable initialisation. try this 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.groups.GroupManager
 
CustomField customField_1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("customField_1")
CustomField customField_2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("customField_2")

String option1 = ((Option) issue?.getCustomFieldValue(customField_1)).getValue()
String option2 = ((Option) issue?.getCustomFieldValue(customField_2)).getValue()
 
def user = issue?.getAssignee();
GroupManager groupManager = ComponentAccessor.getGroupManager()
 
if( (option1.equals("PROD")) || (option2.equals("Yes")) ){
 
        if (    (groupManager.isUserInGroup(user, "SDLC_Test"))
            &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_Dev"))
        ) {
            return false
        }
        else
            return true
}
else if(    (groupManager.isUserInGroup(user, "SDLC_Test"))
        &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_Dev"))
        &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_INT"))
        &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_UAT"))){
        return true
    }  
    else return false

Hi Vasiliy Zverev,

I still have a roadblock. Below is a code I copied from your and reformat as per my fields and values.

Here, I am choosing "Yes" from Sensative Data field. So I should be able to Assign a defect to a user who is in Group "SDLC_Test". But, I am able to assign. So this is an issue.

I did not tested this with environment field. Lets settle down the condition with one field 1st.

import com.atlassian.jira.component.ComponentAccessor

 import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.customfields.option.Option

import com.atlassian.jira.issue.fields.CustomField

 import com.atlassian.jira.security.groups.GroupManager

CustomField senti = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sensative Data")

//CustomField env = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Environment")

String sentid = ((Option) issue?.getCustomFieldValue(senti)).getValue()

//String envt = ((Option) issue?.getCustomFieldValue(env)).getValue()

def user = issue?.getAssignee();

GroupManager groupManager = ComponentAccessor.getGroupManager()

 if( (sentid.equals("Yes")) ) {   

//|| (envt.equals("PROD")) ){

 

        if (    (groupManager.isUserInGroup(user, "SDLC_Test"))

            //&&  (groupManager.isUserInGroup(user, "SDLC_Dev"))

        ) {

            return false

        }

        else

            return true

}

else if(    (groupManager.isUserInGroup(user, "SDLC_Test"))

        &&  (groupManager.isUserInGroup(user, "SDLC_Dev"))

        &&  (groupManager.isUserInGroup(user, "SDLC_INT"))

        &&  (groupManager.isUserInGroup(user, "SDLC_UAT"))){

        return true

    }  

    else return false

 

I put the above code in Create transition Validator tab. And got the below error.

 

Time (on server): Tue Dec 20 2016 12:43:26 GMT-0600 (Central Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2016-12-20 13:43:26,113 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2016-12-20 13:43:26,124 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script> groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.atlassian.jira.security.groups.DefaultGroupManager#isUserInGroup. Cannot resolve which method to invoke for [null, class java.lang.String] due to overlapping prototypes between: [interface com.atlassian.crowd.embedded.api.User, class java.lang.String] [interface com.atlassian.jira.user.ApplicationUser, class java.lang.String] at com.atlassian.jira.security.groups.GroupManager$isUserInGroup$0.call(Unknown Source) at Script162.run(Script162.groovy:20)

Vasiliy Zverev Community Champion Dec 20, 2016

This is the reason why I do not like groovy style for def. Try this version with variable clas specification

import com.atlassian.jira.component.ComponentAccessor
 import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
 import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser
CustomField senti = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sensative Data")
//CustomField env = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Environment")
String sentid = ((Option) issue?.getCustomFieldValue(senti)).getValue()
//String envt = ((Option) issue?.getCustomFieldValue(env)).getValue()
ApplicationUser user = issue?.getAssignee();
GroupManager groupManager = ComponentAccessor.getGroupManager()
 if( (sentid.equals("Yes")) ) {   
//|| (envt.equals("PROD")) ){
 
        if (    (groupManager.isUserInGroup(user, "SDLC_Test"))
            //&amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_Dev"))
        ) {
            return false
        }
        else
            return true
}
else if(    (groupManager.isUserInGroup(user, "SDLC_Test"))
        &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_Dev"))
        &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_INT"))
        &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_UAT"))){
        return true
    }  
    else return false

Vasiliy Zverev,

Sorry, I tried to put the above code in Validator of Create transition and got the error as below.

image2016-12-21 10:27:21.png

Vasiliy Zverev Community Champion Dec 22, 2016

Here is updated version:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.groups.GroupManager

CustomField senti = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sensative Data")
//CustomField env = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Environment")
String sentid = ((Option) issue?.getCustomFieldValue(senti)).getValue()
//String envt = ((Option) issue?.getCustomFieldValue(env)).getValue()
def user = issue?.getAssignee();
GroupManager groupManager = ComponentAccessor.getGroupManager()
if( (sentid.equals("Yes")) ) {
//|| (envt.equals("PROD")) ){

    if (    (groupManager.isUserInGroup(user, "SDLC_Test"))
    //&amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_Dev"))
    ) {
        return false
    }
    else
        return true
}
else if(    (groupManager.isUserInGroup(user, "SDLC_Test"))
        &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_Dev"))
        &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_INT"))
        &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_UAT"))){
    return true
}
else return false

Vasiliy Zverev,

I really do not understand this. I am not sure what is the issue. I am still not geting the expected result. I put the code in Post function of Create transition and the log says no Failure when it ran but I am not having the result. Can you pls help me?

image2016-12-23 14:28:51.png

Vasiliy Zverev Community Champion Dec 24, 2016

Hey, it is not a postfunction. It is a scripts validator. It prevent transition if condition is false. 

 

Vasiliy Zverev

Thanks much for your continious support.

I put the above code in Validator of Create Transition and worked perfect when I have one Customfield condition, but when I try to implement the code condition in both fields then its not working as expected. I have implemented your code to satisfy the condition in two of my fields but no luck.

Note: If I implement this code in 1 field at a time it is working fine.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.groups.GroupManager 

CustomField senti = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sensative Data")
CustomField env = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Environment")

String sentid = ((Option) issue?.getCustomFieldValue(senti)).getValue()
String envt = ((Option) issue?.getCustomFieldValue(env)).getValue()

def user = issue?.getAssignee();

GroupManager groupManager = ComponentAccessor.getGroupManager()

if( (sentid.equals("Yes")) || (envt.equals("PROD")) ){

    if (    (groupManager.isUserInGroup(user, "SDLC_Test"))
    	&&  (groupManager.isUserInGroup(user, "SDLC_Dev"))

    ) {
        return false
    }
    else
        return true
}
else if(    (groupManager.isUserInGroup(user, "SDLC_Test"))
        &&  (groupManager.isUserInGroup(user, "SDLC_Dev"))
        &&  (groupManager.isUserInGroup(user, "SDLC_INT"))
        &&  (groupManager.isUserInGroup(user, "SDLC_UAT"))){
    return true
}
else return false

 

There is no any error log loaded after running this script.

 

 

 

Vasiliy Zverev,

Update on the above issue!!!

I reformat the above code as below. And now If I choose Sensative data = Yes and Environment != Prod, Or if I do Sensative Data != Yes and Environment = Prod, the below code works just fine. Which is good.

Now the issue here are:

1) If I do Sensative data = Yes and Environment = Prod, the code doesnot give me the expected result, which means I am still able to assign defect to the user in SDLC_Test group.But infact I should not be able to assign to that user.

2) If I do Sensative Data = Yes and Try to Assign to a user of "SDLC_INT", I am not able to Assign the defect. Same issue is happening with Environment field.

But, this is fine for Users of"SDLC_Test".

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.groups.GroupManager 

CustomField senti = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sensative Data")
CustomField env = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Environment")

String sentid = ((Option) issue?.getCustomFieldValue(senti)).getValue()
String envt = ((Option) issue?.getCustomFieldValue(env)).getValue()

def user = issue?.getAssignee();

GroupManager groupManager = ComponentAccessor.getGroupManager()

if (govd.equals("Yes")){ 
if (envt.equals("PROD")) { 
    if (    (groupManager.isUserInGroup(user, "SDLC_Test"))
    	&amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_Dev"))

    ) {
        return false
    }
    else
        return true
}
}
else if(    (groupManager.isUserInGroup(user, "SDLC_INT"))
        &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_UAT"))){
    return true
}
else return false
Vasiliy Zverev Community Champion Dec 28, 2016

Note that variable govd is not defined, but sentid is not used. It seems that something is wrong.

Vasiliy Zverev

Hi Vasiliy Zverev,

Any update on this please?

Vasiliy Zverev Community Champion Jan 11, 2017

Lets try this one

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.groups.GroupManager

CustomField senti = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sensative Data")
CustomField env = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Environment")

String sentid = ((Option) issue?.getCustomFieldValue(senti)).getValue()
String envt = ((Option) issue?.getCustomFieldValue(env)).getValue()

Issue issue

String user = issue?.getAssigneeId();

GroupManager groupManager = ComponentAccessor.getGroupManager()

if (sentid.equals("Yes")){
    if (envt.equals("PROD")) {
        if (    (groupManager.isUserInGroup(user, "SDLC_Test"))
                &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_Dev"))

        ) {
            return false
        }
        else
            return true
    }   
}
else if(    (groupManager.isUserInGroup(user, "SDLC_INT"))
        &amp;&amp;  (groupManager.isUserInGroup(user, "SDLC_UAT"))){
    return true
}
else return false

Vasiliy Zverev

 

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 Thursday in Confluence

Three common content challenges + how to manage them

An efficient enterprise content management system, or ECM, is a must-have for companies that create work online (cough   cough, all companies). If content calendars, marketing plans, and bu...

86 views 0 6
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