scriptrunner groovy check whether user has permission in a project

Martina Riedel
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.
October 10, 2019

There are a lot of old answers out there and only a few obscure answers using the current API

https://community.atlassian.com/t5/Answers-Developer-Questions/Script-Runner-Post-Function-Permission-Checking/qaq-p/542213 is the old answer and it would be so cool to be able to just add a new answer.

1 answer

1 accepted

2 votes
Answer accepted
Martina Riedel
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.
October 10, 2019
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.plugin.ProjectPermissionKey

def permissionManager = ComponentAccessor.getPermissionManager()
def hasPerm = permissionManager.hasPermission(new ProjectPermissionKey("TRANSITION_ISSUES"), issue, currentUser)

 The list of permissions is in https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/permission/ProjectPermissions.html

This works in conditions and validators

Tom November 25, 2019

Hallo Martina,

I am unfortunately a beginner in groovy/java and can't get the code snippet to run.
I get the error "cant find matching method: ProjectPermissionKey" when I run it in ScriptRunner Console.

 

def currentUser = 'User1'
def issue = 'Test-1'

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.plugin.ProjectPermissionKey

def permissionManager = ComponentAccessor.getPermissionManager()
def hasPerm = permissionManager.hasPermission(ProjectPermissionKey("TRANSITION_ISSUES"), issue, currentUser)
//"ADD_COMMENTS"

 

What am I doing wrong?

Gruß Thomas

Martina Riedel
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.
November 25, 2019

Issue isn't just a string. In conditions and Validators it's properly set. In the console you have to get the issue object yourself.

see getIssueObject('your Issue ID') 

If you want this for a condition or validator, I just create a simple condition and do previews and asserts right in there to get it right.

Martina Riedel
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.
November 25, 2019

just found an example where I get an issue in teh Console:

 

def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueByCurrentKey(issueKey)
Tom November 26, 2019

Sorry, but I change it and its show the same error:

groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.ProjectPermissionKey() is applicable for argument types: (java.lang.String) values: [TRANSITION_ISSUES] at Script209.run(Script209.groovy:13)

I run JIRA Server 7.13.x

 

Unbenannt.png

Martina Riedel
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.
November 26, 2019

You are on your own then. Have you tried it in workflow?That's where this worked for me in 7.13

Tom December 10, 2019

I found a solution for me:

def permissionManager = ComponentAccessor.getPermissionManager()
if (permissionManager.hasPermission(ProjectPermissions.ADD_COMMENTS, issue, reporter) ){

xxx

}

Suggest an answer

Log in or Sign up to answer