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?
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.