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.
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.ComponentAccessorimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.jira.user.ApplicationUsersimport com.atlassian.jira.bc.user.search.UserSearchServiceimport com.atlassian.jira.security.groups.GroupManagerimport com.atlassian.jira.user.util.UserUtilimport org.apache.log4j.Loggerimport org.apache.log4j.Leveldef 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.userUtildef 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?
def userids = ["user1","user2"]userids.each { userid ->ApplicationUser user = ComponentAccessor.getUserManager().getUserByKey(userid.toString())}
Use the above instead of the emailid lines above
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)
}
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.bc.user.search.UserSearchServiceimport com.atlassian.jira.bc.user.search.UserSearchParamsimport com.atlassian.jira.bc.user.UserServiceimport com.atlassian.jira.user.ApplicationUserfinal List<String> jiraList =[]
def userService = ComponentAccessor.getComponent(UserService)def asUser = ComponentAccessor.jiraAuthenticationContext.loggedInUserdef userUtil = ComponentAccessor.userUtildef 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"} }
Hi @Fazila Ashraf
I would like knwo if i can insert the login in the "@ShortTextInput"
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
Thanks in advance
It looks like you're new here. Sign in or register to get started.