Is there a way to check if the user is jira-administrator using a rest call?
Community moderators have prevented the ability to post new answers.
Here's some sample code for doing what you want:
AJS.$.get("/jira/rest/auth/1/session", function (data) { userName = data.name; // check groups AJS.$.get("/jira/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}); if (jQuery.inArray("jira-administrators", groups.items)) { ... } }); });
Use developer tools in Firefox or chrome to look at the console for errors, and put semi-colons after your alerts, that's not valid javascript. And when you have the console working replace the alerts with console.log()...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for sharing sample code. I tried to execute that but it it is not working. I added some alerts but it is not calling any of the alerts which means the control is not going inside the function except the first alert.
jQuery(document).ready(function() { alert(1) AJS.$.get("/jira/rest/auth/1/session", function (data) { alert(2) userName = data.name; alert("3 "+userName) // check groups AJS.$.get("/jira/rest/api/2.0.alpha1/user", {username: userName, expand: "groups"}, function (data) { alert(4) var groups = data.groups; alert(5) var groupItems = jQuery.map(groups.items, function (val, j) {return val.name}); alert("6 "+groupItems) if (jQuery.inArray("jira-administrators", groups.items)) { alert("admin "); } }); }); });
Please suggest if there is any change that is requried.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In the console i find the following information.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
OK that assumes you are running jira at the context /jira. In that case just remove /jira from both urls. Better would be to replace both with AJS.params.baseURL + "/rest..."
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I replaced /jira from both the URL's and i am able to see the alerts now...
Thanks a ton for your solution.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
"jQuery." should be "AJS.$." and inArray returns -1 if not found, so not to fail on the first array index, the condition should be "inArray(...) >= 0" and the 2nd groups.items should be groupItems
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.
Yep... although it's a combo of two calls:
will return you the currently logged in username.
GET https://jira.atlassian.com/rest/api/2/user?username=[USERNAME]&expand=groups
will get you the groups they belong to... along with other user info.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Rich,
Thanks for your reply. I am actually new to Rest and so i have no idea about this.I have seen in some documents using the Rest and so written code like below with your information. Could you please let me know whether it is correct or nor? If not could you please suggest the right way of handling this?
jQuery(document).ready(function() {
/*var username = GET https://jira.atlassian.com/rest/auth/1/session var groups = GET https://jira.atlassian.com/rest/api/2/user?username=[USERNAME]&expand=groups*/ var base = https://localhost:8080 var username = jQuery.getJSON(base + '/rest/auth/1/session', function(data) { jQuery.ajax({ type: "POST", url: base + "/rest/auth/1/session", dataType: "json", contentType: "application/json" }) }) alert(username) });
Thanks in advance,
Patruni
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Does this help? http://jsbin.com/exehug/5/edit#html,live
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Rich,
I am able to get the username but i am not getting the groups. tested with some alerts and found that all the alerts are coming without calling the groupsCallback() method, i.e. missing alert 7.
// Called after /session is called getting the username function authCallback(d){ alert(d.name) var head= document.getElementById('main-content'); alert(2) var script= document.createElement('script'); alert(3) script.type= 'text/javascript'; alert(4) script.src = 'http://localhost:8080/rest/api/2/user?username=' + d.name + '&expand=groups&jsonp-callback=groupsCallback'; alert(5) head.appendChild(script); alert(6) } // Called after /user?username= is called getting the user's displayName and groups function groupsCallback(d){ alert(7) var names = $.map(d.groups.items,function(i,e){ return i.name; }) alert(d.displayName); alert(names.join(', ')); }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am using 4.4.4
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What version of JIRA are you running? The above example works with JIRA 5... haven't tested with 4.x
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You seem to be assuming it's JSONP - but what's returned does not have the padding which would make it jsonp.
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.