Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

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 !

0 votes

Comments

I use rtc 502



One answer

Permanent link

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

.

0 votes

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.

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,942
× 17
× 6
× 5

Question asked: Jan 13 '17, 5:34 a.m.

Question was seen: 2,919 times

Last updated: Jan 15 '17, 6:21 p.m.

Confirmation Cancel Confirm