how to get current user id in groovy or how to pass run parameter value to groovy?

SKR January 19, 2014

Hi,

how to get current user id in groovy or how to pass run parameter value to groovy?

please find below my example logic and I want it something similar to below:

In the below example I would like to get the current user id through groovy plugin, then based on that, will execute the SQL query to get department of the logged in person, later check for the department using IF condition and value is true then display the links accordingly.

import com.atlassian.renderer.v2.RenderMode

def renderMode = RenderMode.suppress(RenderMode.F_FIRST_PARA)

def userSql = "select dept from users table where user ='\$current_user_id'"

def userMacro = "{sql-query:datasource=testDS|table=false} ${userSql} {sql-query}"

def userdept = subRenderer.render(userMacro, context, renderMode)

if(userdept == 'sales'){

def dept="<a href="gotosales">Sales</a>"

}else if(userdept == 'sales'){

def dept="<a href="gotomanager">manager</a>"

}else{

## do nothing

}

out.println(dept)

Here I'm attaching the screen shot for better understanding, please find that and let me know how to get it fixed.

Out put:

Thanks alot.

4 answers

0 votes
SKR January 26, 2014

I found the answer, create a RUN macro.

First parameter of the RUN should be hidden with any value, and second parameter should contain the current logged in user id, find the example below

{run:replace=g1::hidden value:Hidden description:hidden, g2:$current_user_id:User

Then write groovy script inside RUN macro, like below

import org.apache.commons.lang.StringEscapeUtils; 
import com.atlassian.renderer.v2.RenderMode
def renderMode = RenderMode.suppress(RenderMode.F_FIRST_PARA)
def choiceSql = "select dept from AC_users where ad_user='$g2'"
def choiceMacro = "{sql-query:datasource=testDS|table=false} ${choiceSql} {sql-query}"
def choices = subRenderer.render(choiceMacro, context, renderMode)
if(choices == 'Design'){
   link = "<a href='http://gmail.com'>Design</a>"
}
out.println link
 
But I'm unable convert <a href tag into link ant its displaying as it is.
0 votes
Bob Swift OSS (Bob Swift Atlassian Apps)
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.
January 19, 2014

Please clarify your question. Are you talking about JIRA or Confluence? You have mentioned both in the labels. Remove the labels that don't apply.

0 votes
Bob Swift OSS (Bob Swift Atlassian Apps)
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.
January 19, 2014

For Confluence, Pre-defined variables describes the run variables available including user based variables.

0 votes
John Bishop
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.
January 19, 2014

You can get the ApplicationUser object for the currently logged in user with something like this:

defcurrentUser = ComponentAccessor.getJiraAuthenticationContext().getUser()

Then you should be able to call that object's getKey() method.

https://docs.atlassian.com/jira/latest/com/atlassian/jira/user/ApplicationUser.html

SKR January 19, 2014

Thanks for your quick response, however still I have got some doubt.

should I import any calss or library for this?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events