why JiraAuthenticationContext.getUser returns null?

Arkadiusz Zychewicz March 24, 2014

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

1 vote
Answer accepted
Shravan P March 24, 2014

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

2 votes
Ringo De Smet December 7, 2014

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
TAGS
AUG Leaders

Atlassian Community Events