Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

marking bulk users as Active/Inactive using groovy , jelly or any other script ?

Sumit Kumar
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.
November 14, 2013

Is there any method available for marking Users as inactive using Jelly , Groovy or any other script ?

Thanks in advance

Regards,

Sumit

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

6 votes
Answer accepted
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.
November 14, 2013

Using Script Runner you can use this script to deactivate a user:

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

userManager.getUsers().findAll{user -> user.displayName == 'Test'}.each { user ->
    updateUser = ImmutableUser.newUser(user).active(false).toUser()
    updateUserValidationResult = userService.validateUpdateUser(updateUser)
    if (updateUserValidationResult.isValid()) {
        userService.updateUser(updateUserValidationResult)
    } else {
        // throw error
    }
}

In this case the script searches for a user with display name "Test" and deactivates this user. You can adapt this to your needs.

 

Sumit Kumar
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.
November 14, 2013

Thanks a lot ... you saved my lot of time..!!

You are the BOSS ..!!

Regards,

Sumit

srinivasp
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.
July 20, 2015

Henning, We are using JIRA 6.2, since validateUpdateUser(User) is deprecated in the latest version, I have modified the JAVA code as below but it is not working. Strangely, the same code is working on groovy console. Am I doing something wrong here? Please suggest. UserService userService = ComponentAccessor.getComponent(UserService.class); UserService.UpdateUserValidationResult updateUserValidationResult; ImmutableUser.Builder builder = ImmutableUser.newUser(jiraUser); builder.active(false); ApplicationUser updatedUser = new DelegatingApplicationUser( jiraUser.getName(), builder.toUser()); updateUserValidationResult = userService.validateUpdateUser(updatedUser); if (updateUserValidationResult.isValid()) { userService.updateUser(updateUserValidationResult); }

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.
July 21, 2015

Did you get any errors?

srinivasp
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.
July 21, 2015

There are no errors. I found updateUserValidationResult.isValid() is returning false

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.
July 21, 2015

Then take a look into the errorcollection of the validation result. In Groovy this could be done like this updateUserValidationResult.errorCollection.errorMessages.each { log?.error "Error Message: $it" } updateUserValidationResult.errorCollection.errors.each { log?.error "Error: $it" } updateUserValidationResult.errorCollection.reasons.each { log?.error "Reason: $it" }

srinivasp
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.
July 22, 2015

I found there are no error messagas when I execute from groovy console but from JAVA i am seeing this message "You do not have the permission to update users."

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.
July 22, 2015

If your Java code is running as a service there is probably no user within the current jira authentication context (ComponentAccessor.getJiraAuthenticationContext().getUser()). Try to run as a specific user by setting it to a user who has the needed rights (in this case 'admin', replace with appropriate username)

def user = userManager.getUserByName('admin')
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(user)

 

srinivasp
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.
July 23, 2015

Yes, My Java code is running as JIRA service and there is no user with the current JIRA authentication context. With your suggestion, I had added the statement to run as specific user with admin rights and now it started working. Thank you very much for your help, appreciate that.

Thibauld Leprince January 11, 2017

Hi Henning,

can you help me to make you script work with a condition on a group ?

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.
January 11, 2017

Sure. Basically you could add your condition to the 

.findAll{user -> user.displayName == 'Test'}

part of the script. But more likely you could only retrieve users that are relevant for the groups you're looking for. For this you could use 

ComponentAccessor.getUserUtil().getAllUsersInGroupNamesUnsorted(['groupname1', 'groupname2'])

instead of 

userManager.getUsers().findAll{user -> user.displayName == 'Test'}

If you need more help, you need to be more specific about your goal.

Have fun,
Henning

Thibauld Leprince January 12, 2017

Hi Henning,

it worked perfectly, all my users in "jira-inactive-users" are now disabled. Its great.

Thank you

Vaibhav Kamble December 5, 2017

I am faceing "Error Messages: [You do not have the permission to update users.]" this issue in service context.

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.
December 5, 2017
Vaibhav Kamble December 5, 2017

Thank you now, it's working

Priya Thakur June 3, 2019

Can I get help on this code for Jira 8. Its not working.

TAGS
AUG Leaders

Atlassian Community Events