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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

758 views 2 19
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