How to get the login user in JS of validate?
Hello,
I want to prenvent saving workitem when the owner is not equal the login user.I use the js following:
dojo.provide("org.example.validateStateFirst");
dojo.require("com.ibm.team.workitem.api.common.Severity");
dojo.require("com.ibm.team.workitem.api.common.Status");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("com.ibm.team.repository.web.client.session.Session");
(function() {
var Severity= com.ibm.team.workitem.api.common.Severity;
var Status= com.ibm.team.workitem.api.common.Status;
var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;
var getAuthenticatedContributor = com.ibm.team.repository.web.client.session.getAuthenticatedContributor;
var validateState= dojo.declare("org.example.validateStateFirst", null, {
validate: function(attributeId, workItem, configuration) {
var severity= configuration.getChild("parameters").getStringDefault("severity", Severity.ERROR.name);
var message= configuration.getChild("parameters").getStringDefault("message", "");
var state= workItem.getValue(attributeId);
var billType = workItem.getValue(WorkItemAttributes.TYPE);
var owner = workItem.getLabel("owner");
if(billType==="com.ygsoft.billType.bug"){
if(state==="com.ygsoft.workflow.bug.state.s4"){
var currentUser =getAuthenticatedContributor().name;
if(owner!=currentUser){
return new Status(Severity[severity], message64);
}
}
}
return Status.OK_STATUS;
}
});
})();
It can show error message beside the arttribute,but cann't prevent the user saving workitem,when I change the “currentUser” into constant ,like this:
var currentUser =getAuthenticatedContributor().name; ----------------->var currentUser ="testuser"
then the js can be properly implemented.Can show the error message at the top of the page and prevent user saving workitem.
I think it may be I got the wrong way to get the currenten user,but I don't know how to deal with it. Can someone help me ?
Thank you very much !
One answer
If using a validator, set the operational behavior (attribute validation and set severity to error) see https://jazz.net/wiki/bin/view/Main/AttributeCustomization.
I personally would avoid using JavaScript as it only works with the Web UI and only limited with the Eclipse client (getAuthenticatedContributor().name does not work with it,) and would prefer an advisor similar to
Only Owner Can Close WorkItem Advisor
.Comments
Thank you for your reply.
I have configured Attribute validation and the message in the Process Configuration Source ;But it still not works with the Web UI.Now I only want to realize it in Web UI.Are there other factors that contribute to this phenomenon?
dojo.provide("org.example.validateStateFirst");
dojo.require("com.ibm.team.workitem.api.common.Severity");
dojo.require("com.ibm.team.workitem.api.common.Status");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("com.ibm.team.repository.web.client.session.Session");
(function() {
var Severity= com.ibm.team.workitem.api.common.Severity;
var Status= com.ibm.team.workitem.api.common.Status;
var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;
var getAuthenticatedContributor = com.ibm.team.repository.web.client.session.getAuthenticatedContributor;
var validateState= dojo.declare("org.example.validateStateFirst", null, {
validate: function(attributeId, workItem, configuration) {
var severity= configuration.getChild("parameters").getStringDefault("severity", Severity.ERROR.name);
var message= configuration.getChild("parameters").getStringDefault("message", "");
var state= workItem.getValue(attributeId);
var billType = workItem.getValue(WorkItemAttributes.TYPE);
var owner = workItem.getLabel("owner");
if(billType==="com.ygsoft.billType.bug"){
if(state==="com.ygsoft.workflow.bug.state.s4"){
var currentUser =getAuthenticatedContributor().name;
if(owner!=currentUser){
return new Status(Severity[severity], message);
}
}
}
return Status.OK_STATUS;
}
});
})();
This is not going to work. As you have observed, the Web UI only presents the error messages and never prevents the user from clicking the Save button. Once the WI is posted to the server, it is up to the server side. For the reason Ralph already mentioned, your code does not work on the server side, and the Save operation just goes through. You should consider the option that Ralph suggested.
Comments
Li Li
Jan 15 '17, 6:17 p.m.I use rtc 502