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

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);

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Marketplace Apps

Tips on how to choose the best estimation method for your planning

Planning and grooming sessions all come with their own sets of rules. Team members meet to estimate stories or other work items, all according to an agreed-upon process. And with every session comes ...

77 views 0 11
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