Missed Team ’24? Catch up on announcements here.

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

how to check if the login user is a jira-administrator using a rest call.

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.
February 28, 2012

Is there a way to check if the user is jira-administrator using a rest call?

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
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.
February 28, 2012

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)) {
            ...
        }
    });
});

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.
February 28, 2012

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()...

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.
February 28, 2012

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.

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.
February 28, 2012

In the console i find the following information.

  1. GET http://localhost:8080/jira/rest/auth/1/session?_=1330545129416 404 (Not Found)
    1. d.support.ajax.d.ajaxTransport.sendbatch.js:11
    2. d.extend.ajaxbatch.js:11
    3. d.each.d.(anonymous function)batch.js:11
    4. (anonymous function)projectadmin.js:4
    5. d.extend._Deferred.f.resolveWithbatch.js:11
    6. d.extend.readybatch.js:11
    7. c.addEventListener.y
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.
February 28, 2012

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

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.
February 28, 2012

I replaced /jira from both the URL's and i am able to see the alerts now...

Thanks a ton for your solution.

Nabil Sayegh
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.
August 2, 2016

"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

Dean McGinn August 17, 2018

AJS.toInit(function(){...});

1 vote
rich
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 28, 2012

Yep... although it's a combo of two calls:

GET https://jira.atlassian.com/rest/auth/1/session

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.

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.
February 28, 2012

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

rich
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 28, 2012
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.
February 28, 2012

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(', '));
  }

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.
February 28, 2012

I am using 4.4.4

rich
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 28, 2012

What version of JIRA are you running? The above example works with JIRA 5... haven't tested with 4.x

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.
February 28, 2012

You seem to be assuming it's JSONP - but what's returned does not have the padding which would make it jsonp.

TAGS
AUG Leaders

Atlassian Community Events