Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to remove the users from the Jira-user group

Edited

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.

2 answers

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

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

 

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 Apr 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

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.

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 Apr 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]);

}

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

@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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you