How to remove the users from the Jira-user group

Peddinti Hemanth Satyakumar April 25, 2019

Hi Team,

     I'm trying to remove the inactive users from the jira-user group. Could anyone help me how to implement it using Groovy script?

Regards,

Hemanth.

3 answers

0 votes
Salim Hammar June 24, 2022

Hi @Fazila Ashraf 

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 {    //group name here to delete users from that group

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"
} }

0 votes
Fazila Ashraf
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 25, 2019

Hi @Peddinti Hemanth Satyakumar 

 

I run the db command to get the list of inactive users and then with the users list i remove them in bulk from the group.

 

Here is my code to do this using  scriptrunner script console

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.util.UserUtil
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("Remove users from jira-users")
log.setLevel(Level.DEBUG)
log.debug ("Remove users from group - start")

def userManager = ComponentAccessor.getUserManager()
def groupManager = ComponentAccessor.getGroupManager()

def userUtil = ComponentAccessor.userUtil

def group = groupManager.getGroup("jira-users")

def emailIds = ["user1@email.com","user2@email.com"]

emailIds.each { emailid ->
def userSearchService = ComponentAccessor.getComponent(UserSearchService)

def user = userSearchService.findUsersByEmail(emailid)

log.debug "Email id " + emailid +": User "+ user[0]

userUtil.removeUserFromGroup(group,user[0]);

}

log.debug ("Remove users from group - End")

 

Peddinti Hemanth Satyakumar April 25, 2019

Hi Fazila,

     In this script, you are using the email Id's for removing, Is it possible to use the username for removing?

Regards,

Hemanth.

Fazila Ashraf
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 25, 2019
def userids = ["user1","user2"]

userids.each { userid ->

ApplicationUser user = ComponentAccessor.getUserManager().getUserByKey(userid.toString())

}

 

Use the above instead of the emailid lines above

Peddinti Hemanth Satyakumar April 25, 2019

Hi Fazila,

     Thanks for your reply, I tried as you suggested but I'm facing below exception. Could you please suggest on this.

{

java.lang.IllegalArgumentException: User must not be null if trying to add or delete them from a group. at com.atlassian.jira.user.util.UserUtilImpl.validateParameters(UserUtilImpl.java:654) at com.atlassian.jira.user.util.UserUtilImpl.removeUserFromGroup(UserUtilImpl.java:377) at com.atlassian.jira.user.util.UserUtil$removeUserFromGroup.call(Unknown Source) at Script282$_run_closure3.doCall(Script282.groovy:37) at Script282.run(Script282.groovy:34)

}

 

Regards,

Hemanth.

Peddinti Hemanth Satyakumar April 25, 2019

I'm getting null in this line

ApplicationUser user = ComponentAccessor.getUserManager().getUserByKey(userid.toString())

Could you please suggest where I'm going wrong.

 

Regards,

Hemanth. 

Fazila Ashraf
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 25, 2019

I have seen this thing for my users list also.. Something with the db data having null references.

Add this additional block to ignore the null users and process only to not null entries

if (user != null){
log.debug ": User "+ user


userUtil.removeUserFromGroup(group,user[0]);

}
Peddinti Hemanth Satyakumar April 25, 2019

Hi Ashraf,

     Still, I'm facing the same. I'm getting the null in the user variable. Below is the script I'm trying, Please check If any modifications needed.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.bc.group.GroupService
import com.atlassian.jira.bc.JiraServiceContext
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.group.GroupService
import com.atlassian.jira.bc.group.GroupRemoveChildMapper

def jiraList=[]

UserUtil userUtil = ComponentAccessor.userUtil
def groupManager = ComponentAccessor.groupManager
def userManager = ComponentAccessor.getUserManager()

groupManager.getUsersInGroup("jira-tester").findAll{!it.isActive()}.each {

jiraList.add("\'"+it.getName()+"\'")

}

def group = groupManager.getGroup("jira-tester")

jiraList.each {
ApplicationUser user = ComponentAccessor.getUserManager().getUserByKey(it.toString())
log.warn(user)
if (user != null){
log.debug ": User "+ user

userUtil.removeUserFromGroup(group,user);

}
}

return jiraList.size()

 

And user[0] also gives an exception in the script as shown in the screenshot below.

Screenshot_20190425_185644.png

kdickason October 26, 2021

@Peddinti Hemanth Satyakumar were you ever able to figure this out?  I'm facing the same issue.  Could you send me the complete script if you have it working? Thank you for any help!

Suggest an answer

Log in or Sign up to answer