Posts Tagged Java
Using jQuery and Custom fields to enforce issue type security in Jira
Posted by suddenelfilio in Programming, Web & Design on 06/09/2010
When working on our companies new issue tracking system I ran into the need to be able to prevent certain users or groups to create a specific type of issue. This however strange it may seem is not supported out-of-the-box by Jira and browsing the Jira Issue site the nice people at Atlassian don’t seem to put high value in this much requested feature. Anyways looking at the comments in the issue I discovered that you can use a custom field called Velocity processed Custom Message Field (for edit) to use a mix of Apache Velocity templating and javascript to do the trick. Okay it is a hack, but for now it is the only possibility out there that I’ve discovered.
Now before you go looking for the custom field you have to know that is not installed by default. The Velocity processed Custom Message Field can be found in a Jira plugin called Jira Toolkit over at the Plugin exchange. So if you want this to work go and install this plugin.
Now with the plugin installed create a new custom field of type Velocity processed Custom Message Field (for edit), give it a name and make sure you select All issue types and the global context! Don’t worry I’ll explain later why.
Having created the custmom field it is time for the logic. What we are going to do is to create a mix of velocity script and javascript to show an error message to a user when he/she does not belongs to a certain user group and remove all the input fields from the form. The script I’m going to use for that looks as follows:
#if ($authcontext.user.inGroup('developers'))
##do nothing
#else
<script language="JavaScript">
jQuery(function ()
{
AJS.$("<div class='warningBox maxWidth' style='text-align:center;color:DarkRed'><h1>$authcontext.user.getFullName() ,<br/>This issue type is for internal use only. <br/>Please create an Incident, Enhancement or New Feature instead!</h1></div>").appendTo('.intform');
AJS.$(".jiraform").remove();
});
</script>
#end
A little explenation is in order here I guess. All the lines preceded by the # sign is Velocity markup. Jira uses the Velocity templating engine to create its webpages. What the velocity code does is rather straight forward it checks if the current user is in the group ‘developers’ - $authcontext.user.inGroup(‘developers’) -. If that is the case then do nothing. When the current user is not in the ‘developers’ user group a jQuery script will be executed which appends a div with a warning to the page and then removes the HTML table that contains all the input fields. A little note on this is that Jira must load the jQuery library. I found that my instance has got a line like this that load the jQuery library:
<script type="text/javascript" src="/s/531/1/2.1.3/_/download/batch/com.atlassian.auiplugin:ajs/com.atlassian.auiplugin:ajs.js" ></script>
If you don’t have the jQuery library you need to make sure it is loaded.
So that is the script that will do the magic stuff. Now all you need to do is enter it as the default value for your custom field you created above. You can do this by clicking the ‘configure’ link and on the new page ‘edit default value’. In the textbox paste the script and update the field.
Remember that I said you needed to select all issue types and the global context when you created the custom field? The reason for that is that in Jira 4.1.x there seems to be an issue with setting the default value when you have not selected as is. When you select specific issue types and projects instead of all issue types and global context you’ll get strange page when you go and try to set the custom field’s default value. The textbox is not there. This is a known issue and the woirkaround for when you need to edit the default value is to set it back to all issue types and global context. I know that’s not really fun to do, but it will get it done!
Now to secure certain issue types you don’t want non developers to create just select the types you want and if necessary select the project(s). You can do this by clicking the ‘configure’ link of the custom field and then select the ‘Edit configuration’ option.
After you’ve saved these settings creating a prohibited issue type will render like this. What you see below is a customer trying to create a bug while a customer is only allowed to create an incident, New Feature or Enhancement
As you can see you address the the user in this case Customer Person (which is the Fullname of the logged in jira user) that he/she can only create an Incident, Enhancement or New Feature. Also notice that there are no input fields on the form.
So depending on the version of jira you are using you will need to tweak the jQuery to show and remove the correct elements, but this should be applicable to any version where you can use the Velocity processed Custom Message Field.
A small note I wish to add is that when a user disable javascipt in his/hers browser this solution will become useless since it uses javascript to disable those fields, but then again so becomes the rest of the internet just try and surf without javascript enabled
In part 2 I’ll show you how to change the label of the Version field using similar techniques.
Release a new project: Microsoft Tag Java Library
Posted by suddenelfilio in MSTagLib on 22/11/2009
Remember the previous PHP library I released for the Microsoft Tag API, well this time I’m releasing a library for Java development.
Go check it out at codeplex: http://mstagjavalib.codeplex.com/



