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

Can I get user's groups via javascript in Confluence from the Custom HTML

DoD Support August 1, 2014

Is it possible to get the groups the currently logged in user belongs to in Atlasian Confluence using Javasrcipt stored in the "Custom HTML"?

I can get the currently logged in user as follows but cant find an example for getting the user's groups:

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;

}

(EDIT:)

All I really need to do is:

  • Get the current user
  • Check if the user is in a specific Group
  • If they are, bind a jQuery function to avery `<a>` tag (link) on the page.

Im really considering just writing a html "shell" page that handles user management and showing Confluence in an iFrame.

That, or maybe just store the usernames I want to match in a javascript array or JSON object there in the `custom HTML` and just match the user name that I can already get directly, foregoing groups all together.

Neither are ideal but wouldnt require the learning curve of throwing in Java for such a small need. Any issues I might run into with those ideas?

3 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Stephen Deutsch
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 6, 2014

Your easiest bet would be to use a simple user macro, something similar to as follows:

## @param group:title=Group|type=string|desc=Enter group name
#if ($userAccessor.hasMembership($paramgroup, $req.getRemoteUser()))
  &lt;script&gt;
    ## Your jQuery here
  &lt;/script&gt;
#end

Replace "## Your jQuery here" with the required javascript, save it under a particular name (like "bindlinks"), and make sure that it is set to "Visible only to system administrators in the Macro Browser".

If you're using the documentation theme, then embedding it in every page is easy. Just go to the admin section, themes, configure theme, and add the following line in the header section: {bindlinks:group=groupname}, where groupname is the name of the group that you want.

If you're using the default theme, then you need to go to Layouts, find "Page Layout", and click on "Create Custom". At the end of the text put the following line and save:

$action.getHelper().renderConfluenceMacro("{bindlinks:group=groupname}")

I think that's the easiest way to solve your problem. If you have any other questions, let me know.

DoD Support August 7, 2014

Giving this a shot I'll let you know how it works out. Thank you!

1 vote
Davin Studer
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 3, 2014

Using the json-rpc API you can get this.

http://wiki/rpc/json-rpc/confluenceservice-v2/getUserGroups

Pass it ["username"].

AJS.$.ajax({
	dataType: 'json',
	contentType: 'application/json',
	type: 'POST',
	url: 'http://wiki/rpc/json-rpc/confluenceservice-v2/getUserGroups',
	data: '["' + {username} + '"]',
	async: false,
	success: function(data){
		for(var group in data) {
		}
	}
});

Davin Studer
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 4, 2014

Does this work for your needs?

DoD Support August 6, 2014

Hm, this works but only if the crrent user is a Confluence Admin. If I run this while logged in as a non-admin, I get the below error returned:

The application was unable to serve your request: com.atlassian.confluence.rpc.NotPermittedException: You don't have the 'Confluence Administrator' permission.

Any way to make this work as a non-admin?

Davin Studer
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 6, 2014

On the confluence admin under general configuration do you have "Remote API" checked?

DoD Support August 7, 2014

I did not, so I checked it. Same result though, same error if not logged in as an admin.

1 vote
Bhushan Nagaraj
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, 2014

Hi,

You will have access to the userAccessor in your velocity file where you might be entering your javascript. Take a look at the objects accessible in velocity.

https://developer.atlassian.com/display/CONFDEV/Confluence+Objects+Accessible+From+Velocity

Then using $userAccessor.getGroupNamesForUsername method, you can get groups.

https://docs.atlassian.com/com/atlassian/confluence/atlassian-confluence/latest/index.html?com/atlassian/confluence/user/UserAccessor.html

If you are working on a custom plugin that requires javascript manipulation before serving content, you might find this tutorial helpful.

http://blog.jiradev.com/2014/07/web-resource-transformer-plugin-module.html

DoD Support August 2, 2014

Hi thank you for your time,

Yeah, I was afraid of that. I know nothing about Velocity / Java. I was hoping to be able to do this without having to write a plugin, just by using Javascript loaded in the `Custom HTML`( so it loads in the head of every page on the site). You can get the username easily this way, it's a shame you cant also get the user's groups.

Are there any easy to follow tutorials for writing a simple plugin? I read through the one you posted and it looks like just what I would need. But now I m looking for something to spell out the basic plugin writing/installing process for me.

All I really need to do is:

  • Get the current user
  • Check if the user is in a specific Group
  • If they are, bind a jQuery function to avery `<a>` tag (link) on the page.

Im really considering just writing a html "shell" page that handles user management and showing Confluence in an iFrame.

That, or maybe just store the usernames I want to match in a javascript array or JSON object there in the `custom HTML` and just match the user name that I can already get directly, foregoing groups all together.

Neither are ideal but wouldnt require the learning curve of throwing in Java for such a small need.

(editing my question to include that)

Bhushan Nagaraj
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 3, 2014
I would highly recommend implementing all your customization as addons as this will signifactly ease your upgrades in future. Atlassian developer documentation has good documentation to get you started with developing your own add-on. www.jiradev.comhas everything you need to build a simple plugin. Take a look at the tutorials available here.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events