Missed Team ’24? Catch up on announcements here.

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

What is the best way to determine if the current JIRA user is in a certain Group?

Eric Grubaugh February 3, 2013

I have a custom web item to display a link in JIRA's top navigation menu. I only want this link to display if the current user is in a specific user group. From the shouldDisplay method for my custom Condition, what is best way to determine if the current user is in a specific group?

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
Jobin Kuruvilla [Adaptavist]
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 4, 2013
Eric Grubaugh February 4, 2013

That looks like it will give me what I need. Here's the code I have in my Condition so far:

public boolean shouldDisplay(Map<String, Object> context) {
		GroupManager groupMngr = ComponentAccessor.getGroupManager();
		
		return groupMngr.isUserInGroup(???, 'fisheye-users');
}

The last question I have is then how do I get the current user's username (i.e. how do I fill in the ??? in the code)?

FYI This is my first plugin development attempt, and I am struggling to find documentation surrounding the Conditions for web items.

Jobin Kuruvilla [Adaptavist]
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 4, 2013

CompoentAccessor.getJiraAuthenticationContext().getLoggedInUser().getName()

Eric Grubaugh February 4, 2013

src/main/java/.../UserCanAccessFisheyeCondition.java:

package com.threesixtycloudsolutions.plugins.jira.appswitcher.web.condition;

import java.util.Map;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.security.groups.GroupManager;
import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.Condition;

/**
 * Defines a JIRA web fragment <code>Condition</code> that determines whether
 * the current user is able to access the 360 Cloud FishEye application.
 *
 * @see com.atlassian.plugin.web.Condition
 */
public class UserCanAccessFisheyeCondition implements Condition {

	/* (non-Javadoc)
	 * @see com.atlassian.plugin.web.Condition#init(java.util.Map)
	 */
	@Override
	public void init(Map<String, String> params) throws PluginParseException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see com.atlassian.plugin.web.Condition#shouldDisplay(java.util.Map)
	 */
	@Override
	public boolean shouldDisplay(Map<String, Object> context) {
		GroupManager groupMngr = ComponentAccessor.getGroupManager();
		JiraAuthenticationContext authCtx =
				ComponentAccessor.getJiraAuthenticationContext();
		
		// If a user is logged in, check if they are in the fisheye-users group
		if (authCtx.isLoggedInUser()) {
			String username = authCtx.getLoggedInUser().getName();
			return groupMngr.isUserInGroup(username, "fisheye-users");
		}
		
		return false;
	}
}
Eric Grubaugh February 4, 2013

Thank you, Jobin. With one little addition, that worked perfectly. For other noobs like me, here is the final code I used to get this working.

src/main/resources/atlassian-plugin.xml:

<!-- location on the web-section must match the linkId of the link on the main web-item (Applications, in this case) -->
<web-section name="AppSwitcher Web Section" key="app-switcher-web-section" location="app-switcher-menu" weight="1000">
</web-section>
<web-item name="Applications" i18n-name-key="app-switcher-menu.name" key="app-switcher-menu" section="system.top.navigation.bar" weight="1">
    <description key="app-switcher-menu.description">Main link item in the AppSwitcher menu</description>
    <label key="app-switcher-menu.label"/>
    <link linkId="app-switcher-menu"/>
</web-item>
<!-- The format of section for these web-items is [linkId for the primary link]/[web-section key] (without brackets) -->
<web-item name="Confluence" i18n-name-key="app-switcher-confluence-link.name" key="app-switcher-confluence-link" section="app-switcher-menu/app-switcher-web-section" weight="1000">
    <description key="app-switcher-confluence-link.description">Link to Confluence</description>
    <label key="app-switcher-confluence-link.label"/>
    <link linkId="app-switcher-confluence-link">http://wiki.360cloudsolutions.com/</link>
</web-item>
<web-item name="FishEye" i18n-name-key="app-switcher-fisheye-link.name" key="app-switcher-fisheye-link" section="app-switcher-menu/app-switcher-web-section" weight="1000">
    <description key="app-switcher-fisheye-link.description">Link to FishEye</description>
    <!-- This will call the shouldDisplay method of UserCanAccessFisheyeCondition, which will determine whether the web-item will display or not -->
    <condition class="com.threesixtycloudsolutions.plugins.jira.appswitcher.web.condition.UserCanAccessFisheyeCondition"/>
    <label key="app-switcher-fisheye-link.label"/>
    <link linkId="app-switcher-fisheye-link">http://reviews.360cloudsolutions.com/</link>
</web-item>

0 votes
Andy Brook [Plugin People]
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 3, 2013

Try com.atlassian.jira.workflow.condition.UserInGroupCondition, its a Workflow condition that checks if the caller is in the required argument "group"

TAGS
AUG Leaders

Atlassian Community Events