From 060669c2b3dde4627061f8e31f56c4d3ef182bc4 Mon Sep 17 00:00:00 2001 From: suzana_dirla Date: Tue, 12 Mar 2019 16:48:13 +0200 Subject: [PATCH 1/8] [ACA-2259] Edit in Microsoft Office - check update permissions --- projects/adf-office-services-ext/src/lib/evaluators.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/projects/adf-office-services-ext/src/lib/evaluators.ts b/projects/adf-office-services-ext/src/lib/evaluators.ts index 9453ae1fc5..b49d1bbedd 100644 --- a/projects/adf-office-services-ext/src/lib/evaluators.ts +++ b/projects/adf-office-services-ext/src/lib/evaluators.ts @@ -63,5 +63,11 @@ export function canOpenWithOffice( return false; } - return true; + if (file.entry.hasOwnProperty('allowableOperationsOnTarget')) { + return context.permissions.check(file, ['update'], { + target: 'allowableOperationsOnTarget' + }); + } + + return context.permissions.check(file, ['update']); } From b07e036cacd334fcc780b4361cf954c4c723a10a Mon Sep 17 00:00:00 2001 From: suzana_dirla Date: Wed, 13 Mar 2019 11:06:56 +0200 Subject: [PATCH 2/8] [ACA-2259] check update permissions - unit tests --- .../src/lib/evaluators.spec.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/projects/adf-office-services-ext/src/lib/evaluators.spec.ts b/projects/adf-office-services-ext/src/lib/evaluators.spec.ts index 66e5c97da9..bd18623e91 100644 --- a/projects/adf-office-services-ext/src/lib/evaluators.spec.ts +++ b/projects/adf-office-services-ext/src/lib/evaluators.spec.ts @@ -189,6 +189,50 @@ describe('evaluators', () => { expect(canOpenWithOffice(context)).toBeFalsy(); }); + it('should return [false] if permissions check is false', () => { + const context: any = { + selection: { + file: { + entry: { + name: 'document.docx', + isLocked: false, + properties: {} + } + } + }, + permissions: { + check: () => false + } + }; + + expect(canOpenWithOffice(context)).toBeFalsy(); + }); + + it('should check the [update] permission when selected file has allowableOperationsOnTarget', () => { + let checkOnTarget = ''; + const context: any = { + selection: { + file: { + entry: { + name: 'document.docx', + isLocked: false, + properties: {}, + allowableOperationsOnTarget: {} + } + } + }, + permissions: { + check: (source: any, permissions: string[], options?: any) => { + checkOnTarget = options && options.target; + return true; + } + } + }; + + expect(canOpenWithOffice(context)).toBeTruthy(); + expect(checkOnTarget).toEqual('allowableOperationsOnTarget'); + }); + it('should return [true] if all checks succeed', () => { const context: any = { selection: { @@ -199,6 +243,9 @@ describe('evaluators', () => { properties: {} } } + }, + permissions: { + check: () => true } }; From 25a13b0b73cf941e9d83d57106f2a1e174777b8e Mon Sep 17 00:00:00 2001 From: suzana_dirla Date: Wed, 13 Mar 2019 16:17:03 +0200 Subject: [PATCH 3/8] [ACA-2259] refactor - check allowableOperationsOnTarget from service --- .../src/lib/evaluators.spec.ts | 25 ------------------- .../src/lib/evaluators.ts | 6 ----- .../extensions/evaluators/app.evaluators.ts | 13 ---------- src/app/services/node-permission.service.ts | 4 +++ 4 files changed, 4 insertions(+), 44 deletions(-) diff --git a/projects/adf-office-services-ext/src/lib/evaluators.spec.ts b/projects/adf-office-services-ext/src/lib/evaluators.spec.ts index bd18623e91..e38843a5f7 100644 --- a/projects/adf-office-services-ext/src/lib/evaluators.spec.ts +++ b/projects/adf-office-services-ext/src/lib/evaluators.spec.ts @@ -208,31 +208,6 @@ describe('evaluators', () => { expect(canOpenWithOffice(context)).toBeFalsy(); }); - it('should check the [update] permission when selected file has allowableOperationsOnTarget', () => { - let checkOnTarget = ''; - const context: any = { - selection: { - file: { - entry: { - name: 'document.docx', - isLocked: false, - properties: {}, - allowableOperationsOnTarget: {} - } - } - }, - permissions: { - check: (source: any, permissions: string[], options?: any) => { - checkOnTarget = options && options.target; - return true; - } - } - }; - - expect(canOpenWithOffice(context)).toBeTruthy(); - expect(checkOnTarget).toEqual('allowableOperationsOnTarget'); - }); - it('should return [true] if all checks succeed', () => { const context: any = { selection: { diff --git a/projects/adf-office-services-ext/src/lib/evaluators.ts b/projects/adf-office-services-ext/src/lib/evaluators.ts index b49d1bbedd..910b0e713e 100644 --- a/projects/adf-office-services-ext/src/lib/evaluators.ts +++ b/projects/adf-office-services-ext/src/lib/evaluators.ts @@ -63,11 +63,5 @@ export function canOpenWithOffice( return false; } - if (file.entry.hasOwnProperty('allowableOperationsOnTarget')) { - return context.permissions.check(file, ['update'], { - target: 'allowableOperationsOnTarget' - }); - } - return context.permissions.check(file, ['update']); } diff --git a/src/app/extensions/evaluators/app.evaluators.ts b/src/app/extensions/evaluators/app.evaluators.ts index 2a65a530f9..40e3763afc 100644 --- a/src/app/extensions/evaluators/app.evaluators.ts +++ b/src/app/extensions/evaluators/app.evaluators.ts @@ -141,13 +141,6 @@ export function canDeleteSelection( return context.permissions.check(context.selection.nodes, ['delete']); } - // workaround for Shared Files - if (isSharedFiles(context, ...args)) { - return context.permissions.check(context.selection.nodes, ['delete'], { - target: 'allowableOperationsOnTarget' - }); - } - return context.permissions.check(context.selection.nodes, ['delete']); } return false; @@ -321,12 +314,6 @@ export function canUpdateSelectedNode( return false; } - if (node.entry.hasOwnProperty('allowableOperationsOnTarget')) { - return context.permissions.check(node, ['update'], { - target: 'allowableOperationsOnTarget' - }); - } - return context.permissions.check(node, ['update']); } return false; diff --git a/src/app/services/node-permission.service.ts b/src/app/services/node-permission.service.ts index c6e2c09e53..694f82a671 100644 --- a/src/app/services/node-permission.service.ts +++ b/src/app/services/node-permission.service.ts @@ -80,6 +80,10 @@ export class NodePermissionService implements NodePermissions { private getAllowableOperations(node, target): string[] { const entry = node.entry || node; + if (!target && entry.allowableOperationsOnTarget) { + return entry.allowableOperationsOnTarget; + } + if (!target && entry.allowableOperations) { return entry.allowableOperations; } From 0723c3d885ea361f8e5f61f2300308bddd563567 Mon Sep 17 00:00:00 2001 From: suzana_dirla Date: Thu, 14 Mar 2019 06:41:42 +0200 Subject: [PATCH 4/8] [ACA-2259] add back check only on target for SharedFiles --- src/app/extensions/evaluators/app.evaluators.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/app/extensions/evaluators/app.evaluators.ts b/src/app/extensions/evaluators/app.evaluators.ts index 40e3763afc..a939f07539 100644 --- a/src/app/extensions/evaluators/app.evaluators.ts +++ b/src/app/extensions/evaluators/app.evaluators.ts @@ -141,6 +141,13 @@ export function canDeleteSelection( return context.permissions.check(context.selection.nodes, ['delete']); } + // workaround for Shared Files + if (isSharedFiles(context, ...args)) { + return context.permissions.check(context.selection.nodes, ['delete'], { + target: 'allowableOperationsOnTarget' + }); + } + return context.permissions.check(context.selection.nodes, ['delete']); } return false; From de1eab15a0861bcee09ffcf7f4339c740fa128fd Mon Sep 17 00:00:00 2001 From: suzana_dirla Date: Thu, 14 Mar 2019 09:41:00 +0200 Subject: [PATCH 5/8] [ACA-2259] SharedLinks are not folders --- src/app/store/reducers/app.reducer.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/app/store/reducers/app.reducer.ts b/src/app/store/reducers/app.reducer.ts index 7b7e3a23eb..935b7ee86a 100644 --- a/src/app/store/reducers/app.reducer.ts +++ b/src/app/store/reducers/app.reducer.ts @@ -227,14 +227,7 @@ function updateSelectedNodes( ? true : false; }); - folder = nodes.find((entity: any) => - // workaround Shared - entity.entry.isFolder || - entity.entry.nodeId || - entity.entry.sharedByUser - ? true - : false - ); + folder = nodes.find((entity: any) => entity.entry.isFolder); } } From 475b98adc83b879c5772c5638b32ace86e3409c7 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Thu, 14 Mar 2019 12:01:54 +0000 Subject: [PATCH 6/8] type-safe api for node permissions --- src/app/services/node-permission.service.ts | 73 +++++++++++++-------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/src/app/services/node-permission.service.ts b/src/app/services/node-permission.service.ts index 694f82a671..0340cd4e6e 100644 --- a/src/app/services/node-permission.service.ts +++ b/src/app/services/node-permission.service.ts @@ -25,6 +25,14 @@ import { Injectable } from '@angular/core'; import { NodePermissions } from '@alfresco/adf-extensions'; +import { Node, SharedLink, SharedLinkEntry, NodeEntry } from '@alfresco/js-api'; + +export type PermissionSource = NodeEntry | SharedLinkEntry | Node; + +export interface PermissionOptions { + target?: string; + operation?: string; +} @Injectable({ providedIn: 'root' @@ -32,31 +40,41 @@ import { NodePermissions } from '@alfresco/adf-extensions'; export class NodePermissionService implements NodePermissions { static DEFAULT_OPERATION = 'OR'; - private defaultOptions = { + private defaultOptions: PermissionOptions = { operation: NodePermissionService.DEFAULT_OPERATION, target: null }; - check(source: any, permissions: string[], options?: any): boolean { + check( + source: PermissionSource | PermissionSource[], + permissions: string[], + options?: PermissionOptions + ): boolean { const opts = Object.assign({}, this.defaultOptions, options || {}); - if (source) { - if (Array.isArray(source) && source.length) { - const arr = this.sanitize(source); + if (!source) { + return false; + } + + if (Array.isArray(source)) { + source = source.filter(item => item); - return ( - !!arr.length && - source.every(node => this.hasPermission(node, permissions, opts)) + if (source.length > 0) { + return source.every(node => + this.isOperationAllowed(node, permissions, opts) ); } - - return this.hasPermission(source, permissions, opts); + return false; + } else { + return this.isOperationAllowed(source, permissions, opts); } - - return false; } - private hasPermission(node, permissions, options): boolean { + private isOperationAllowed( + node: PermissionSource, + permissions: string[], + options: PermissionOptions + ): boolean { const allowableOperations = this.getAllowableOperations( node, options.target @@ -77,25 +95,26 @@ export class NodePermissionService implements NodePermissions { return false; } - private getAllowableOperations(node, target): string[] { - const entry = node.entry || node; + private getAllowableOperations( + node: PermissionSource, + property?: string + ): string[] { + let entry: Node | SharedLink; - if (!target && entry.allowableOperationsOnTarget) { - return entry.allowableOperationsOnTarget; + if ('entry' in node) { + entry = node.entry; + } else { + entry = node; } - if (!target && entry.allowableOperations) { - return entry.allowableOperations; + if (property) { + return entry[property] || []; } - if (target && entry[target]) { - return entry[target]; + if ('allowableOperationsOnTarget' in entry) { + return entry.allowableOperationsOnTarget || []; + } else { + return entry.allowableOperations || []; } - - return []; - } - - private sanitize(selection): any[] { - return (selection || []).filter(item => item); } } From 8ba63a708763feb00082265a0d3ec2dc6604d9d6 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Thu, 14 Mar 2019 13:51:23 +0000 Subject: [PATCH 7/8] workaround for shared files --- .../src/lib/evaluators.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/projects/adf-office-services-ext/src/lib/evaluators.ts b/projects/adf-office-services-ext/src/lib/evaluators.ts index 910b0e713e..ad4d41c5ac 100644 --- a/projects/adf-office-services-ext/src/lib/evaluators.ts +++ b/projects/adf-office-services-ext/src/lib/evaluators.ts @@ -26,7 +26,24 @@ export function canOpenWithOffice( const { file } = context.selection; - if (!file || !file.entry || !file.entry.properties) { + if (!file || !file.entry) { + return false; + } + + // workaround for Shared files + if ( + context.navigation && + context.navigation.url && + context.navigation.url.startsWith('/shared') + ) { + if (file.entry['allowableOperationsOnTarget']) { + return context.permissions.check(file, ['update'], { + target: 'allowableOperationsOnTarget' + }); + } + } + + if (!file.entry.properties) { return false; } From 8d29095fcd66e11c298ef9ae2cab1cb2541603f7 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Thu, 14 Mar 2019 13:58:23 +0000 Subject: [PATCH 8/8] use hasOwnProperty function --- projects/adf-office-services-ext/src/lib/evaluators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/adf-office-services-ext/src/lib/evaluators.ts b/projects/adf-office-services-ext/src/lib/evaluators.ts index ad4d41c5ac..3a8dd0dcea 100644 --- a/projects/adf-office-services-ext/src/lib/evaluators.ts +++ b/projects/adf-office-services-ext/src/lib/evaluators.ts @@ -36,7 +36,7 @@ export function canOpenWithOffice( context.navigation.url && context.navigation.url.startsWith('/shared') ) { - if (file.entry['allowableOperationsOnTarget']) { + if (file.entry.hasOwnProperty('allowableOperationsOnTarget')) { return context.permissions.check(file, ['update'], { target: 'allowableOperationsOnTarget' });