Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to check current login group using javascript?

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

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

});

{);

{);

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,

Hi Spatruni,

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

Thanks,

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

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.

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.

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

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"

I am using jira 5 installed on linux server on Amazon

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

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

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?

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

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 sarah likes this

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

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 

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you