Jira JAVA API - get list of all JIRA users

Daniel Burke December 13, 2017

Does anybody have any experience getting a list of all of the users in their JIRA application?

I need to write a plugin for this. I currently have some code:

package com.bosch.plugin.user.list.api;

import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.api;
import com.bosch.jira.timeinstatus.AtlassianPluginsTestRunner;
import com.bosch.jira.timeinstatus.RunWith;
import com.bosch.plugin.user.list.impl.MyPluginComponentImpl;
import com.atlassian.jira.web.action.admin.user.UserBrowser;
import com.sun.istack.internal.Nullable;
import com.atlassian.jira.user.UserKeyService;

import java.lang.String;
import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.*;


@RunWith(AtlassianPluginsTestRunner.class)
public class ListUsers implements User
{

private String name;
private String userName;
private long directoryId;
private String emailAddress;
private boolean isActive;
private int userCount;
private final JiraAuthenticationConext;
private final SearchRequest;


public void validator(String name, String userName, int userCount, long directoryId, String emailAddress, boolean isActive, final JiraAuthenticationContext authenticationContext, final SearchRequest searchRequest)
{
this.name = name;
this.userName = userName;
this.emailAddress = emailAddress;
this.isActive = isActive;
this.userCount = userCount;
this.JiraAuthenticationService = authenticationContext;
this.searchRequest = searchRequest;
}

public static void main(String[] args) {
System.out.println("entry point of app");
}

//compare user name
public int compareTo(User user){
return name.compareTo(user.getName());
}

//getter and setter for name
public String getName(){
return name;
}

//getter and setter for user name
public String getuserName(){
return userName;
}
//getter and setter for email
public String getEmailAddress(){
return emailAddress;
}
//getter and setter for directory ID in JIRA
public long getDirectoryId(){
return directoryId;
}
//getter and setter for checking if user is active
public boolean isActive(){
return isActive;
}

public int getUserCount(){
return userCount;
}

public Query retrieveOrMakeSearchRequest(User loggedInUser, Project currentProject){
loggedInUser = authenticationContext.getLoggedInUser();
SearchRequestManager searchRequestManager = ComponentManager.getInstance().getSearchRequestManager();
SearchRequest src=searchRequestManager.getSearchRequestById(loggedInUser);


return null;
}

public ArrayList<User> getAllUsers(){
// Query query = retrieveOrMakeSearchRequest(loggedInUser, currentProject);
ArrayList<User> all = new ArrayList<User>(getAllUsers());
return all;
}

public ArrayList<User> getAllUsersInGroups(Collection<String> arg0){
ArrayList<User> allUsersandGroups = new ArrayList<User>(getAllGroups());
return allUsersandGroups;
}

public ArrayList<User> getAllGroups(){
ArrayList<User> allGroups = new ArrayList<User>(getGroups());
return allGroups;
}
}

 

Is this the write way about getting all of my users in the JIRA application? Can somebody help or point me in the right direction?

Thanks! 

1 answer

0 votes
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 13, 2017
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.bc.user.search.UserSearchParams


def userSearchService = ComponentAccessor.getComponent(UserSearchService.class);
UserSearchParams userSearchParams = (new UserSearchParams.Builder()).allowEmptyQuery(true).includeActive(true).includeInactive(true).maxResults(100000).build();

userSearchService.findUsers("", userSearchParams).each{ it->
  log.error(it.getKey())
}
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 13, 2017

It is a groovy script. But I think you can convert it to Java. If not then tell me, I ll do it for you

Daniel Burke December 13, 2017

Thanks, I haven't used Groovy before, would you mind showing me? 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 13, 2017
 UserSearchService userSearchService = ComponentAccessor.getComponent(UserSearchService.class);
 UserSearchParams userSearchParams = (new UserSearchParams.Builder()).allowEmptyQuery(true).includeActive(true).includeInactive(true).maxResults(100000).build();

        for (ApplicationUser cuser : userSearchService.findUsers("", userSearchParams)) {
            
                // do your code here
        }
Daniel Burke December 13, 2017

Excellent. Thank you!

Daniel Burke December 21, 2017

Hello Alexey, do you have the POM dependencies that would be required for this?

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 21, 2017

I do not think there must any special dependencies. If you have any problems to build the project, kindly provide the logs.

Harinath T December 27, 2017

Hi Alexey Matveev 

I need help on making requests to JIRA from external application,

I followed Basic Authentication and OAuth, but here problem is I have to use username/password to make request on JIRA every time. 

And Basic Authentication is not what I need because the requests must be made in the name of the currently logged-in user (here logged-in user is from external app user).

And with OAuth: Here if user does some update on issue then we want to know who is performing the action (transitions and updations). And it will be difficult to ask individual users to register their own tokens. 

 

Here my expectation is to make requests on JIRA from external application who is logged-in.

Please give me a better solution to make requests. 

Thanks in Advance.

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 27, 2017

Hello,

You need to use SSO. I had such experience with Kerberos

Harinath T December 27, 2017

Thanks many for the quick reply, if possible can you please explore how can I use SSO to make requests on JIRA API.

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 27, 2017

You need to put all logic in your external application. You need to use api for the sso provider to recieve the authentication ticket from the sso provider. Then you send the ticket to Jira using the providers Api.

For example, let s say you use Kerberos. Then you have to install an add-on to Jira which can hanlde Kerberos authentication. then for example you use java application to make calls to Jira. You would need to use a java package for Kerberos authentication. Then you would make a call to Jira Rest Api using the java kerberos package. It would do everything for you.

Harinath T December 27, 2017

Thank you so much, I will try this, and question is if I use Kerberos, do I need to provide any header information on making requests to JIRA. I mean username/password in the request header. 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 27, 2017

No, you do not pass it.

That is the whole point of SSO. If you use Kerberos, you authenticate yourself while logging to your pc. That is all. After it you only get tickets from the key distribution server. 

Lars Olav Velle
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 27, 2017

@Harinath T I work for Kantega Single Sign-on.

With our add-on you can optionally enable Kerberos for REST.

 

With Kerberos you use the currently logged in users credentials and not a provided username/password.

Here are two examples using curl:

Kerberos:
curl -D- --negotiate -u: -X GET -H "Content-Type: application/json" "https://bitbucket.example.com/rest/api/1.0/users/"

Basic:
curl -D- -u mark.miller:MyNotSoSecretPassword -X GET -H "Content-Type: application/json" "https://bitbucket.example.com/rest/api/1.0/users/"

 

-Lars

Suggest an answer

Log in or Sign up to answer