From d5d66f0001ee1b56614b607dd077a6ddc3c2fda3 Mon Sep 17 00:00:00 2001 From: Cilibiu Bogdan Date: Fri, 13 Apr 2018 09:27:46 +0300 Subject: [PATCH] declare permission property to check from of node (#302) --- .../services/node-permission.service.spec.ts | 18 ++++++------ .../services/node-permission.service.ts | 29 ++++++++++++------- .../shared-files/shared-files.component.html | 4 +-- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/app/common/services/node-permission.service.spec.ts b/src/app/common/services/node-permission.service.spec.ts index 50752ae6b..630ef4a52 100644 --- a/src/app/common/services/node-permission.service.spec.ts +++ b/src/app/common/services/node-permission.service.spec.ts @@ -57,7 +57,7 @@ describe('NodePermissionService', () => { { entry: { allowableOperationsOnTarget: ['update'] } } ]; - expect(permission.check(source, ['update'])).toBe(true); + expect(permission.check(source, ['update'], { target: 'allowableOperationsOnTarget' })).toBe(true); }); it('should return false when source does not have allowableOperations permission', () => { @@ -67,7 +67,7 @@ describe('NodePermissionService', () => { { entry: { allowableOperations: ['delete'] } } ]; - expect(permission.check(source, ['update'])).toBe(false); + expect(permission.check(source, ['update'], { target: 'allowableOperationsOnTarget' })).toBe(false); }); it('should return false when source does not have allowableOperationsOnTarget permission', () => { @@ -77,7 +77,7 @@ describe('NodePermissionService', () => { { entry: { allowableOperationsOnTarget: ['delete'] } } ]; - expect(permission.check(source, ['update'])).toBe(false); + expect(permission.check(source, ['update'], { target: 'allowableOperationsOnTarget' })).toBe(false); }); it('should return true when source has `OR` allowableOperations permission', () => { @@ -97,7 +97,7 @@ describe('NodePermissionService', () => { { entry: { allowableOperations: ['update', 'updatePermissions', 'other'] } } ]; - expect(permission.check(source, ['update', 'other'], 'AND')).toBe(true); + expect(permission.check(source, ['update', 'other'], { operation: 'AND' })).toBe(true); }); it('should return false when source has no `AND` allowableOperations permission', () => { @@ -107,7 +107,7 @@ describe('NodePermissionService', () => { { entry: { allowableOperations: ['update', 'updatePermissions', 'other'] } } ]; - expect(permission.check(source, ['update', 'bogus'], 'AND')).toBe(false); + expect(permission.check(source, ['update', 'bogus'], { operation: 'AND' })).toBe(false); }); it('should return false when source has no allowableOperations', () => { @@ -142,7 +142,7 @@ describe('NodePermissionService', () => { it('should return true when source has allowableOperationsOnTarget permission', () => { const source = { entry: { allowableOperationsOnTarget: ['update'] } }; - expect(permission.check(source, ['update'])).toBe(true); + expect(permission.check(source, ['update'], { target: 'allowableOperationsOnTarget' })).toBe(true); }); it('should return false when source does not have allowableOperations permission', () => { @@ -154,7 +154,7 @@ describe('NodePermissionService', () => { it('should return false when source does not have allowableOperationsOnTarget permission', () => { const source = { entry: { allowableOperationsOnTarget: ['delete'] } }; - expect(permission.check(source, ['update'])).toBe(false); + expect(permission.check(source, ['update'], { target: 'allowableOperationsOnTarget' })).toBe(false); }); it('should return true when source has `OR` allowableOperations permission', () => { @@ -166,13 +166,13 @@ describe('NodePermissionService', () => { it('should return true when source has `AND` allowableOperations permission', () => { const source = { entry: { allowableOperations: ['update', 'other'] } }; - expect(permission.check(source, ['update', 'other'], 'AND')).toBe(true); + expect(permission.check(source, ['update', 'other'], { operation: 'AND' })).toBe(true); }); it('should return false when source has no `AND` allowableOperations permission', () => { const source = { entry: { allowableOperations: ['update', 'updatePermissions', 'other'] } }; - expect(permission.check(source, ['update', 'bogus'], 'AND')).toBe(false); + expect(permission.check(source, ['update', 'bogus'], { operation: 'AND' })).toBe(false); }); it('should return false when source has no allowableOperations', () => { diff --git a/src/app/common/services/node-permission.service.ts b/src/app/common/services/node-permission.service.ts index 9c8089745..3ef8ad765 100644 --- a/src/app/common/services/node-permission.service.ts +++ b/src/app/common/services/node-permission.service.ts @@ -29,25 +29,32 @@ import { Injectable } from '@angular/core'; export class NodePermissionService { static DEFAULT_OPERATION = 'OR'; - check(source: any, permissions: string[], operation: string = NodePermissionService.DEFAULT_OPERATION): boolean { + private defaultOptions = { + operation: NodePermissionService.DEFAULT_OPERATION, + target: null + }; + + check(source: any, permissions: string[], options: any = {}): boolean { + const opts = Object.assign({}, this.defaultOptions, options); + if (source) { if (Array.isArray(source) && source.length) { const arr = this.sanitize(source); - return !!arr.length && source.every(node => this.hasPermission(node, permissions, operation)); + return !!arr.length && source.every(node => this.hasPermission(node, permissions, opts)); } - return this.hasPermission(source, permissions, operation); + return this.hasPermission(source, permissions, opts); } return false; } - private hasPermission(node, permissions, operation): boolean { - const allowableOperations = this.getAllowableOperations(node); + private hasPermission(node, permissions, options): boolean { + const allowableOperations = this.getAllowableOperations(node, options.target); if (allowableOperations.length) { - if (operation === NodePermissionService.DEFAULT_OPERATION) { + if (options.operation === NodePermissionService.DEFAULT_OPERATION) { return permissions.some(permission => allowableOperations.includes(permission)); } else { return permissions.every(permission => allowableOperations.includes(permission)); @@ -57,15 +64,15 @@ export class NodePermissionService { return false; } - private getAllowableOperations(node): string[] { + private getAllowableOperations(node, target): string[] { const entry = node.entry || node; - if (entry.allowableOperationsOnTarget) { - return entry.allowableOperationsOnTarget; + if (!target && entry.allowableOperations) { + return entry.allowableOperations; } - if (entry.allowableOperations) { - return entry.allowableOperations; + if (target && entry[target]) { + return entry[target]; } return []; diff --git a/src/app/components/shared-files/shared-files.component.html b/src/app/components/shared-files/shared-files.component.html index 7ae0ebeac..918aa2fc9 100644 --- a/src/app/components/shared-files/shared-files.component.html +++ b/src/app/components/shared-files/shared-files.component.html @@ -64,7 +64,7 @@