Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

userManager : getAllUsers() & getAllApplicationUsers() difference ?

Quentin Mahé April 9, 2020

Hello everyone,

I'm working on a simple script that should return a list of all JIRA users and their last login date.

I've searched this place for a bit and found that if someone wanted to get a list of all JIRA users, the 2 methods in the title were the way to go.

But I can't find what's exactly the difference between those 2. I've tried on different servers (prod, preprod and so on), and they would both give me the same amount of users. In the end I can use any, but I would still like to use the right methods for my script.

Thanks !

PS : I know they're deprecated but I do need a list of ALL users so it sadly is the way to go (may change in the future tho)

1 answer

1 accepted

3 votes
Answer accepted
Hana Kučerová
Community Champion
April 9, 2020

Hi @Quentin Mahé ,

welcome to the community!

I've studied the documentation and it seems to me, that the methods are the same. I would use getAllApplicationUsers, because it is newer.

I've tried to find another way to get all users. It returns both active and inactive users. How many users do you have in your instance?

import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

UserSearchParams.Builder paramBuilder = UserSearchParams.builder()
.allowEmptyQuery(true)
.includeActive(true)
.includeInactive(true)

ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
JiraServiceContextImpl jiraServiceContext = new JiraServiceContextImpl(loggedInUser)

Collection<ApplicationUser> users = ComponentAccessor.getComponent(UserSearchService).findUsers(jiraServiceContext, "", paramBuilder.build())

Would it work in your case? Please let me know. Thank you.

Quentin Mahé April 10, 2020

Hello, thank you very much !

On the current instance I'm using, both getAllUsers().size() and getAllApplicationUsers().size() return 9739.

I tried your code (returning users.size()), and it returns only 1000 results, but I must admit I'm not familiar with userSearchService.

I forgot to mention that what I'm trying to achieve, is to get a list of all users and their last login date.

Hana Kučerová
Community Champion
April 11, 2020

Hi @Quentin Mahé ,

please try to increase the maximum number of results like this:

UserSearchParams.Builder paramBuilder = UserSearchParams.builder()
.allowEmptyQuery(true)
.includeActive(true)
.includeInactive(true)
.maxResults(10000)

Unfortunately I don't have such big instance now to be able to test it myself. Thank you.

Please look at this script for inspiration how to get last login time.

Like Flora Vidal likes this
Quentin Mahé April 14, 2020

Hello @Hana Kučerová 

I just tried and it does return the same number of users than the other 2 methods, thanks ! And I already found how to get the last login date.

Now I'm trying to get all users whose name DON'T start with either 8A or 9T, is it possible to do it in a single query ? Do regexp work with UserSearchService ?

Hana Kučerová
Community Champion
April 15, 2020

Hi @Quentin Mahé ,

I don't think this is possible. You can pass search string as a second parameter of findUsers method, but not regular expression.

Maybe you can find these users using your script? It is not optimal, because you will have to go through all the users and it would be better to limit the returned set of users, but it could work.

Quentin Mahé April 16, 2020

Hello @Hana Kučerová ,

Too bad, that would have been perfect, but I still found a solution. I've tried a few things and it seems that the search string in findUsers will only give you the users that match the query (ie. it doesn't seem possible to return all users that don't start with 8A).

In the end I used findAll on the findUsers() result to apply my filter on names, and then using each I get the last login date and run another filter (ie. users that logged-in at least once during the last 3 months).

I'm not sure if it's the right way to do it (using each{} straight after findUsers, instead of saving findUsers()'s result in a variable, then running each{} on it), but it works pretty fast (~3s).

 ComponentAccessor.getComponent(UserSearchService).findUsers(jiraServiceContext, "", paramBuilder.build()).findAll{!it.getUsername().startsWith("8A") && !it.getUsername().startsWith("9T")}.each { user ->
lastLoginTimestamp = loginManager.getLoginInfo(user.getUsername()).getLastLoginTime()

if (lastLoginTimestamp != null && new Date(lastLoginTimestamp).after(seuilLastLogin)) {
Return.add(Username: user.getUsername(), LastLoginDate: new Date(lastLoginTimestamp).format("yyyy-MM-dd HH:mm:ss"))
}
}

 

Hana Kučerová
Community Champion
April 16, 2020

Hi @Quentin Mahé

great, this should work. Thanks for sharing your code.

Suggest an answer

Log in or Sign up to answer