Checking if security level can be set (Scriptrunner) Edited

Hi,

In a workflow post function I'm using Scriptrunner "Set security level to ...". Works fine under the conditions that the user has the right to set security level and the project has the right security scheme. Problem being it ignores the function without warnings to the user if the conditions are not met

I've tried to find a way to validate that with a Script Validator. So that you can't do the transition if this condition is not met. 

I'm not a great coder (!) and normally I find a snippet on the net and try to make it work for my needs. I haven't been able to find anything for this.

Anyone out there with an idea?

2 answers

1 accepted

1 vote
Henning Tietgens Community Champion Apr 05, 2017

Hi,

this is a method that could be used for checking if a security level id is valid for an issue.

Boolean isSecurityLevelValid(Issue issue, Long securityLevelId, Logger log = null) {
    def issueSecuritySchemeManager = ComponentAccessor.getComponent(IssueSecuritySchemeManager)
    def issueSecurityLevelManager = ComponentAccessor.issueSecurityLevelManager

 def srcProject = issue.projectObject
 def issueSecurityScheme = issueSecuritySchemeManager.getSchemeFor(srcProject)

 if (issueSecurityScheme) {
  def validLevels = issueSecurityLevelManager.getIssueSecurityLevels(issueSecurityScheme.id)
  log?.debug "..validLevels-IDs: ${validLevels*.id?.dump()}"
  return validLevels*.id.contains(securityLevelId)
 } else {
  log?.debug "..Doesn't find any IssueSecurityScheme for project ${srcProject.key}"
  return false
 }
}

Henning

Thank you,

I guess I'm going to have to have some imports on top of that?!

This code assumes that an Issue already exists? But if I'm going to use it in a validator in the create statement - that's probably not true?!

Henning Tietgens Community Champion Apr 05, 2017

Yes, you need some imports.

The projectObject of the issue is already defined for the create validator and the only thing used. So it should work.

Try this as a scripted validator after adapting SECURITY_LEVEL_ID to the correct id.

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.opensymphony.workflow.InvalidInputException

Long SECURITY_LEVEL_ID = 12345

if (!isSecurityLevelValid(issue, SECURITY_LEVEL_ID, log)) {
    invalidInputException = new InvalidInputException("Security level would not be valid.")
}

Boolean isSecurityLevelValid(Issue issue, Long securityLevelId, Logger log = null) {
    def issueSecuritySchemeManager = ComponentAccessor.getComponent(IssueSecuritySchemeManager)
    def issueSecurityLevelManager = ComponentAccessor.issueSecurityLevelManager

    def srcProject = issue.projectObject
    def issueSecurityScheme = issueSecuritySchemeManager.getSchemeFor(srcProject)

    if (issueSecurityScheme) {
        def validLevels = issueSecurityLevelManager.getIssueSecurityLevels(issueSecurityScheme.id)
        log?.debug "..validLevels-IDs: ${validLevels*.id?.dump()}"
        return validLevels*.id.contains(securityLevelId)
    } else {
        log?.debug "..Doesn't find any IssueSecurityScheme for project ${srcProject.key}"
        return false
    }
}

Henning

Thank you!

It works

I put it in a simple scripted validator - so i just discarded your initial if-sentence so it returns true/false.

 

Hello Henrik:

Could you please provide the code that you are using so that we have something to base our development on?

Cheers!

Hi DYelamos,

Thank you for responding. But I'm afraid I don't understand your question. What i'm trying to do is finding some code that validates if a security scheme/level is available. I need to run it in the validator part of create issue, I think I can use Script Validator for this.

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
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

108 views 0 2
Join discussion

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