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

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

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

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

should I import any calss or library for this?

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

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.

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.

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...

332 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