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?
Community moderators have prevented the ability to post new answers.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
CompoentAccessor.getJiraAuthenticationContext().getLoggedInUser().getName()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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; } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try com.atlassian.jira.workflow.condition.UserInGroupCondition, its a Workflow condition that checks if the caller is in the required argument "group"
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.