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

 

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
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
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

74 views 0 5
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