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
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Looks like the same code from my answer here: https://answers.atlassian.com/questions/38542/how-to-check-if-the-login-user-is-a-jira-administrator-using-a-rest-call
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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..."
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Which version of jira are you using? It works for 4.x and 5 only.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
But for me it worked in the same way. Are you able to get the jira user from first call?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Have you tried the way i suggested to get the groups?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.