Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,456,245
Community Members
 
Community Events
176
Community Groups

How to get users from a list in Scriptrunner

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 Leader Dec 02, 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