A simple example code: function CaptchaReset(username)

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

import com.atlassian.jira.component.ComponentAccessor
import groovy.sql.Sql
import org.ofbiz.core.entity.ConnectionFactory
import org.ofbiz.core.entity.DelegatorInterface

import java.sql.Connection
def delegator = (DelegatorInterface) ComponentAccessor.getComponent(DelegatorInterface)
String helperName = delegator.getGroupHelperName("default")

@BaseScript CustomEndpointDelegate delegate

CaptchaReset(httpMethod: "GET", groups: ["jira-users"]) { MultivaluedMap queryParams->
    def username = queryParams.getFirst("username") as String
    if ( username == null ) {  return Response.serverError().entity([error:"NO_USERNAME"]).build();}
    
    def sqlStmt = "update cwd_user_attributes set attribute_value = '0' where user_id = (select max(id) from cwd_user where user_name = '$username') and attribute_name = 'login.totalFailedCount';"
    def sqlStmt2 = "update cwd_user_attributes set attribute_value = '0' where user_id = (select max(id) from cwd_user where user_name = '$username') and attribute_name = 'login.currentFailedCount';"
    
	Connection conn = ConnectionFactory.getConnection(helperName)
	Sql sql = new Sql(conn)
	try {
    	StringBuffer sb = new StringBuffer()
    	sql.execute(sqlStmt) 
        sql.execute(sqlStmt2)
	}
	finally {
    	sql.close()
	}
    return Response.ok(new JsonBuilder([result: "OK"]).toString()).build();
}

Example query: /rest/scriptrunner/latest/custom/CaptchaReset?username=mraddon

Example result:

{"result":"OK"}

By MrAddon

Posted by:.

One thought on “REST Endpoint to RESET CAPTCHA of users in Jira Server using Scriptrunner

  1. IMPORTANT NOTE: If we have more than one Directory, we need to change the SQL updates to only apply to the enabled one!
    Example: (Note the DIRECTORY_ID = ‘10800’ in the Subquery)

     def sqlStmt = "update cwd_user_attributes set attribute_value = '0' where user_id = (select max(id) from cwd_user where user_name = '$username' and DIRECTORY_ID = '10800') and attribute_name = 'login.totalFailedCount';"
        def sqlStmt2 = "update cwd_user_attributes set attribute_value = '0' where user_id = (select max(id) from cwd_user where user_name = '$username' and DIRECTORY_ID = '10800') and attribute_name = 'login.currentFailedCount';"
    

    More info: https://developer.atlassian.com/server/jira/platform/database-user-and-group-tables/

    Like

Leave a Reply to . Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s