how to check currently logged in user of JIRA in velocity template ?

Hi,

I have developed JIRA custom field plugin(type : text) and i want it to be visible only for particular user .

How to check currently logged in user of JIRA in velocity template ?

Do i need to fetch loggedin user in my java file and then assign to varaible then it can be accessible to vm file

5 answers

1 accepted

you can try with $authcontext.loggedInUser.name

or using javascript you can get current loggedin user details

try with this inside javascript,

AJS.params.loggedInUser

try with this idf you want check current user in specific group

${jiraUserUtils.getGroupNamesForUser($authcontext.loggedInUser.name).contains('jira-users')}

check this, it may help you

https://answers.atlassian.com/questions/31839/jira-api-check-if-user-is-in-group

if something helped then don't forgot to accept as a answer it will help to other users to choose right answer!!

Hi ,

Thanks lot :)

I ll explain my scenario :

I have developed one custom field plugin , wanted to display only for the user who belong to particular group.

I have used the below line of code in my edit.vm file and built a jar deployed in jira using admin role , then created new custom field using that JAR addedd to particular project and to the screen which the project is using.

After logeg in to jira as normal user still i'm not able to view that field when creating issue.

Could you pls help me

#if (${jiraUserUtils.getGroupNamesForUser($authcontext.loggedInUser.name).contains('Jira-users')})
#if($value)
<input type="text" name="$customField.id" value="$value" id="$customField.id" class="text"/>
#else
<input type="text" name="$customField.id" value="" id="$customField.id" class="text"/>
#end
#end

Still it is hidden only . Even if user in 'jira-user' group not able to view when creating issue .

which version of jira you are using?

I m using Jira v 5.0.

can you confirm what is the group name you used

jira-users or Jira-users ? check the first leter, it should be small leter

Hi rambanam ,

great !!!!!! it's working fine. thanks a lot .

and one more doubt. field name should also be hidden . is it possible? because custom field name being created when creating new custom field

Glad to hear it worked!!

where needs to hidden the field name?

In screen i want to hide label name of the custom field. (ex : when creating issue)

Hi ,

Is there any document where i could find how to use objects from JIRA-API class in velocity template?

For "jiraUserUtils" which is being used in velocity template. i am not able find any API document for further reference

hi @rambanam prasad

Thanks for your immmediate respone for my queries .

In the above link i coudn't find "jiraUserUtils" object .The only thing i could see the object for velocity template in this link, but i want to know how the objects can be used in velocity template

EX : ${jiraUserUtils.getGroupNamesForUser($authcontext.loggedInUser.name).contains('jira-users')}

In the above example "$authcontext" object is used to find logged in user and i have checked the corresponding API class in the below link:

https://docs.atlassian.com/jira/latest/com/atlassian/jira/security/JiraAuthenticationContext.html

Here i couldn't find "loggedInUser" method instead "getLoggedInUser" is available anyhow i considered that "getLoggedInUser" can also be called as "$authcontext.loggedInUser" ,but "name" is not available in that class.

from where it's being called.

Could you please explain ?

hi @rambanam prasad

Actually the field is displaying based on the user group but i also want to hide label name for the field . how to hide it ?

i am not much expertise in velocity files, i suggest gothrough the following document

http://velocity.apache.org/engine/devel/user-guide.html

you can find the "jiraUserUtils" Object here i think you can use UserUtils Object also to get user details

https://developer.atlassian.com/display/JIRADEV/Velocity%20Context%20for%20Email%20Templates

"$authcontext.loggedInUser" will return the User Object in that getName() method is there so use the "$authcontext.loggedInUser.name"

How to hide label:

where you have declared label for your field?

@rambanam prasad

Actually i have build custom field jira plugin where it is visible only for "jira-users"

group .then i have uploaded to "Universal plugin manager" in JIRA

When creating new custom field ,Name for the field must be set and assigned that filed to screen which is used in "X" project.

when creating issue , field input box displaying to the user who belong to the "Jira-users" group, but when the other user creating the issue at that time filed is hidden but the name for the field is still displaying which has been set when the custom field is created.


check i have added another answeer!!

Hi Augustin,

you can use $authcontext.getUser().getName()=='user1'

and follow link to know about velocity variables.

appreciated if accept as an answer.

Thanks a lot :)

Hi Bharadwaj,

I want to display the field only to the members who is belong to 'jira-users' group .

https://confluence.atlassian.com/display/JIRA040/How+to+create+a+new+Custom+Field+Type

I have tried this above refrence URL ,but it is not displaying field even the user belong to 'jira-users' group

In the above example i have used 'Jira-users' instaed of 'Jira-administrators'.

check my answer i have updated!!

@Augustin

i am not sure how to hide the custom field name in velocity file

i have another solution to hide a field to specific group/user with javascript, add the following script as webresource in a plugin

&lt;script type="text/javascript"&gt;  
jQuery(document).ready(	function($) {
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function(e,context) {
hideField();    
});
hideField(); 
function hideField(){
        var user=getCurrentUserName();		
		var statusText=$('#status-val').text();	
		
		if(isUserInGroup(user,'Developers') ){
			//to hide field
			$("#customfield_10571").closest('div.field-group').hide();
		}else  {
			AJS.$("#customfield_10571").closest('div.field-group').show();
		}

}
function getCurrentUserName()
{
var user;
     AJS.$.ajax({
        url: "/rest/gadget/1.0/currentUser",
        type: 'get',
        dataType: 'json',
        async: false,
        success: function(data) {
            user = data.username;
        }
     });
     return user;
}
  
  
function getGroups(user)
{
var groups;
     AJS.$.ajax({
        url: "/rest/api/2/user?username="+user+"&amp;expand=groups",
        type: 'get',
        dataType: 'json',
        async: false,
        success: function(data) {
            groups = data.groups.items;
        }
     });
     return groups;
}
function isUserInGroup(user, group){
    var groups = getGroups(user);
    for (i = 0; i &lt; groups.length; i++){
         if (groups[i].name == group){
              return true;
         }
    }
    return false;
}
    
});
&lt;/script&gt;

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

178 views 1 10
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