diff --git a/platform/policy/bundle.json b/platform/policy/bundle.json index ea836ca732..0f27b51136 100644 --- a/platform/policy/bundle.json +++ b/platform/policy/bundle.json @@ -10,6 +10,12 @@ "implementation": "PolicyActionDecorator.js", "depends": [ "policyService" ] }, + { + "type": "decorator", + "provides": "viewService", + "implementation": "PolicyViewDecorator.js", + "depends": [ "policyService" ] + }, { "type": "provider", "provides": "policyService", diff --git a/platform/policy/src/PolicyActionDecorator.js b/platform/policy/src/PolicyActionDecorator.js index 1057dca905..08cadd5423 100644 --- a/platform/policy/src/PolicyActionDecorator.js +++ b/platform/policy/src/PolicyActionDecorator.js @@ -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 diff --git a/platform/policy/src/PolicyViewDecorator.js b/platform/policy/src/PolicyViewDecorator.js new file mode 100644 index 0000000000..e236ba6066 --- /dev/null +++ b/platform/policy/src/PolicyViewDecorator.js @@ -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; + } +); \ No newline at end of file diff --git a/platform/policy/test/PolicyViewDecoratorSpec.js b/platform/policy/test/PolicyViewDecoratorSpec.js index 60007fb73b..3931a23507 100644 --- a/platform/policy/test/PolicyViewDecoratorSpec.js +++ b/platform/policy/test/PolicyViewDecoratorSpec.js @@ -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); });