mirror of
https://github.com/nasa/openmct.git
synced 2025-06-16 22:28:13 +00:00
[Code Style] Use prototypes in policy bundle
WTD-1482.
This commit is contained in:
@ -53,12 +53,37 @@ define(
|
||||
* @returns {boolean} false if disallowed; otherwise, true
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* The `policyService` handles decisions about what things
|
||||
* are and are not allowed in certain contexts.
|
||||
* @interface PolicyService
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check whether or not a certain decision is allowed by
|
||||
* policy.
|
||||
* @param {string} category a machine-readable identifier
|
||||
* for the kind of decision being made
|
||||
* @param candidate the object about which the decision is
|
||||
* being made
|
||||
* @param context the context in which the decision occurs
|
||||
* @param {Function} [callback] callback to invoke with a
|
||||
* string message describing the reason a decision
|
||||
* was disallowed (if its disallowed)
|
||||
* @returns {boolean} true if the decision is allowed,
|
||||
* otherwise false.
|
||||
* @method PolicyService#allow
|
||||
*/
|
||||
|
||||
/**
|
||||
* Provides an implementation of `policyService` which consults
|
||||
* various policy extensions to determine whether or not a specific
|
||||
* decision should be allowed.
|
||||
* @memberof platform/policy
|
||||
* @constructor
|
||||
* @implements {PolicyService}
|
||||
* @param {Policy[]} policies the policies to enforce
|
||||
*/
|
||||
function PolicyProvider(policies) {
|
||||
var policyMap = {};
|
||||
@ -87,49 +112,33 @@ define(
|
||||
|
||||
// Populate the map for subsequent lookup
|
||||
policies.forEach(addToMap);
|
||||
|
||||
return {
|
||||
/**
|
||||
* Check whether or not a certain decision is allowed by
|
||||
* policy.
|
||||
* @param {string} category a machine-readable identifier
|
||||
* for the kind of decision being made
|
||||
* @param candidate the object about which the decision is
|
||||
* being made
|
||||
* @param context the context in which the decision occurs
|
||||
* @param {Function} [callback] callback to invoke with a
|
||||
* string message describing the reason a decision
|
||||
* was disallowed (if its disallowed)
|
||||
* @returns {boolean} true if the decision is allowed,
|
||||
* otherwise false.
|
||||
* @memberof platform/policy.PolicyProvider#
|
||||
*/
|
||||
allow: function (category, candidate, context, callback) {
|
||||
var policyList = policyMap[category] || [],
|
||||
i;
|
||||
|
||||
// Iterate through policies. We do this instead of map or
|
||||
// forEach so that we can return immediately if a policy
|
||||
// chooses to disallow this decision.
|
||||
for (i = 0; i < policyList.length; i += 1) {
|
||||
// Consult the policy...
|
||||
if (!policyList[i].allow(candidate, context)) {
|
||||
// ...it disallowed, so pass its message to
|
||||
// the callback (if any)
|
||||
if (callback) {
|
||||
callback(policyList[i].message);
|
||||
}
|
||||
// And return the failed result.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// No policy disallowed this decision.
|
||||
return true;
|
||||
}
|
||||
};
|
||||
this.policyMap = policyMap;
|
||||
}
|
||||
|
||||
PolicyProvider.prototype.allow = function (category, candidate, context, callback) {
|
||||
var policyList = this.policyMap[category] || [],
|
||||
i;
|
||||
|
||||
// Iterate through policies. We do this instead of map or
|
||||
// forEach so that we can return immediately if a policy
|
||||
// chooses to disallow this decision.
|
||||
for (i = 0; i < policyList.length; i += 1) {
|
||||
// Consult the policy...
|
||||
if (!policyList[i].allow(candidate, context)) {
|
||||
// ...it disallowed, so pass its message to
|
||||
// the callback (if any)
|
||||
if (callback) {
|
||||
callback(policyList[i].message);
|
||||
}
|
||||
// And return the failed result.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// No policy disallowed this decision.
|
||||
return true;
|
||||
};
|
||||
|
||||
return PolicyProvider;
|
||||
}
|
||||
);
|
||||
|
Reference in New Issue
Block a user