Is there a way to bulk edit users in JIRA?

Susan Hazlett August 15, 2013

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!

3 answers

1 vote
Henning Tietgens
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 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

Susan Hazlett August 16, 2013

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.

Bryan Karsh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 22, 2014

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 22, 2014

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

Bryan Karsh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 23, 2014

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: []>

Bryan Karsh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 23, 2014

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 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... :-)

Bryan Karsh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 23, 2014

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 votes
Mentorship4U July 11, 2020

I wrote a blog post on how to bulk edit Jira user e-mail accounts.
It's based on ScriptRunner and Groovy.

Cheers!

0 votes
Marcus Silveira
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 23, 2014

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.

Danilo Conrad
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 23, 2014

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 Sign up to answer