automatically setting security level based on user group

I need to automatically set the security level of an issue based on the users group ,

i am writing a groovy post function for this using script runner

i have 2 security levels with ids 10000 and 10001

below is the groovy script

import com.atlassian.jira.ComponentManager  
import com.atlassian.jira.ManagerFactory  
import com.atlassian.jira.issue.security.IssueSecurityLevelManager  
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager  
import com.opensymphony.user.User  
import com.atlassian.jira.issue.IssueImpl
import org.apache.log4j.Category  
import com.atlassian.jira.issue.MutableIssue;
import org.ofbiz.core.entity.GenericValue  
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.bc.project.component.ProjectComponent;
import com.atlassian.jira.bc.project.component.ProjectComponentManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueFactory;
    
	
  ComponentManager componentManager = ComponentManager.getInstance() ; 
  IssueFactory issueFactory = componentManager.getIssueFactory();
  MutableIssue issue = issueFactory.getIssue();
  User currentUser = componentManager.getJiraAuthenticationContext().getUser()  
  if(currentUser.inGroup("jira-administrators"))
    {
		issue.setSecurityLevelId(10001)  
    }
  else if(currentUser.inGroup("jira-developers"))  
    {
		issue.setSecurityLevelId(10000)  
    }
  issue.store() 

i am getting an error :Passed List had more than one value.

please can someone guide me where i am going wrong ?

thanx in advance

9 answers

1 accepted

Are those security levels valid?

Does it work when you set the security level in the web UI?

Add some logging so you can see exactly where the failure is.

Hi Jamie ,

I am able to set the security level through UI , I actually took help from your blog . When i run the exact script which you have provided there i get this error in log Could not find security level for Private . I guess i am missing something important in my script .


I added some logging in my script and found that the issue object i get is null , i commented few lines and the script works fine .

	log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")  
log.debug("PostFunction function running")
  ComponentManager componentManager = ComponentManager.getInstance() ; 
  //IssueFactory issueFactory = componentManager.getIssueFactory();
  //log.error("user admin $issueFactory")
  //MutableIssue issue = issueFactory.getIssue();
  //log.error("user admin $issue")
  User currentUser = componentManager.getJiraAuthenticationContext().getUser()  
  if(currentUser.inGroup("jira-administrators"))
  {
		log.debug("user admin")
		issue.setSecurityLevelId(10001)  
  }
  else if(currentUser.inGroup("jira-developers"))  
  {
	log.debug("user developer")
	issue.setSecurityLevelId(10000)  
    }
 

Thanx :)

Great, well done. Yes, issue is passed in the binding to your script so you don't need to get it or declare it.

issueFactory.getIssue gets an empty issue - use this to create a new issue.

it should be issue.setSecurityLevelId(secLevelGv.id)

Thanks fixed my problem via issue.setSecurityLevelId(10100)

IssueSecuritySchemeManager issueSecuritySchemeManager = ManagerFactory.getIssueSecuritySchemeManager();

IssueSecurityLevelManager issueSecurityLevelManager = ManagerFactory.getIssueSecurityLevelManager();

srcProjectGV = issue.getProject();

issueSecurityScheme = issueSecuritySchemeManager.getSchemes(srcProjectGV)

secLevelGv = issueSecurityLevelManager.getSecurityLevelsByName("Internal");

issue.setSecurityLevelId(secLevelGv.id);

// Do not use $ or [] here it should NOT be issue.setSecurityLevelId($secLevelGv.id) or issue.setSecurityLevelId([secLevelGv.id])

issue.store();

And you are done...!!!!

I can set issue security as a script from workflow post fuction in Jira 5.2.x

It doesn't work with Jira 5.x.
What must i change?

Regards,
Tim

Hi!

I know its a little bit late but...:

import com.atlassian.jira.ComponentManager  
import com.atlassian.crowd.embedded.api.*  
import org.ofbiz.core.entity.GenericValue  
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.security.groups.GroupManager    
	
  ComponentManager componentManager = ComponentManager.getInstance()  
  User currentUser = componentManager.getJiraAuthenticationContext().getUser()
  GroupManager groupManager = componentManager.getComponentInstanceOfType(GroupManager.class)
    
 if(groupManager.isUserInGroup(currentUser.name, "jira-administrators"))
    {
  		issue.setSecurityLevelId(10001)  
    }
  else if(groupManager.isUserInGroup(currentUser.name, "jira-developers"))  
    {
		issue.setSecurityLevelId(10000)  
    }

Does this work on Jira 7.5? I want to use this code as a Script Listener.

How do I add the script to work in jira 5.2? Where did you place the script?

Then I tried

secLevelGv = issueSecurityLevelManager.getSecurityLevelsByName("Internal");

issue.setSecurityLevel([secLevelGv]);

Now the error is

Caused by: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setSecurityLevel() is applicable for argument types: (java.util.ArrayList) values: [[[[id:10152, scheme:10040, description:Only for Jira Admins, ...]]]]

Possible solutions: setSecurityLevel(org.ofbiz.core.entity.GenericValue), getSecurityLevel(), setSecurityLevelId(java.lang.Long), getSecurityLevelId()

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)

... 198 more

Caused by: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setSecurityLevel() is applicable for argument types: (java.util.ArrayList) values: [[[[id:10152, scheme:10040, description:Only for Jira Admins, ...]]]]

at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)

at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)

at Script37.run(Script37.groovy:66)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:315)

... 199 more

Yes, It is breaking in Jira 5.2.11

ERROR:

Caused by: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setSecurityLevel() is applicable for argument types: (java.lang.Integer) values: [10152]

Possible solutions: setSecurityLevel(org.ofbiz.core.entity.GenericValue), getSecurityLevel(), setSecurityLevelId(java.lang.Long), getSecurityLevelId()

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)

... 198 more

Caused by: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setSecurityLevel() is applicable for argument types: (java.lang.Integer) values: [10152]

Possible solutions: setSecurityLevel(org.ofbiz.core.entity.GenericValue), getSecurityLevel(), setSecurityLevelId(java.lang.Long), getSecurityLevelId()

Could you please help with it?

I tried

issue.setSecurityLevel(10152);
issue.setSecurityLevelID(10152);

The problem is in the error message... check the list list is not empty and then do:

issue.setSecurityLevel(secLevelGv.first());

it is giving an error

Caused by: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.lang.String.first() is applicable for argument types: () values: []

Possible solutions: print(java.io.PrintWriter), print(java.lang.Object), find(), find(java.lang.String), find(java.lang.CharSequence), find(java.util.regex.Pattern)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)

... 198 more

Caused by: groovy.lang.MissingMethodException: No signature of method: java.lang.String.first() is applicable for argument types: () values: []

at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)

at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)

at Script2.run(Script2.groovy:74)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:315)

... 199 more

I even tried hard coding the value as and then pass it to set but no luck

secLevelGv = ("10152");
issue.setSecurityLevel(secLevelGv.first());

or

issue.setSecurityLevel([secLevelGv])

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 10 hours ago in United States

Administrators Cleanup

Good morning, After a long weekend cleaning up, I am still in the cleanup mode 😊. I am curious to know: When do you feel the urgency to cleanup your Atlassian Tools? How many times do y...

15 views 0 0
View post

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