declare permission property to check from of node (#302)

This commit is contained in:
Cilibiu Bogdan
2018-04-13 09:27:46 +03:00
committed by Denys Vuika
parent 31ad535348
commit d5d66f0001
3 changed files with 29 additions and 22 deletions

View File

@@ -57,7 +57,7 @@ describe('NodePermissionService', () => {
{ entry: { allowableOperationsOnTarget: ['update'] } } { 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', () => { it('should return false when source does not have allowableOperations permission', () => {
@@ -67,7 +67,7 @@ describe('NodePermissionService', () => {
{ entry: { allowableOperations: ['delete'] } } { 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', () => { it('should return false when source does not have allowableOperationsOnTarget permission', () => {
@@ -77,7 +77,7 @@ describe('NodePermissionService', () => {
{ entry: { allowableOperationsOnTarget: ['delete'] } } { 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', () => { it('should return true when source has `OR` allowableOperations permission', () => {
@@ -97,7 +97,7 @@ describe('NodePermissionService', () => {
{ entry: { allowableOperations: ['update', 'updatePermissions', 'other'] } } { 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', () => { it('should return false when source has no `AND` allowableOperations permission', () => {
@@ -107,7 +107,7 @@ describe('NodePermissionService', () => {
{ entry: { allowableOperations: ['update', 'updatePermissions', 'other'] } } { 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', () => { it('should return false when source has no allowableOperations', () => {
@@ -142,7 +142,7 @@ describe('NodePermissionService', () => {
it('should return true when source has allowableOperationsOnTarget permission', () => { it('should return true when source has allowableOperationsOnTarget permission', () => {
const source = { entry: { allowableOperationsOnTarget: ['update'] } }; 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', () => { 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', () => { it('should return false when source does not have allowableOperationsOnTarget permission', () => {
const source = { entry: { allowableOperationsOnTarget: ['delete'] } }; 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', () => { 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', () => { it('should return true when source has `AND` allowableOperations permission', () => {
const source = { entry: { allowableOperations: ['update', 'other'] } }; 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', () => { it('should return false when source has no `AND` allowableOperations permission', () => {
const source = { entry: { allowableOperations: ['update', 'updatePermissions', 'other'] } }; 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', () => { it('should return false when source has no allowableOperations', () => {

View File

@@ -29,25 +29,32 @@ import { Injectable } from '@angular/core';
export class NodePermissionService { export class NodePermissionService {
static DEFAULT_OPERATION = 'OR'; 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 (source) {
if (Array.isArray(source) && source.length) { if (Array.isArray(source) && source.length) {
const arr = this.sanitize(source); 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; return false;
} }
private hasPermission(node, permissions, operation): boolean { private hasPermission(node, permissions, options): boolean {
const allowableOperations = this.getAllowableOperations(node); const allowableOperations = this.getAllowableOperations(node, options.target);
if (allowableOperations.length) { if (allowableOperations.length) {
if (operation === NodePermissionService.DEFAULT_OPERATION) { if (options.operation === NodePermissionService.DEFAULT_OPERATION) {
return permissions.some(permission => allowableOperations.includes(permission)); return permissions.some(permission => allowableOperations.includes(permission));
} else { } else {
return permissions.every(permission => allowableOperations.includes(permission)); return permissions.every(permission => allowableOperations.includes(permission));
@@ -57,15 +64,15 @@ export class NodePermissionService {
return false; return false;
} }
private getAllowableOperations(node): string[] { private getAllowableOperations(node, target): string[] {
const entry = node.entry || node; const entry = node.entry || node;
if (entry.allowableOperationsOnTarget) { if (!target && entry.allowableOperations) {
return entry.allowableOperationsOnTarget; return entry.allowableOperations;
} }
if (entry.allowableOperations) { if (target && entry[target]) {
return entry.allowableOperations; return entry[target];
} }
return []; return [];

View File

@@ -64,7 +64,7 @@
<button <button
mat-menu-item mat-menu-item
*ngIf="permission.check(documentList.selection, ['delete'])" *ngIf="permission.check(documentList.selection, ['delete'], { target: 'allowableOperationsOnTarget' })"
[app-move-node]="documentList.selection"> [app-move-node]="documentList.selection">
<mat-icon color="primary">library_books</mat-icon> <mat-icon color="primary">library_books</mat-icon>
<span>{{ 'APP.ACTIONS.MOVE' | translate }}</span> <span>{{ 'APP.ACTIONS.MOVE' | translate }}</span>
@@ -81,7 +81,7 @@
<button <button
mat-menu-item mat-menu-item
*ngIf="permission.check(documentList.selection, ['delete'])" *ngIf="permission.check(documentList.selection, ['delete'], { target: 'allowableOperationsOnTarget' })"
[app-delete-node]="documentList.selection"> [app-delete-node]="documentList.selection">
<mat-icon color="primary">delete</mat-icon> <mat-icon color="primary">delete</mat-icon>
<span>{{ 'APP.ACTIONS.DELETE' | translate }}</span> <span>{{ 'APP.ACTIONS.DELETE' | translate }}</span>