Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

automatically setting security level based on user group

Mizan
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.
January 17, 2012

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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
JamieA
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.
January 17, 2012

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.

Mizan
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.
January 19, 2012

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 .


Mizan
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.
January 19, 2012

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 :)

JamieA
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.
January 19, 2012

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.
2 votes
Amit Girme April 29, 2014
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

1 vote
Amit Girme April 29, 2014

it should be issue.setSecurityLevelId(secLevelGv.id)

Alexander Fedtke March 31, 2016

Thanks fixed my problem via issue.setSecurityLevelId(10100)

1 vote
Tim Eddelbüttel
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.
December 4, 2012

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

Regards,
Tim

Vidic Florjan
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.
May 13, 2013

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)  
    }
Like # people like this
Stefani Kaimakliotou February 12, 2019

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

0 votes
Amit Girme April 27, 2014

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])

0 votes
JamieA
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 25, 2014

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

issue.setSecurityLevel(secLevelGv.first());

0 votes
Amit Girme April 25, 2014

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);

0 votes
Amit Girme April 25, 2014

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

0 votes
M February 16, 2014

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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events