Hide custom field type in edit / view screen

devzero0815 May 29, 2023

I've written an own plugin according to https://developer.atlassian.com/server/jira/platform/creating-a-custom-field-type/ 

hiding custom field to users being member of a specific project role:

 

package com.test;
import com.atlassian.jira.issue.customfields.impl.GenericTextCFType;
import com.atlassian.jira.issue.customfields.manager.GenericConfigManager;
import com.atlassian.jira.issue.customfields.persistence.CustomFieldValuePersister;
import com.atlassian.jira.issue.customfields.view.CustomFieldParams;
import com.atlassian.jira.issue.fields.TextFieldCharacterLengthValidator;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.plugin.spring.scanner.annotation.imports.JiraImport;
import java.util.Collection;
import java.util.Map;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.security.roles.*;
import java.util.Iterator;
public class JiraCustomField extends GenericTextCFType {
private final JiraAuthenticationContext authContext;
private final ProjectRoleManager roleManager;
public JiraCustomField(
@JiraImport ProjectRoleManager projectRoleManager,
@JiraImport CustomFieldValuePersister customFieldValuePersister,
@JiraImport GenericConfigManager genericConfigManager,
@JiraImport TextFieldCharacterLengthValidator textFieldCharacterLengthValidator,
@JiraImport JiraAuthenticationContext jiraAuthenticationContext) {
super(customFieldValuePersister, genericConfigManager, textFieldCharacterLengthValidator , jiraAuthenticationContext);
this.authContext = jiraAuthenticationContext;
this.roleManager = projectRoleManager;
}
@Override
public Map getVelocityParameters(Issue issue, CustomField field, FieldLayoutItem fieldLayoutItem) {
Map params = super.getVelocityParameters(issue, field, fieldLayoutItem);
Collection<ProjectRole> roles = roleManager.getProjectRoles(authContext.getLoggedInUser(), issue.getProjectObject());
Iterator<ProjectRole> iterator = roles.iterator();
boolean bHidden = false;
while (iterator.hasNext()) {
if(iterator.next().getName().startsWith("external")) {
bHidden = true;
break;
}
}
params.put("hidden", bHidden);
return params;
}
}

 

Parameter "hidden" is successfully accessible in the vm files.

I've managed to set the field to readonly in edit screen which works flawlessly.

 

#disable_html_escaping()
#customControlHeader ($action $customField.id $customField.name $fieldLayoutItem.required $displayParameters $auiparams)
#if ($hidden)
<input class="text" id="$customField.id" name="$customField.id" type="text" value="[hidden]" readonly="readonly"/>
#else
<input class="text" id="$customField.id" name="$customField.id" type="text" value="$textutils.htmlEncode($!value)"/>
#end
#customControlFooter ($action $customField.id $fieldLayoutItem.fieldDescription $displayParameters $auiparams)

Unfortunately in view screen there is always the little edit pencil behind the field which allows all users to see and edit the field.

Question:

How can I hide content and the edit button in view screen to non authorized users?

1 answer

1 accepted

2 votes
Answer accepted
Graham Twine
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.
May 29, 2023

Hello @devzero0815 ,

What mapping have you got in your plugin.xml for the different screens?

There is a view, edit and xml mapping to a velocity template in the custom-field element.

Are you using the same template for each of these?

Note: This is due to the global Inline edit feature in Jira.

I have not had the need to disable editing based on groups so I am unsure if the view template will disable this but that would be the first place to check.

You need to consider how to find the element on the view screen using a css selector on a class
Presuming you have a javascript file included in your downloadable resources you can add a unique css class to your elements that you KNOW will not find with other fields on the screen

//untested
var
foundElements = AJS.$('.your-custom-css-class') if (foundElements.length){ foundElements.removeClass('editable-field'); }

You may also want to ask this question in the developer community rather than this community.

You can find the developer community here

https://community.developer.atlassian.com/

devzero0815 May 30, 2023

Hi @Graham Twine ,

thank you very much for pointing to the JIRA inline edit feature.
By now I just disabled it and I get what I want.

Next I will also test your JS solution.

Best regards

--

Suggest an answer

Log in or Sign up to answer