I have lots of users that I need to deactivate. Is there an API of some kind to do this? Using the UI will take to long.
I've tried setting the Active field to 0 directly in the cwd_user table, but JIRA ignores this (though when done via the JIRA UI this column is changed to 0).
Using JIRA version is 5.2.4.1 with a local user directory
Community moderators have prevented the ability to post new answers.
If you don't want to shut down JIRA you can get the Script Runner plugin and use this script to deactivate users.
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 -> user.name == 'username'}.each { user -> 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
You have to adapt the findAll{} part to find the users you want to deactivate.
Henning
Nice. We are deactivating the users as part of an upgrade, so shutting down is not a problem. But I'll keep this in mind in the future.
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is there any way to do this by the crowd client alone? I've got a crowd client written and can search for all the required users that way. I just can't find out how to deactivate a user with the crowd.service.client.CrowdClient class. I've tried to user client.updateUser but it takes a crowd.model.user.User and not an ImmutableUser.toUser().
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Henning, This is a great script and I have used it to delete a few users and it works! Is there anyway I can import a CSV file with usernames and have the script read that file and perform this on the findAll to make the users inactive? We have a list of terminated users from HR and we can't categorize them based on Jira attributes. If you have any ideas let me know. Thanks so much! Rich
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sure, just add a list of usernames to the beginning of the script like this def usernamelist = [ 'auser', 'buser' 'cuser' ] And than change the findAll{} part of the script to .findAll{user -> usernamelist.contains(user.name)} Now test on a test system :-) Henning
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
this is not working with jira 7
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Updated for version 7:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.bc.JiraServiceContextImpl import com.atlassian.crowd.embedded.impl.ImmutableUser import com.atlassian.jira.user.DelegatingApplicationUser import com.atlassian.jira.bc.user.search.UserSearchService import com.atlassian.jira.bc.user.UserService def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() def jiraServiceContext = new JiraServiceContextImpl(currentUser) def userSearchService = ComponentAccessor.getComponent(UserSearchService.class) def userService = ComponentAccessor.getComponent(UserService.class) def appUser = userSearchService.getUserByName(jiraServiceContext, 'test') def errors = '' if (appUser != null) { ImmutableUser.Builder builder = ImmutableUser.newUser(appUser.getDirectoryUser()); builder.active(false) //builder.displayName('New display name') //builder.emailAddress('newemail@somewhere.com') def updateUser = new DelegatingApplicationUser(appUser.getId(), appUser.getKey(), builder.toUser()); def updateUserValidationResult = userService.validateUpdateUser(updateUser) if (updateUserValidationResult.isValid()) { userService.updateUser(updateUserValidationResult) } else { errors += "Update of ${appUser.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}\n" } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
we need to do any modification in the above script. to delete users
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How can I use this to get users from csv file
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Finally got the SQL approach to work. Lucas' answer triggered an idea. I had to shutdown JIRA to get it to pick up the change. It's caching user information each time a user is examined in the admin UI.
The SQL is simple:
update cwd_user set active = 0 where <your filter here>;
Thanks for both answers
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
jira --action updateUser --userId xxxx --deactivate
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This doesn't seem to work in our cloud instance {code} [root@uk-ctoolbox-1 ~]# /usr/bin/java -jar /usr/local/jira_cli/lib/jira-cli-4.5.0-SNAPSHOT.jar --server https://mimecast.jira.com --user tsweeney --password 'XXXXXXXX' --project CoreGrid -a updateUser --userId aali --deactivate Client error: Activate and deactivate cannot be set for Cloud. [root@uk-ctoolbox-1 ~]# {code}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi All,
Please note that direct database modification is not supported by Atlassian per our Support Offerings.
The ability to bulk deactivate users has been requested as a new feature, see JRA-30708
Cheers!
Tim | Atlassian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry Tim....customers don't have time to wait for 5 years for such small changes. JRA-30708 was opened in Nov 2012.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Tim's response seems like a boilerplate one that Jira product managers are required to post whenever the AI detects the the phrase 'UPDATE/INSERT/DELETE <whatever>' in a forum posting :-(
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Chiz,
Many user modifications via database doesn't work on the fly. You can disable them via database as well but I sugest you to shutdown JIRA (and perform a database backup) before modifying the data under cwd_user table.
Best regards,
Lucas Timm
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Alvin,
create a list of usernames from the csv file and paste the list into this script.
import com.atlassian.jira.bc.user.ApplicationUserBuilderImpl
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
def usernames = [
'username1',
'username2',
'username3'
]
def test = true
def userManager = ComponentAccessor.userManager
def userService = ComponentAccessor.getComponent(UserService.class)
def result = ""
usernames.each{username ->
def appUser = userManager.getUserByName(username)
def updateUser = (new ApplicationUserBuilderImpl(appUser)).active(false).build()
def updateUserValidationResult = userService.validateUpdateUser(updateUser)
if (updateUserValidationResult.isValid()) {
if (!test) {
userService.updateUser(updateUserValidationResult)
}
result += "Deactivated ${updateUser.name}<br>"
} else {
result += "<b>Update of ${updateUser.displayName} (${updateUser.name}) failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}</b><br>"
}
}
result
Check I a first run if everything would run fine and than change test = true to test = false.
Henning
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Henning Tietgens ,
Thank you for your very quick response, I have achieved this using iteration and placing the whole process inside while loop.
I will give your suggestion a try. Thank you so much. If you can suggest on how can I do this without pasting the whole usernames inside the script , that would be great!
Best Regards,
Alvin
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try this for the first part. Adapt filename, delimiter and column (starting at 0) according to your csv file.
def test = true
def csvFilename = '/var/opt/jira/data/users.csv'
def delimiter = ','
def username_column = 2
def result = ""
def usernames = []
try {
def f = new File(csvFilename)
def lines = f.readLines()
lines.each { line ->
def parts = line.split(delimiter)
if (parts.size() > username_column) {
usernames.add(parts[username_column])
} else {
result += "Line '$line' of file $csvFilename could not be interpreted (size ${parts.size()}).<br>"
}
}
}
catch (e) {
result += "File error. $e<br>"
return
}
Henning
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Henning Tietgens ,
Thank you so much for keeping an extra eye and effort regarding with my questions, will definitely give this a try. Thank you so much!
I will let you know with the results.
Best Regards,
Alvin
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Henning Tietgens ,
For the first solution you've given, test = false made the users to inactive. Thank you!
Best Regards,
Alvin
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Could you help me?
I want to pass the enumerate using query parameter.
def usernames = [
'username1',
'username2',
'username3'
]
how can I get the enumerate via groovy script?
Thank you in advance!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What did you mean with "query parameter"? For which query?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For example I want to send next request
http://jiratest.ex.ru/rest/my-groovy/latest/custom/MyScript?username=testUser
"username=testUser" is the query parameter the request
And then I want to get "testUser" in my script
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In this case you have to define a REST endpoint, see https://scriptrunner.adaptavist.com/latest/jira/rest-endpoints.html
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I use this plugin
https://marketplace.atlassian.com/apps/1218755/mygroovy?hosting=server&tab=overview
and I don't realize how to define a REST endpoint
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I don't know how to do this with mygroovy plugin, too. Sorry.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Good for Jira...
Any similar suggestions for Confluence? 6.5.4
Have to free up about 400 inactive user licenses...
Appreciate any suggestions.
thx,
V
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
With ScriptRunner for Confluence this is similar possible.
Henning
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.