Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,297,670
Community Members
 
Community Events
165
Community Groups

How can we activate and deactivate a user in JIRA using groovy

How can we activate and deactivate a user in JIRA using groovy script that can be run from script console. I already have the user details which I want to activate first and then make them inactive again.

2 answers

import com.atlassian.jira.bc.user.UserService

import com.atlassian.jira.component.ComponentAccessor




def userManager = ComponentAccessor.userManager

def userService = ComponentAccessor.getComponent(UserService)
def active =true



[

    'anuser',

    'otheruser',

    // add more as required

].each { username ->

    def user = userManager.getUserByName(username)

    if (!user) {

        log.warn "Failed to find user with name ${username}"

        return

    }




    def updatedUser = userService.newUserBuilder(user).active(active).build()




    def updateUserValidationResult = userService.validateUpdateUser(updatedUser)




    if (updateUserValidationResult.valid) {

        log.warn "Deactivating user ${username}"

        userService.updateUser(updateUserValidationResult)

    } else {

        log.warn "Update of ${user.name} failed: ${updateUserValidationResult.errorCollection.errors.entrySet().join(',')}\n"

    }

}


 

Set the active variable to true or false for activating and deactivating respectively.

 

Regards,

Anzar

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you