Get a list of users in JIRA from within a JIRA plugin

First post here so fingers crossed.
I’m writing a JIRA plugin which needs to present a list of Users to an Admin user. I’m using UserManager to check that the admin user has logged in but the UserManager appears to only have methods for a single user.
I have tried the REST API but using this from within the plugin java code requires credentials (e.g. BASIC AUTH) to be added to the request header and unless there are ‘service’ accounts, I’m sure I cant retrieve the admin user’s name and password to inject into the request header.
I’m using JIRA v 7.2.1 and can find no way to just get the list of users (which would seem to be a typical task).
I’ve looked briefly at the DefaultUserPickerSearchService which appears to have the method calls I need - but its constructor has 13 parameters!
Or do I need to call the database directly with a sql query?

Hi John,

You can use this to check whether the current user has logged in

        ApplicationUser currentUser = jiraAuthenticationContext.getLoggedInUser();
        if (!globalPermissionManager.hasPermission(GlobalPermissionKey.SYSTEM_ADMIN, currentUser))
        {
           //user does not have system_admin rights
            return "denied";
        }
ComponentAccessor.getUserUtil().getAllApplicationUsers();

though, its deprecated in current versions. there are other methods, eg, if you have a common group:

			final CrowdService crowdService = ComponentAccessor.getCrowdService();
            final MembershipQuery<User> membershipQuery = QueryBuilder.queryFor(User.class, EntityDescriptor.user()).childrenOf(EntityDescriptor.group()).withName(groupName).returningAtMost(EntityQuery.ALL_RESULTS);
            Iterable<User> goodGriefReallyAllOfThem = crowdService.search(membershipQuery);

Thanks for your reply Huasoon but I was not really looking to check whether a user had logged in or not, it was more to do with getting a list of all users (or a subset of the list as I understand there can be thousands in some installations and it can be a performance drain).

Hi Andy thanks for your reply. I’d rather not use deprecated methods but following your suggestions led me to the UserSearchService - I’ll have a look at that to see if it does what I need. Thanks.

Hi @johnsmithweb,

You can try UserSearchService.findUsersAllowEmptyQuery(). This method only returns active users.

Thanks,
Anne

1 Like

Thanks Anne, that looks like it will do what I need. One last questions: what’s the xml snippet for the pom to include this dependency for injection? Also, is there a list of POM dependency snippets anywhere? Whenever I try to add a dependency I have no idea what the groupId and the artifact id should be for these things.

You can annotation to import UserSearchService - provided by jira-api AFAIK, so should be there already.

@Scanned
public class YourClass {

    @ComponentImport
    private final UserSearchService userSearchService;

    @Inject
    public YourClass (UserSearchService userSearchService){
        this.userSearchService =  userSearchService;

       // use it as @acalantog suggested =]
    }
}

Hi Diego,
I tried your suggestion and maven wouldn’t compile my project - it couldn’t find the UserSearchService.
I already use @ComponentImport on constructor parameters and they are injected okay - provided the dependency is set up in the POM, which was what I was looking for.

I hope you have found the answer you were looking for John, if not…

This is the main dependency

    <dependency>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-api</artifactId>
        <version>${jira.version}</version>
        <scope>provided</scope>
    </dependency>

UserSearchService is provided by it and described here https://docs.atlassian.com/jira/server/com/atlassian/jira/bc/user/search/UserSearchService.html

Good luck

Thanks Diego, I already had that dependency in my pom.xml but it always complained that it couldn’t find it when I used @ComponentImport in my constructor. I resorted to adding it to my MyPluginComponentImpl class and now it works fine. I think this is a bit of a backdoor hack, but it works.

Weird, were you setting the import in the interface?

Anyway, glad you found a way out :slight_smile: