Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Runner Behavior is not working

JIRA sys-admins April 8, 2019

Hello Team,

I am trying to restrict Issue Type= Internal Defect for one group, so that people under this group will only have access to Internal Defect Issue type. Therefore i am using below code Initializer :

{code}

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

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

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def allIssueTypes = ComponentAccessor.constantsManager.allIssueTypeObjects

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueTypeField = getFieldById(ISSUE_TYPE)

def remoteUsersRoles = projectRoleManager.getProjectRoles(user, issueContext.projectObject)*.name
def availableIssueTypes = []

if ("atau-g-jira_bmm" in remoteUsersRoles) {
availableIssueTypes.addAll(allIssueTypes.findAll { it.name in ["Internal Defect"] })
}

if ("jira-users" in remoteUsersRoles) {
availableIssueTypes.addAll(allIssueTypes.findAll { it.name in ["Build Revision - MK6", "Risk", "Field Defect", "Build Revision - Gen7/GDK/Gen8 EGM"] })
}

issueTypeField.setFieldOptions(availableIssueTypes)

{code}

 

Even after this code, user under group *atau-g-jira_bmm* is still able to see/select all issues types. I am not sure what went wrong. Can you please suggest the solution.

 

Regards,
Manoj

2 answers

0 votes
PD Sheehan
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.
April 9, 2019

If I understand correctly, it looks like you are interested in limiting issue type access based on the jira group membership (jira-users vs atau-g-jira_bmm).

However,  your code is actually looking at the project role membership.

You can see that by sending your list of remoteUsersRoles to the log.

If you want to get the current user's general group membership (regardless of role on the current project), you can have a look at this other post: https://community.atlassian.com/t5/Marketplace-Apps-questions/jira-scriptrunner-user-belongs-to-group/qaq-p/640102

0 votes
Antoine Berry
Community Champion
April 9, 2019

Hi,

This script is which roles the current user belongs to. So you cannot test groups against "remoteUsersRoles", but you should use roles rather.

Also, I added some imports that fixed some execution errors : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.issuetype.IssueTypeImpl
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE

@BaseScript FieldBehaviours fieldBehaviours

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def allIssueTypes = ComponentAccessor.constantsManager.allIssueTypeObjects

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueTypeField = getFieldById(ISSUE_TYPE)

def remoteUsersRoles = projectRoleManager.getProjectRoles(user, issueContext.projectObject)*.name
log.error("remoteUsersRoles : " + remoteUsersRoles)
def availableIssueTypes = []

if ("Administrators" in remoteUsersRoles) {
availableIssueTypes.addAll(allIssueTypes.findAll { it.name in ["Internal Defect"] })
}

if ("Developers" in remoteUsersRoles) {
availableIssueTypes.addAll(allIssueTypes.findAll { it.name in ["Build Revision - MK6", "Risk", "Field Defect", "Build Revision - Gen7/GDK/Gen8 EGM"] })
}

issueTypeField.setFieldOptions(availableIssueTypes)

Now the script is still not working on my instance : 

 [c.o.j.groovy.user.FormField] Unsupported type class com.atlassian.jira.issue.issuetype.IssueTypeImpl in setFieldOptions()

And I was not able to resolve it. So if you do, please let me know. Otherwise maybe raise a support ticket to adaptavist.

Antoine

Antoine Berry
Community Champion
April 16, 2019

Hi @JIRA sys-admins ,

Did that help at all ?

Like Zita Bagi likes this
Zita Bagi
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.
February 28, 2020

Hi Antoine_Berry, I tried this, I tried limiting the available issue types based on project key, and it doesnt't fully work. For eg there is an issue type called "Project" which can be opened by people only in project role Administrator / Team Leaders, but the script doesn't allow anyone or any role to open "Project" issue type.

Antoine Berry
Community Champion
February 28, 2020

Hi @Zita Bagi ,

It will be hard to help if you do not share your code. Also consider opening a new question so it has more visiblity and everyone can help. :)

Zita Bagi
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.
February 28, 2020
Antoine Berry
Community Champion
February 28, 2020

Nice, I accepted her answer on this question so it has more visiblity. :)

Like Zita Bagi likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events