Add group to security level Groovy

 

I've been unable to find a method in the Jira 7 API allowing me to add groups to a security level. Any help would be much appreciated,

 

Thanks

3 answers

1 accepted

So I was actually able to accomplish this with the following code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.scheme.SchemeEntity

def issueSecurityLevelManager = ComponentAccessor.getIssueSecurityLevelManager()
def issueSecuritySchemeManager = ComponentAccessor.getComponent(IssueSecuritySchemeManager)

def scheme = issueSecuritySchemeManager.getSchemeObject("scheme 1") //Name of scheme

def levels = issueSecurityLevelManager.getIssueSecurityLevels(scheme.id)
levels.each{
def entity = new SchemeEntity("group", "jira-users", it.id)
def gv = issueSecuritySchemeManager.getScheme(scheme.id)
issueSecuritySchemeManager.createSchemeEntity(gv, entity)
}

The above code takes a scheme, traverses all of its levels, and adds the "jira-user" group to each one. Now, I assume this isn't exactly what you'd like to happen in your instance, but, as I don't know your exact requirements, I figured it would be a good example. The biggest problem with the code in the above answers is this method here: 

def gv = issueSecuritySchemeManager.getIssueSecurityLevelScheme(scheme.id)

 As I'm sure you've found out, that method doesn't actually return a GenericValue, which is necessary for the "createSchemeEntity" method. I searched for quite some time looking for a way to make that method work, but I didn't have much luck so you may just have to use the deprecated method "getScheme" instead. But regardless, it should get the job done.

Best,

Aidan 

here's 95% of it, can't get the final statement to work, taken from https://community.atlassian.com/t5/JIRA-questions/Groovy-to-automatically-add-a-few-hundreds-of-Security-levels-to/qaq-p/72941

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.scheme.SchemeEntity
import com.atlassian.jira.scheme.SchemeManager
import org.ofbiz.core.entity.GenericValue

def issueSecurityLevelManager = ComponentAccessor.getIssueSecurityLevelManager()
def issueSecuritySchemeManager = ComponentAccessor.getComponent(IssueSecuritySchemeManager)

def scheme = issueSecuritySchemeManager.createSchemeObject("FRIDAY", "Issue security scheme for testing")
def level = issueSecurityLevelManager.createIssueSecurityLevel(scheme.id, "Private", "Group only")
def entity = new SchemeEntity("group", "jira-users", level.id)
def gv = issueSecuritySchemeManager.getIssueSecurityLevelScheme(scheme.id)
SchemeManager.createSchemeEntity(gv, entity)

doesn't work

 

for the above to work in 7.3.1 I need to supply a scheme of type GenericValue to createSchemeEntity(). I tried to cast but no luck:

 

IssueSecurityLevelScheme gv = issueSecuritySchemeManager.getIssueSecurityLevelScheme(issueSecurityScheme.id)
issueSecuritySchemeManager.createSchemeEntity(gv as GenericValue, entity)

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.atlassian.jira.issue.security.IssueSecurityLevelScheme@59f1a670' with class 'com.atlassian.jira.issue.security.IssueSecurityLevelScheme' to class 'org.ofbiz.core.entity.GenericValue'
 at Script253.run(Script253.groovy:25)

Can anybody assist?

Even I couldn't get closer than this - I give up for the moment :/ :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.issue.security.IssueSecurityTypeManager
import com.atlassian.jira.security.type.SecurityType
import com.atlassian.jira.issue.security.IssueSecurityLevelPermission

def group = ComponentAccessor.getGroupManager().getGroup ("jira-users")
def securityLevelManager = ComponentAccessor.getIssueSecurityLevelManager()
def issueSecuritySchemeManager = ComponentAccessor.getComponent(IssueSecuritySchemeManager.class)
def issueSecurityTypeManager = ComponentAccessor.getComponent(IssueSecurityTypeManager.class)

def issueSecurityTypes = issueSecurityTypeManager.getSchemeTypes()
def issueSecurityType = issueSecurityTypeManager.getSchemeTypes()["group"]

def issueSecurityLevelScheme = issueSecuritySchemeManager.getIssueSecurityLevelSchemes().find {
it.name == "Example Security Level"
}

def securityLevel = securityLevelManager.getIssueSecurityLevels(issueSecurityLevelScheme.id).find {
it.name == "Level 1"
}

From this point, following steps might give you a clue.

  1. Enable profiling in JIRA.
  2. Add a group to a security level.
  3. Disable profiling.
  4. Inspect atlassian-jira.log carefully with keywords like store, set, permission, issuesecuritylevelpermission, entity, etc. - this might give you a clue.

Regards,

Shaakunthala

profiling flags up:

 

/secure/admin/AddIssueSecurity.jspa [c.a.util.profiling.UtilTimerStack] [15ms] - /jira/secure/admin/AddIssueSecurity.jspa
[14ms] - AddIssueSecurity.execute()

/secure/admin/EditIssueSecurities!default.jspa [c.a.util.profiling.UtilTimerStack] [90ms] - /jira/secure/admin/EditIssueSecurities!default.jspa
[0ms] - EditIssueSecurities.execute()

 not sure what one would do with it though, api docs don't say much

doesn't work in 7.3.1 unfortunately

I get the following error: 

groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.scheme.SchemeManager.createSchemeEntity() is applicable for argument types: (org.ofbiz.core.entity.GenericValue, com.atlassian.jira.scheme.SchemeEntity) 

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 Jira Service Desk

How the Telegram Integration for Jira helps Sergey's team take their support efficiency to the bank

...+ reading Fantasy). The same is true for him at the bank he works for: Efficiency is key when time literally equals money. Read on to learn how Sergey makes most of the time he has by...

340 views 2 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