Adding conditions for web fragments

As we saw in the previous recipes, adding a web fragment is pretty easy. However, the job doesn't always end there. In many cases, we would want to limit the web item based on a set of conditions.

For example, an Edit link on an issue should only appear for people with edit permission on an issue. The Administration link should appear only if the user is a JIRA administrator. In this recipe, let us look at how we can implement conditions for displaying web fragments.

How to do it...

It is possible to add one or more conditions to a web section or web item. In the latter case, the conditions element is used, which is a collection of condition/conditions elements and a type attribute. The type attribute is either the logical AND or OR.

For example, the following condition specifies that the user should have either the admin permission or use permission in a project before he/she can see the web fragment that has the condition on it.

<conditions type="OR"> 
     <condition class="com.atlassian.jira.plugin.webfragment
     .conditions.JiraGlobalPermissionCondition"> 
  
       <param name="permission">admin</param>

     </condition>

     <condition class="com.atlassian.jira.plugin.webfragment
     .conditions.JiraGlobalPermissionCondition">

       <param name="permission">use</param>
 
    </condition>
</conditions> 

Possible values of permission, as of 7.0, are admin,use, sysadmin, project, browse, create, edit, update (same as edit), scheduleissue, assign, assignable, attach, resolv, close, transition, comment, delete, work, worklogdeleteall, worklogdeleteown, worklogeditall, worklogeditown, link, sharefilters, groupsubscriptions,move, setsecurity, pickusers, viewversioncontrol, modifyreporter, viewvotersandwatchers, managewatcherlist,bulkchange, commenteditall,commenteditown, commentdeleteall, commentdeleteown, attachdeleteall, attachdeleteown, and viewworkflowreadonly. This list can be found from the com.atlassian.jira.security.Permissions class.

Let us consider a simple example of how to write a condition and display the web items based on it. In this example, we will display a web item in the top navigation bar if, and only if, the user has logged in and belongs to the jira-developers group. The following are the steps:

  1. Write the condition class. The class should extend the AbstractWebCondition class and override the following abstract method.
            public abstract booleanshouldDisplay(ApplicationUser user, 
            JiraHelperjiraHelper);
  2. In our example, all we need to check is that the user is not null and is a member of the group jira-developers. The class is implemented as follows:
            public class DeveloperCondition extends AbstractWebCondition{
                @Override 
                public booleanshouldDisplay(ApplicationUser user, 
                JiraHelper helper) {
                   return user != null && ComponentAccessor.getGroupManager()
                    .getGroupNamesForUser(user).contains("jira-developers"); 
                  }
            }
  3. Add the new condition class in the web-item:
            <web-item key="jtricks-condition-menu" 
             name="JTricks Condition Menu" 
             section="system.top.navigation.bar" weight="160">
         
              <description>J Tricks Web site with condition</description>
              <label>JTricks Conditional Menu</label>
              <tooltip>J Tricks Web site</tooltip>
              <link linkId="jtricks-condition-menu">
                 http://www.j-tricks.com
              </link>
              <condition class="com.jtricks.ui.conditions.DeveloperCondition"/>
             </web-item>

    As you can see, the section here is system.top.navigation.bar, which will place the new link on the Top Navigation bar. The link will only be visible if the condition DeveloperCondition returns true.

    We can easily invert a condition using the invert flag as follows:

              <condition class=
               "com.jtricks.ui.conditions.DeveloperCondition" 
                invert="true"/>

    This will display the link if the user is not logged in or not in the group of JIRA developers!

  4. Deploy the plugin.

How it works...

Once the plugin is deployed, we can see that the new JTricks Conditional Menu is rendered in the top navigation bar only when the user is logged in and in the group of jira-developers.

The following screenshot shows the dashboard of a user who is logged in and in the group ofjira-developers:

How it works...

If the user is not logged in, or if the user is not in the jira-developers group, the condition is not satisfied and the above menu is not shown.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.172.195