Validate whether user is part of group a||group b when item is created via simple scripted validator

Luke November 22, 2019

My client wants to restrict item creation to people of two teams.

Due to the circumstances I can't work with simple permissions.

The client is using scriptrunner for other projects; So my initial thought was to leverage that.

By using a simple scripted validator in the "create" transition I want to check whether the current user is part of group a or group b, if it validates to true, the item can be created.

 

I am a total groovy script noob, so naturally I've done some digging and came up with the following groovy "script":

import com.atlassian. jira.components.ComponentAccessor
if(ComponentAccessor.getGroupManager().isUserInGroup(currentUser, "group name a")){}
else if(ComponentAccessor.getGroupManager().isUserInGroup(currentUser, "group name b")) {}

 Which does not work.

Regardless of it working as intended or not, it also doesn't display the error message I provided in the ssv.

 

What am I doing wrong? Is there a better solution, do I just need to adjust the script?

1 answer

1 vote
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 22, 2019

Hi @Luke,

you can do that using below steps --> Script Validator --> Simple Script Validator

source: code

Error: your error message

Field: Issue Type

 

I'm attaching snippet for your reference, here I'm not allowing "Group-A" members to create "Story", you can modify this code according to your requirement

import com.atlassian.jira.component.ComponentAccessor 

def groupManager = ComponentAccessor.getGroupManager()

if(groupManager.isUserInGroup(issue.reporter?.name, 'Greoup-A') && !issue.issueType.name == "Story"){
return true
}else{
return false
}
 

 

BR,

Leo

Luke November 22, 2019

Hi Leo

 

Thanks for your input.

I have adapted my simple scripted validator to your code, exchanging some parts and leaving others out.

After publishing the workflow on the clients test environment, it successfully blocks me from creating an issue, since I'm in neither of the groups. But the error message still isn't getting displayed, it just doesn't create the issue; Just like when you leave a required field empty.

I am starting to think that it might be an issue with the clients setup of jira and scriptrunner itself.

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 24, 2019

Hey, 

if you are using "Simple Script validator", have you filled below 2 fields?

1. Error Message:(error to display users)

2. Field:(under which field error message should appear)

if you are using "Custom Script validator", use below snippet instead of "return false" statement

import com.opensymphony.workflow.InvalidInputException // importing exception page to handle

throw new InvalidInputException("custom error message") // instead of return false

 

BR,

Leo 

Luke December 12, 2019

Hi Leo

I am using the "simple scripted validator", and yes I've got the two fields "error message" and "field" set to the respective values.

The validation seems to work, as it does not allow me to create items anymore (since I'm not part of that usergroup), but there's never an error message.

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 12, 2019

You can try with custom error handling, maybe my previous update can give you some idea/reference 

Suggest an answer

Log in or Sign up to answer