[Code Style] Use prototypes in policy bundle

WTD-1482.
This commit is contained in:
Victor Woeltjen 2015-08-14 15:49:45 -07:00
parent f8cb3f464c
commit 07a2065c11
3 changed files with 82 additions and 81 deletions

View File

@ -33,29 +33,25 @@ define(
* @param {ActionService} actionService the service to decorate
* @constructor
* @memberof platform/policy
* @implements {ActionService}
*/
function PolicyActionDecorator(policyService, actionService) {
return {
/**
* Get actions which are applicable in this context.
* These will be filtered to remove any actions which
* are deemed inapplicable by policy.
* @param context the context in which the action will occur
* @returns {Action[]} applicable actions
* @memberof platform/policy.PolicyActionDecorator#
*/
getActions: function (context) {
// Check if an action is allowed by policy.
function allow(action) {
return policyService.allow('action', action, context);
}
// Look up actions, filter out the disallowed ones.
return actionService.getActions(context).filter(allow);
}
};
this.policyService = policyService;
this.actionService = actionService;
}
PolicyActionDecorator.prototype.getActions = function (context) {
var policyService = this.policyService;
// Check if an action is allowed by policy.
function allow(action) {
return policyService.allow('action', action, context);
}
// Look up actions, filter out the disallowed ones.
return this.actionService.getActions(context).filter(allow);
};
return PolicyActionDecorator;
}
);

View File

@ -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;
}
);

View File

@ -33,29 +33,25 @@ define(
* @param {ViewService} viewService the service to decorate
* @constructor
* @memberof platform/policy
* @implements {ViewService}
*/
function PolicyActionDecorator(policyService, viewService) {
return {
/**
* Get views which are applicable to this domain object.
* These will be filtered to remove any views which
* are deemed inapplicable by policy.
* @param {DomainObject} the domain object to view
* @returns {View[]} applicable views
* @memberof platform/policy.PolicyViewDecorator#
*/
getViews: function (domainObject) {
// Check if an action is allowed by policy.
function allow(view) {
return policyService.allow('view', view, domainObject);
}
// Look up actions, filter out the disallowed ones.
return viewService.getViews(domainObject).filter(allow);
}
};
function PolicyViewDecorator(policyService, viewService) {
this.policyService = policyService;
this.viewService = viewService;
}
return PolicyActionDecorator;
PolicyViewDecorator.prototype.getViews = function (domainObject) {
var policyService = this.policyService;
// Check if an action is allowed by policy.
function allow(view) {
return policyService.allow('view', view, domainObject);
}
// Look up actions, filter out the disallowed ones.
return this.viewService.getViews(domainObject).filter(allow);
};
return PolicyViewDecorator;
}
);