getLoggedInUser() does not work in Jira 7.1.4

Tim Twisk May 2, 2016

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
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.
May 3, 2016

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.

0 votes
Tim Twisk May 8, 2016

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