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

Augustin atto September 10, 2013

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

7 votes
Answer accepted
RambanamP
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.
September 10, 2013

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

RambanamP
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.
September 10, 2013

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

Augustin atto September 11, 2013

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 .

RambanamP
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.
September 11, 2013

which version of jira you are using?

Augustin atto September 11, 2013

I m using Jira v 5.0.

RambanamP
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.
September 11, 2013

can you confirm what is the group name you used

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

Augustin atto September 11, 2013

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

RambanamP
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.
September 11, 2013

Glad to hear it worked!!

where needs to hidden the field name?

Augustin atto September 11, 2013

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

Augustin atto September 11, 2013

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

RambanamP
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.
September 11, 2013
Augustin atto September 12, 2013

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 ?

Augustin atto September 12, 2013

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 ?

RambanamP
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.
September 12, 2013

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?

Augustin atto September 12, 2013

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


RambanamP
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.
September 12, 2013

check i have added another answeer!!

1 vote
Bharadwaj Jannu
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.
September 10, 2013

Hi Augustin,

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

and follow link to know about velocity variables.

Augustin atto September 10, 2013

Thanks a lot :)

Bharadwaj Jannu
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.
September 10, 2013

appreciated if accept as an answer.

0 votes
RambanamP
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.
September 12, 2013

@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;

0 votes
Augustin atto September 11, 2013

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

RambanamP
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.
September 11, 2013

check my answer i have updated!!

0 votes
Augustin atto September 10, 2013

Thanks a lot :)

Suggest an answer

Log in or Sign up to answer