Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to check current login group using javascript?

tpham March 19, 2012

Hi,

I am building a custom field using multiple select list and use java script to hide some options from some particular group. My current javascript is

<script type="text/javascript">

targetCategory = document.getElementById('customfield_10102');

conditionField = document.getElementById('Reporter');

subCategoryOptionsCount = document.getElementById('customfield_10102').length;

tempVar = 0;

if (conditionField=="admin")

{

while(tempVar < subCategoryOptionsCount)

{

if(targetCategory.options[tempVar].text=="VIC" )

{

targetCategory.remove(tempVar);

subCategoryOptionsCount--;

}

tempVar++;

}

}

</script>

But this doesn't work, I am pretty sure I am messed up in conditionField. Can anyone help?

Thanks,

Tuan

3 answers

1 accepted

0 votes
Answer accepted
srinivasp
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.
March 19, 2012

Use the below rest calls for getting the login user and his associated groups.

AJS.$.get("/rest/auth/1/session", function (data) {

userName = data.name;

AJS.$.get("/rest/api/2.0.alpha1/user", {username: userName, expand: "groups"}, function (data) {

var groups = data.groups;

var groupItems = jQuery.map(groups.items, function (val, j) {

return val.name

});

{);

{);

tpham March 19, 2012

Thanks, Spatruni. However, i cannot make it works on my screen. Can you please review my code?

<script type="text/javascript">

AJS.$.get("/rest/auth/1/session", function (data) {

userName = data.name;

{);

targetCategory = document.getElementById('customfield_10102');

subCategoryOptionsCount = document.getElementById('customfield_10102').length;

tempVar = 0;

if (userName=="Administrator")

{

while(tempVar < subCategoryOptionsCount)

{

if(targetCategory.options[tempVar].text=="VIC" )

{

targetCategory.remove(tempVar);

subCategoryOptionsCount--;

}

tempVar++;

}

}

</script>

Is there any chance you can also advise me how to retrieve the group of this login user?

Thanks,

tpham March 19, 2012

Hi Spatruni,

This is my first time using a REST call, can you please elaborate more on your code?

Thanks,

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.
March 26, 2012

Hi Spatruni,

the above scripts works well on jira 5.0,

how to make it work in jira 4.0? is there any alternate to get loggin user in javascript?

Thanks

Rambanam

JamieA
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.
March 26, 2012
0 votes
tapraj March 1, 2021

Hi 

<script type="text/javascript">
var user=getCurrentUserName()
alert(1234+user)
function getCurrentUserName()
{
var user
AJS.$.ajax({
url: "/rest/gadget/1.0/currentUser",
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
user = data.username;
}
});
alert(6666)
return user;
}
</script>

when I used this code the username is undefined 

0 votes
srinivasp
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.
March 19, 2012

I posted the code for getting the groups aswell. Please test it. May be you require two more closing braces for that. Just put some alerts and test whether you are able to get the response.

tpham March 19, 2012

I've concluded your code into the following function

function getGroups(user)

{

var groups;

AJS.$.ajax({

url: "/rest/api/2.0.alpha1/user?username="+user+"&expand=groups",

type: 'get',

dataType: 'json',

async: false,

success: function(data) {

groups = data.groups.items;

}

});

window.alert(groups);

return groups;

}

But it seems the URL is not correct since I get an "undefined" from group.

srinivasp
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.
March 19, 2012

Means you are running jira at the context /jira. In that case just add /jira to both urls. Better would be to replace both with AJS.params.baseURL + "/jira//rest..."

tpham March 19, 2012

I tried those, this link works

/rest/gadget/1.0/currentUser

This link doesn't

AJS.params.baseURL + "/jira/rest/api/2.0.alpha1/user?username="+user+"&expand=groups"

or "/rest/api/2.0.alpha1/user?username="+user+"&expand=groups"

tpham March 19, 2012

I am using jira 5 installed on linux server on Amazon

srinivasp
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.
March 19, 2012

Which version of jira are you using? It works for 4.x and 5 only.

srinivasp
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.
March 19, 2012

But for me it worked in the same way. Are you able to get the jira user from first call?

tpham March 19, 2012

Yes, Spatruni. I can get the username out of this function

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;

}

The URL /rest/gadget/1.0/currentUser also works for me when I put it into the browser. However. The API one doesn't work. Am I missing anything?

srinivasp
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.
March 19, 2012

Have you tried the way i suggested to get the groups?

tpham March 19, 2012

Hi Spatruni, I've made it works. Thanks so much for your hint, it was because the link should be /rest/api/latest/user?username="+user+"&expand=groups

Like sefti likes this
tapraj March 1, 2021

Hi 

I used the above URL for getting group names

But Im getting the following output when I am trying to display the retrieved group list

Whats wrong?glitch.PNG

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events