How do I change user context in scripted field?

I have a scripted field which basically looks at a custom field on a different issue in a different project.  I want to have the scripted field run under a different user context since the logged in user won't have access to browse the source project.

Here's what I have so far:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter

def customFieldManager = ComponentAccessor.customFieldManager
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)

// get value from custom field on current issue
def myID = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Secret Project ID")).toString();

// this gets logged in user, but I need to change to a named user context
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// run jql to get the issue with same Secret Project ID
def query = jqlQueryParser.parseQuery("project= SECRET and 'Secret Project ID' ~ '${myID}'")
def issuesFound = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

// there should only ever be one result, but just in case, only take first one
return issuesFound?.issues[0]?.summary


 

How can I have this run under a different named users context?

1 answer

1 accepted

This widget could not be displayed.
Gaston Valente Community Champion Jul 26, 2017

Hi David,

Try if this works for you:


JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();
ApplicationUser origUser = authContext.getLoggedInUser();

try{

authContext.setLoggedInUser(ComponentAccessor.getUserManager().getUserByKey("admin"));

//do stuff

}catch(Exception e){
e.printStackTrace();
}finally {
authContext.setLoggedInUser(origUser);
}

That worked.  I'll be honest, I get confused a little between Java syntax and Groovy syntax.  Here's what I came up with:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter

def customFieldManager = ComponentAccessor.customFieldManager
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)

// identify parameter for JQL and create JQL
def myID = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Secret Project ID")).toString();
def query = jqlQueryParser.parseQuery("project = SECRET and 'Secret Project ID' ~ '${myID}'")

// capture context of original user
def authContext = ComponentAccessor.getJiraAuthenticationContext()
def origUser = authContext.getLoggedInUser()

// switch context to named user
authContext.setLoggedInUser(ComponentAccessor.getUserManager().getUserByKey("different_user"))
def user = authContext.getLoggedInUser()

// search issues
def issuesFound = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())
def field = issuesFound?.issues[0]?.summary

// switch back to original user
authContext.setLoggedInUser(origUser);

return field

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

486 views 3 18
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