Is there a way to bulk edit users in JIRA?

I am migrating from Bugzilla to JIRA and while the Bugzilla users come into the system under a group called "bugzilla-import-disabled-users" or "bugzilla-import-unused-users", they are coming in as "active". This is triggering an error for licensing, and with > 2000 disabled/unused users, I don't want to have to edit these all manually. Can anyone point me to how I can do this in bulk?

Thanks!

2 answers

1 vote
Henning Tietgens Community Champion Aug 15, 2013

You can use the Script Runner plugin and the following script:

import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.User
import com.atlassian.crowd.embedded.api.UserWithAttributes
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil

String result = ''

UserUtil userUtil = ComponentAccessor.userUtil
CrowdService crowdService = ComponentAccessor.crowdService
UserService userService = ComponentAccessor.getComponent(UserService.class)
User updateUser
UserWithAttributes user
UserService.UpdateUserValidationResult updateUserValidationResult

def groupNames = [
        'bugzilla-import-disabled-users',
        'bugzilla-import-unused-users'
]

userUtil.getAllUsersInGroupNamesUnsorted(groupNames).findAll{it.isActive()}.each { 
    user = crowdService.getUserWithAttributes(it.getName())
    updateUser = ImmutableUser.newUser(user).active(false).toUser()
    updateUserValidationResult = userService.validateUpdateUser(updateUser)
    if (updateUserValidationResult.isValid()) {
        userService.updateUser(updateUserValidationResult)
        result += "Deactivated ${updateUser.name}\r\n"
    } else {
        result += "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}\r\n"
    }
}
return result

Please test in testsystem first!

Henning

Thanks, Henning. I ended up filing a support request with Atlassian, and it seems that this is not intended behavior of the importer. The script looks like it might be handy if there's not another solution. Thanks for posting it. If i need to, I'll test this out in my staging environment.

For example,

This seems to work in script runner, but it doesn't store change.

import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.User
import com.atlassian.crowd.embedded.api.UserWithAttributes
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserUtil

String result = ''

UserUtil userUtil = ComponentAccessor.userUtil
CrowdService crowdService = ComponentAccessor.crowdService
UserService userService = ComponentAccessor.getComponent(UserService.class)
User updateUser
UserWithAttributes user
UserService.UpdateUserValidationResult updateUserValidationResult

def groupNames = [
        'deactivated-users',
]

userUtil.getAllUsersInGroupNamesUnsorted(groupNames).findAll{it.isActive()}.each {
    user = crowdService.getUserWithAttributes(it.getName())

    ApplicationUser appUser= userUtil.getUserByName(it.getName())

    updateUser = ImmutableUser.newUser(user).active(false).toUser()
    updateUserValidationResult = userService.validateUpdateUser(appUser)
    if (updateUserValidationResult.isValid()) {
        userService.updateUser(updateUserValidationResult)
        result += "Deactivated ${updateUser.name}\r\n"
    } else {
        result += "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}\r\n"
    }
}
return result

Henning Tietgens Community Champion Jun 22, 2014

Maybe a cache issue? Did you tried to refresh the JIRA cache (using the built-in script)?

Hi Henning,

Still running into problems.

When I run the script, it says the users are deactivated. But even when I refresh the cache, and run it, same problem.

I ran the script and dumped out contents of updateUserValidationResult:

<com.atlassian.crowd.model.user.DelegatingUserWithAttributes@2f56ea4b user=testing-123:10400 user=testing-123:10400 attributes=com.atlassian.crowd.embedded.impl.ImmutableAttributes@3fb46d1f>

<com.atlassian.jira.bc.user.UserService$UpdateUserValidationResult@26537e15 user=testing-123(testing-123) errorCollection=Errors: {}

Error Messages: []>

<com.atlassian.crowd.model.user.DelegatingUserWithAttributes@82a778f5 user=jiratestuser:10400 user=jiratestuser:10400 attributes=com.atlassian.crowd.embedded.impl.ImmutableAttributes@7567e92b>

<com.atlassian.jira.bc.user.UserService$UpdateUserValidationResult@1c2c14f2 user=jiratestuser(jiratestuser) errorCollection=Errors: {}

Error Messages: []>

<com.atlassian.crowd.model.user.DelegatingUserWithAttributes@4bdf84b9 user=jira-test-user:10400 user=jira-test-user:10400 attributes=com.atlassian.crowd.embedded.impl.ImmutableAttributes@22dfddd4>

<com.atlassian.jira.bc.user.UserService$UpdateUserValidationResult@68e775cc user=jira-test-user(jira-test-user) errorCollection=Errors: {}

Error Messages: []>

<com.atlassian.crowd.model.user.DelegatingUserWithAttributes@473545d7 user=bryan-test:10400 user=bryan-test:10400 attributes=com.atlassian.crowd.embedded.impl.ImmutableAttributes@40111438>

<com.atlassian.jira.bc.user.UserService$UpdateUserValidationResult@3f24b87c user=bryan-test(bryan-test) errorCollection=Errors: {}

Error Messages: []>

<com.atlassian.crowd.model.user.DelegatingUserWithAttributes@b13858ff user=tempfix:10400 user=tempfix:10400 attributes=com.atlassian.crowd.embedded.impl.ImmutableAttributes@3f245740>

<com.atlassian.jira.bc.user.UserService$UpdateUserValidationResult@2e91ee6b user=tempfix(tempfix) errorCollection=Errors: {}

Error Messages: []>

Hi Henning -- the link below has a version of your script that works in Jira 6.2.5.

Since you are the author, thanks! I appreciate all your examples in the forum.

import com.atlassian.crowd.embedded.api.User
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
 
UserManager userManager = ComponentAccessor.getUserManager()
UserService userService = ComponentAccessor.getComponent(UserService.class)
User updateUser
UserService.UpdateUserValidationResult updateUserValidationResult
 
errors = ''
userManager.getUsers().findAll{user -&gt; user.name == 'test-user'}.each { user -&gt;
    updateUser = ImmutableUser.newUser(user).active(false).toUser()
    updateUserValidationResult = userService.validateUpdateUser(updateUser)
    if (updateUserValidationResult.isValid()) {
        userService.updateUser(updateUserValidationResult)
    } else {
        errors += "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}\n"
    }
}
return errors

Henning Tietgens Community Champion Jun 23, 2014

Thanks for the kind words and a working version of the script. It's weird, the deprecated version of validateUpdateUser() using com.atlassian.crowd.embedded.api.User is working while the one using the ApplicationUser is not... Shortly we will switch to 6.2, maybe this is going to be fun... :-)

Hi -- maybe I was having a cache issue originally -- because your script from Aug 13 works for me too without modification.

In any event, thanks again. :)

0 vote

I believe the best way to disable users would be to simply set their 'active' value to 0 in the database.

Just be sure to have a database backup before doing the changes.

You may find the query to perform this change on the related post below:

https://answers.atlassian.com/questions/127643/how-to-deactivate-a-user-programmatically-java-rest-sql

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,305 views 14 20
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