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

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How deactivate user (as rest parameter) via scriptrunner

Hello,

I would like to know if there is a way to deactivate a user using scriptrunner and pass username via the rest endpoint ?

Something like:  jiraBaseURL/rest/scriptrunner/latest/custom/deactivateUser/?userName=USERNAME

I saw this documentation: https://www.adaptavist.com/doco/display/SFJ/Automatically+deactivate+inactive+JIRA+users but it's for deactivating users that are inactive. Plus this code is not custom endpoint.

I'm a beginner on this area. I only have a skeleton code:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

deactivateUser(httpMethod: "GET", groups: ["jira-administrators"]) { queryParams, body, HttpServletRequest request ->
def userName = request.getParameter("userName")
if (userName) {

// Deactivate userName
// Test if success
// return Response.ok("SUCCESS").build()
// Test if failed (example: if user is project lead)
// return Response.ok("FAILED").build()
}
else
return Response.noContent().build()
}

Can someone help me to build that code ? 

Thank you very much.

 

1 answer

You're nearly there. The code you linked to shows pretty well how to deactivate a user, I think where you're struggling is how to get a user from query parameters. Something like this should do:

import com.atlassian.crowd.embedded.api.CrowdService
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.ApplicationUsers
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

deactivateUser(httpMethod: "GET", groups: ["jira-administrators"]) { MultivaluedMap queryParams, body, HttpServletRequest request ->
def userKey = queryParams.getFirst("userKey") as String
UserService userService = ComponentAccessor.getComponent(UserService)
def crowdService = ComponentAccessor.getComponent(CrowdService)
UserWithAttributes user = crowdService.getUserWithAttributes(userKey)
def updateUser = ApplicationUsers.from(ImmutableUser.newUser(user).active(false).toUser())
def validationResult = userService.validateUpdateUser(updateUser)
if (validationResult.isValid()) {
userService.updateUser(validationResult)
return Response.ok(new JsonBuilder("Deactivated $userKey").toString()).build()
} else {
log.error "Failed to update user with key $userKey"
log.error validationResult.errorCollection
return Response.status(Response.Status.BAD_REQUEST).build()
}
}

Notably, that will fail for users who are project leads, but it should log the error so that you can discern the source of the problem.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence Cloud

Share your Confluence Cloud experience for a chance to win $500!

Take our 5-minute survey to win a $500 Visa gift card! Are you currently using Confluence Cloud? We want to hear from you! Fill out this quick survey about your Confluence Cloud experience so we ...

238 views 5 13
Read article

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