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

Activate or Deactivate a User programmatically

Ariel Rosa September 19, 2014

Hello guys,

Is there a method to activate/deactivate the user thought java code after version 6.0?
 

The same funcionality as clicking in the checkbox for that in the JIRA admin:
lance.jpg 

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
JamieA
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.
September 19, 2014

Here's some sample code... in groovy but you can easily write as java 

For JIRA 7:

def userManager = ComponentAccessor.getUserManager()
def userService = ComponentAccessor.getComponent(UserService.class)
def user = userManager.getUserByName(username)
def updateUser = userService.newUserBuilder(user).active(false).build()

def updateUserValidationResult = userService.validateUpdateUser(updateUser)

if (updateUserValidationResult.isValid()) {
    userService.updateUser(updateUserValidationResult)
} else {
    log.warn "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}\n"
}

in JIRA 6:

def userService = ComponentAccessor.getComponent(UserService.class)
def user = userUtil.getUserByName(username).getDirectoryUser()
def updateUser = ImmutableUser.newUser(user).active(false).toUser()
def updateUserValidationResult = userService.validateUpdateUser(updateUser)

if (updateUserValidationResult.isValid()) {
    userService.updateUser(updateUserValidationResult)
} else {
    log.warn "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}\n"
}
Ariel Rosa October 5, 2014

Thanks Jamie, it worked relly well! :)

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 16, 2015

Hi Jamie, ImmutableUser is not found in com.atlassian.crowd.embedded.impl package. I am using amps version 5.0.13. Could you please suggest?

JamieA
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 16, 2015

what version of jira?

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 16, 2015

Hi Jamie, Actually I need to implement in JAVA for JIRA version 6.2 but for testing the code I'm using groovy console where it was not working. validateUpdateUser method is deprecated in JAVA so I'm trying for different way to deactivate users. Could you please suggest? userService.validateUpdateUser(updateUser) Thanks, Srinivas

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

My Java code is running as JIRA service and there is no user with the current JIRA authentication context. I had added the statement to run as specific user with admin rights and now it started working.

Abyakta Lenka February 21, 2017

@Jamie Echlin [Adaptavist] can you please help with above code you wrote for JIRA version 6.4.14 and version 7 .

i want to activate users using groovy code but getting error .

how i am trying is pull userid from the custom field and process it in the post function to mark it as active .

 

Abyakta

 

JamieA
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.
February 21, 2017

I've updated my answer

Abyakta Lenka June 15, 2017

Hi Jamie Echlin [Adaptavist]

 

I tried the code for jira 6 but faced below error .please help.

groovy.lang.MissingPropertyException: No such property: userUtil for class: Script14
 at Script14.run(Script14.groovy:12)

 

TAGS
AUG Leaders

Atlassian Community Events