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

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published yesterday in Marketplace Apps

The 7 hacks of highly successful automation

...there's anything I've learnt from working, it's that people are lazy! No offense to anyone reading this, but it's true and we can all admit it. The easier you make something for someone, the more...

165 views 0 13
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