[Policy] Implement view decorator

Implement policy-driven view decorator, sufficient to satisfy
specs. WTD-1062.
This commit is contained in:
Victor Woeltjen 2015-04-01 15:58:19 -07:00
parent 7915074b10
commit 892e2c9dd4
4 changed files with 45 additions and 2 deletions

View File

@ -10,6 +10,12 @@
"implementation": "PolicyActionDecorator.js",
"depends": [ "policyService" ]
},
{
"type": "decorator",
"provides": "viewService",
"implementation": "PolicyViewDecorator.js",
"depends": [ "policyService" ]
},
{
"type": "provider",
"provides": "policyService",

View File

@ -15,7 +15,7 @@ define(
return {
/**
* Get actions which are applicable in this context.
* These will be filters to remove any actions which
* 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

View File

@ -0,0 +1,37 @@
/*global define*/
define(
[],
function () {
"use strict";
/**
* Filters out views based on policy.
* @param {PolicyService} policyService the service which provides
* policy decisions
* @param {ViewService} viewService the service to decorate
*/
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
*/
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);
}
};
}
return PolicyActionDecorator;
}
);

View File

@ -54,7 +54,7 @@ define(
it("provides views from its decorated view service", function () {
// Mock policy service allows everything by default,
// so everything should be returned
expect(decorator.getActions(mockDomainObject))
expect(decorator.getViews(mockDomainObject))
.toEqual(testViews);
});