why JiraAuthenticationContext.getUser returns null?

I made a quartz schedule. The mehtod getUser returns null. Why?And how to get user for searchService?

public class MyJob implements Job {
@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {		
		JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
		final User user = authenticationContext.getUser().getDirectoryUser();

}

2 answers

1 accepted

This widget could not be displayed.

You can set the user like this:

UserUtil userUtil = (UserUtil) ComponentAccessor.getComponentOfType(UserUtil.class);
ApplicationUser user = userUtil.getUserByName("some-user");

JiraAuthenticationContext jac = (JiraAuthenticationContext) ComponentAccessor.getComponentOfType (JiraAuthenticationContext.class);
jac.setLoggedInUser(user);

This widget could not be displayed.

While Shravan provides a workaround, an answer to the problem at hand is not given yet. As I bumped into this myself, I investigated and quickly came to the real reason: an authentication context is only available for code directly in the execution path of a web request, be it via the web portal or via REST interface.

If you want to run code asynchronously, then you are not tied to a particular user session so you don't get a user object from the authentication context. If user information is needed to handle your async code, either choose a fixed user, or pass the user info from the web-request to the async code.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted 11 hours ago in Canada

AUG Lunch Roundtable - August 13 - Highlights

Thanks to everyone who came out today for the lunch AUG - we had some great food and discussion (and ice cream sandwiches to fend off the heat!) The welcome and fun fact was great as well, what an aw...

27 views 1 3
View post

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