Remove Users from a custom Jira group

Hemanshu Sood July 20, 2018

Hi,

 

I have written a script for removing 100+ users from a custom Jira group. I have tried to run this in Script console in Jira 6.4.13, but it is giving me an error:

 

Please assist

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.groupManager
def userUtil = ComponentAccessor.userUtil
def userManager = ComponentAccessor.userManager

def usersToRemove = ["exampleUser1","exampleUser2","exampleUser3","exampleUser4","exampleUser5"] //User Names
def groupToAlter = groupManager.getGroup("NameOfGroup")

usersToRemove.each{
def currentUser = userManager.getUserByName(it)
userUtil.removeUserFromGroup(groupToAlter, currentUser)
}




 

Error:

 

No signature of method: com.atlassian.jira.user.util.UserUtilImpl.removeUserFromGroup() is applicable for argument types: (com.atlassian.crowd.embedded.impl.ImmutableGroup, com.atlassian.jira.user.DelegatingApplicationUser)

 

 

It is something related to UserUtil class, but not able to figure out a solution for this

 

 

3 answers

1 accepted

1 vote
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 20, 2018

Hello,

What is your Jira version?

Hemanshu Sood July 20, 2018

HI @Alexey Matveev, it is 6.4.13 Jira server

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 20, 2018

Try like this:

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.groupManager
def userUtil = ComponentAccessor.userUtil
def userManager = ComponentAccessor.userManager

def usersToRemove = ["exampleUser1","exampleUser2","exampleUser3","exampleUser4","exampleUser5"] //User Names
def groupToAlter = groupManager.getGroup("NameOfGroup")

usersToRemove.each{
def currentUser = userManager.getUserByName(it)
userUtil.removeUserFromGroup(groupToAlter, currentUser.getDirectoryUser())
}
Hemanshu Sood July 20, 2018

Wow , this works. Thanks @Alexey Matveev

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 20, 2018

You are welcome!

0 votes
Salim Hammar June 24, 2022

Hi @Alexey Matveev 

I would like knwo if i can insert the login  in the "@ShortTextInput"

barre.png

and he display me the groups attached to this user and i can the delete this user for all groups he attached

Example 

Login : test12

 

test12 attchdes groups : group1-user ; group2-dveelopers 

Delete the users test 12  so that it us not part of the groups that are displayed

 

i have started the script but i'm blocked 

script_delete.png

 

Thanks in advance

0 votes
Deleted user June 14, 2021

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.user.ApplicationUser
final List<String> jiraList =[]

def userService = ComponentAccessor.getComponent(UserService)
def asUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def userUtil = ComponentAccessor.userUtil
def userManager = ComponentAccessor.userManager
//def userService = ComponentAccessor.getComponent(UserService)
def groupManager = ComponentAccessor.getGroupManager()


groupManager.getUsersInGroup("<group_name>").each {

jiraList.add(it.username)

}

jiraList.each {
def cur = userManager.getUserByName(it)
UserService.DeleteUserValidationResult result = userService.validateDeleteUser(asUser,cur)
if (result.isValid()){
log.warn "delete ${cur}"
userService.removeUser(asUser, result)
} else {
log.warn "Update of ${cur} failed: ${result.errorCollection.errors.entrySet().join(',')}\n"
} }

Suggest an answer

Log in or Sign up to answer