[LinkService] Reorder/refactor validation

Reoder validation to consider the composition capability
first; refactor to express as a single expression to clarify
validation logic (explicitly say 'and' instead of implying
it with control flow.)

Based on feedback from nasa/openmctweb#98
This commit is contained in:
Victor Woeltjen 2015-09-22 13:37:56 -07:00
parent 3310016264
commit c17269ba8b

View File

@ -39,20 +39,17 @@ define(
} }
LinkService.prototype.validate = function (object, parentCandidate) { LinkService.prototype.validate = function (object, parentCandidate) {
if (!parentCandidate || !parentCandidate.getId) { var objectId = object.getId();
return false; return !!parentCandidate &&
} !!parentCandidate.getId &&
if (parentCandidate.getId() === object.getId()) { parentCandidate.getId() !== objectId &&
return false; parentCandidate.hasCapability("composition") &&
} parentCandidate.getModel().composition.indexOf(objectId) === -1 &&
if ((parentCandidate.getModel().composition || []).indexOf(object.getId()) !== -1) { this.policyService.allow(
return false; "composition",
} parentCandidate.getCapability('type'),
return this.policyService.allow( object.getCapability('type')
"composition", );
parentCandidate.getCapability('type'),
object.getCapability('type')
) && parentCandidate.hasCapability('composition');
}; };
LinkService.prototype.perform = function (object, parentObject) { LinkService.prototype.perform = function (object, parentObject) {