You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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'
]
I have some user manipulation scripts in my GitHub e.g.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.