Forums

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

How to get users from a list in Scriptrunner

Jeremy Jedlicka
Contributor
December 1, 2022

Hello,

I probably have a very simple problem but I can't seem to solve it.  I've been given a pretty long list of current users that no longer need access to Jira and I need to deactivate them.  This is a one off problem, so I don't really need a repeatable solution.

What I'm trying to do is to just run a script that will deactivate all the users in the list by their Jira ID.  I have the code written to successfully do this for just one user and which should iterate through a list, but I can't get a predefined list to work as the input.

Here's my current code:

import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.UserWithAttributes
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.bc.user.UserService
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.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.user.util.UserUtil

UserUtil userUtil = ComponentAccessor.userUtil
CrowdService crowdService = ComponentAccessor.crowdService
UserService userService = ComponentAccessor.getComponent(UserService)
UserSearchService userSearchService = ComponentAccessor.getComponent(UserSearchService.class);
UserSearchParams userSearchParams = new UserSearchParams(false,true,false);
List<ApplicationUser> userList = userSearchService.findUsers("a451131", userSearchParams);
ApplicationUser updateUser
UserService.UpdateUserValidationResult updateUserValidationResult

GroupManager groupManager = ComponentAccessor.getGroupManager();

userList.findAll{it.isActive()}.each {
    if(groupManager.isUserInGroup(it.getName(),"jira-administrators"))
        return;
    UserWithAttributes user = crowdService.getUserWithAttributes(it.getName())
            updateUser = ApplicationUsers.from(ImmutableUser.newUser(user).active(false).toUser())
            updateUserValidationResult = userService.validateUpdateUser(updateUser)
            if (updateUserValidationResult.isValid()) {
                userService.updateUser(updateUserValidationResult)
                log.info "Deactivated ${updateUser.name}"
            } else {
                log.error "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}"
            }
}

 

I obviously need to do something with the line: 

List<ApplicationUser> userList = userSearchService.findUsers("a451131", userSearchParams);

But I don't know what to replace this with to use a defined list.

This defined list will be something like:

def DeactivateUsers = [
'user1','user2','user3'
]

 

 

1 answer

0 votes
Tom Lister
Community Champion
December 2, 2022

Hi @Jeremy Jedlicka 

I have some user manipulation scripts in my GitHub e.g.

https://github.com/listertuk/groovy/blob/2ded1edab2bdf4c266ee2fb4720a827ddf4effd8/server-dc/Scriptrunner/Admin/RemoveUnusedInternalUsers.groovy

https://github.com/listertuk/groovy/blob/2ded1edab2bdf4c266ee2fb4720a827ddf4effd8/server-dc/Scriptrunner/Admin/RemoveInternalJiraSoftwareUsers.groovy

If you have a fixed list of usernames, you can still loop through them with DeactivatedUsers.foreach() . You will need to get hold of the UserManager and use one of the getUser methods e.g. getUserByName to get the ApplicationUser object

also check out 

def userUtil = ComponentAccessor.getUserUtil()
For some useful helper methods. Some my usages may be deprecated by now.

Suggest an answer

Log in or Sign up to answer