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 votes

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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,026 views 12 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot