getLoggedInUser() does not work in Jira 7.1.4

The Custom "Scripted Field" from Scriptrunner is used to display external url link to the current issue. We do want to put other info in there, but for testing this will do. The script below is stripped to bare and works fine as shown.

Problem is we want it only to be placed in a ticket when the currentUser is a member of a certain group (ex: tpsc-intern). The userUtil construction to compare works. What doesnt work is de "def currentUser" line. From what we read its a version 6 solution and de new JIRA 7 uses the "jira.security" library now for ApplicationUser.getUser(), but i cannot import that one.

Can someone tell me how to fix the below script?

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.RendererManager
import com.atlassian.jira.ComponentManager
// new lib does not work
//import com.atlassian.jira.security

def componentManager = ComponentManager.getInstance()
def info = null

// should work? but doesnt
//def currentUser = ComponentAccessor.JiraAuthenticationContext.getLoggedInUser()
//if (! ComponentAccessor.userUtil.getGroupNamesForUser(currentUser.name).contains("tpsc-intern")) {

// works
//info = "groups: "+ComponentAccessor.userUtil.getGroupNamesForUser("tim")
info = "https://my.patientsafety.com/jira/rest/api/2/issue/"+issue.key

def rendererManager = ComponentAccessor.getComponent(RendererManager.class)
def fieldLayoutItem = ComponentAccessor.getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem("timeinfo")
def renderer = rendererManager.getRendererForField(fieldLayoutItem)
renderer.render(info, null)
//}

2 answers

1 accepted

0 vote

The only difference there is getting the current user, vs hardcoding the name "tim", I don't see why that would not work. Can you explain how it doesn't work? ie error, wrong result etc.

I would consider using:

def groupManager = ComponentAccessor.getComponent(GroupManager)
def currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
groupManager.getGroupNamesForUser(currentUser).contains(...)

the reason being is it lets you pass a user object not a name. 

Sometimes in the API they say pass a name, but I think they mean use a key.

Thanks that was a pointer to the solution. Problem was in the changes of version 7 concerning "User -> ApplicationUser" type changes. Fixed using:

 

def String username = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getName()

if (ComponentAccessor.userUtil.getGroupNamesForUser(username.toString()).contains('tpsc-intern')) {

 

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

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

119 views 0 5
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