Script for remove 100+ users from group - how to?

Hi All,

is there a script I could remove plenty of users from a group? To add multiple users to group is possible I know but how about removing?

Thanks in advance,

Rumi

2 answers

2 votes
Henning Tietgens Community Champion May 25, 2014

Use userUtil.removeUserFromGroup() to remove a user from a group.

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.bc.projectroles.ProjectRoleService

import com.atlassian.jira.security.GlobalPermissionManager

import com.atlassian.jira.security.Permissions

import com.atlassian.jira.security.roles.actor.UserRoleActorFactory

import com.atlassian.jira.user.util.UserUtil

import com.atlassian.jira.util.SimpleErrorCollection

import com.opensymphony.user.Group

import com.opensymphony.user.User

import com.opensymphony.user.UserManager

import org.apache.log4j.Category

import com.atlassian.jira.project.Project;

// Configurable section

isPreview = false

// End

log = Category.getInstance("com.onresolve.jira.groovy.DeactivateOldUsers")

log.debug ("DeactivateOldUsers function running")

userManager = (UserManager) UserManager.getInstance()

GlobalPermissionManager globalPermissionManager = (GlobalPermissionManager) ComponentManager.getInstance().getComponentInstanceOfType(GlobalPermissionManager.class)

// get the first user of the first group which has the ADMIN privilege...

// cannot use current user, not really sure who that is when run as a service

adminGroup = globalPermissionManager.getGroupNames(Permissions.ADMINISTER)[0]

adminUser = userManager.getGroup(adminGroup).getUsers().get(0)

def deactivateUser (User user) {

log.debug ("deactivateUser ${user.getName()}")

// Remove user from all groups...

UserUtil userUtil = ComponentManager.getInstance().getUserUtil()

user.getGroups().each {

Group group = userManager.getGroup(it)

log.debug ("Remove $user from group: \"$group\"")

if (! isPreview) {

userUtil.removeUserFromGroup (group, user)

}

}

// Remove user from all roles...

ProjectRoleService projectRoleService = (ProjectRoleService) ComponentManager.getComponentInstanceOfType(ProjectRoleService.class);

SimpleErrorCollection errorCollection = new SimpleErrorCollection();

log.debug ("Removing all roles references for ${user.getName()}")

projectRoleService.getProjectsContainingRoleActorByNameAndType(userManager.getUser((String) adminUser), user.getName(), UserRoleActorFactory.TYPE, errorCollection).each {Project project ->

log.debug ("Remove user ${user.getName()} from role: ${project.getName()}")

}

if (! isPreview) {

projectRoleService.removeAllRoleActorsByNameAndType (userManager.getUser((String) adminUser), user.getName(), UserRoleActorFactory.TYPE, errorCollection)

}

println errorCollection.dump()

}

def boolean userStillValid (String userName) {

// You need to implement this according to what the criteria are for valid users, eg do an LDAP search,

// or process a list of leaver names as provided by your HR dept for instance

return true

}

Integer beforeUsers = userManager.getGroup("jira-users").getUsers().size()

Integer i = 0

userManager.getGroup("jira-users").getUsers().each {

User user = UserManager.getInstance().getUser(it)

userName = user.getName()

if (!userStillValid (userName)) {

log.warn ("User: $userName should be retired")

deactivateUser (user)

}

i++

}

Integer afterUsers = userManager.getGroup("jira-users").getUsers().size()

log.info ("Was: $beforeUsers, now: $afterUsers, deleted: " + (beforeUsers - afterUsers))

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,207 views 13 19
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot