Activate users using Groovy REST ENDPOINT

Hello,

 

i am trying to activate a user using Scriptrunner API endpoint method but i am getting an error . The below code is just for the test using GET method . please help . the eerror i am getting is in the bold . I am trying this in both jira 6 and jira 7.

 

import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.User
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.util.UserUtil
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.Response
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import org.apache.log4j.Level
import org.apache.log4j.Logger

@BaseScript CustomEndpointDelegate delegate

Uactivate (httpMethod: "GET" , groups: ["jira-administrators"])
{ queryParams, body, HttpServletRequest request ->

def userName = request.getParameter("userName")
if (userName)
{
def jiraUserManager = ComponentAccessor.getUserManager()
def user = jiraUserManager.getUserByName(userName)
if(!user)
{
return Response.noContent().build()
}
ImmutableUser.newUser(user).active(false).toUser()
return Response.ok(new JsonBuilder([user: userName]).toString()).build()
}
else
return Response.noContent().build()
}

 

Abyakta

1 answer

1 accepted

0 votes
Accepted answer

Hi Abyakta,

You seem to be using a mix of the Crowd API and the JIRA API. I am not sure if this is what you want, but this seems to work for me to activate a user using a given username:

import com.atlassian.jira.bc.user.ApplicationUserBuilderImpl
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
Uactivate (httpMethod: "GET" , groups: ["jira-administrators"])
{ queryParams, body, HttpServletRequest request ->
def userName = request.getParameter("userName")
if (userName)
{
def jiraUserManager = ComponentAccessor.getUserManager()
def user = jiraUserManager.getUserByName(userName)
if(!user)
{
return Response.noContent().build()
}
jiraUserManager.updateUser(new ApplicationUserBuilderImpl(user).active(true).build())
return Response.ok(new JsonBuilder([user: userName]).toString()).build()
}
else
return Response.noContent().build()
}

 

Hi Joshua,

 

Thanks for reverting back and correcting me. Your answer works perfectly with jira ver 7 .

 

We have two JIRA one with 7.2 and another with version 6.4 .for version 6.4 below code will not work .

import com.atlassian.jira.bc.user.ApplicationUserBuilderImp

jiraUserManager.updateUser(new ApplicationUserBuilderImpl(user).active(true).build()).

 

Can you please help.

 

hi,

what maven dependency did you add to have ApplicationUserBuilderImpl available?

thx in advance.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

316 views 0 1
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you