JQL search with a different user when scripting: Scriptrunner

I am trying to change the user when doing a JQL search for a scripted field. I realizd that the current user will not be able to browse the project so I would need to use a user that is able to browse the project. A piece of my script can be seen below.  I would like to change the def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() to a different user. As an example, maybe the user name can be JohnDoe

maybe

def user = ComponentAccessor.getJiraAuthenicationContext().getUserByName("JohnDoe")

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.web.bean.PagerFilter;
import java.lang.Object
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import static java.lang.Math.*
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)


boolean check =true


String jqlVaca = 'project = HR AND issuetype = "Employee Information"';
log.debug("Get filter string")

// Get a search-Object for JIRA
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issuesInfo
SearchService.ParseResult parseResultVaca =  searchService.parseQuery(user, jqlVaca)


if (parseResultVaca.isValid()) {
   	 issuesInfo = (searchService.search(user, parseResultVaca.getQuery(), PagerFilter.getUnlimitedFilter()).getIssues())
log.debug("Parse is Valid")
} 
else
	check =1;

 

1 answer

This widget could not be displayed.

 

JiraAuthenticationContext

It has the function "setLoggedInUser(ApplicationUser user)". 
This is used in places like Jelly where we need to switch the identity of a user during execution.

To get the user i think you need UserManager and the getUserByName (or Key) function.

 I never used it but this should be the function you need.

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...

425 views 3 17
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