Skip to content

Commit

Permalink
[ACA-2259] Edit in Microsoft Office - check update permissions (#1015)
Browse files Browse the repository at this point in the history
* [ACA-2259] Edit in Microsoft Office - check update permissions

* [ACA-2259] check update permissions - unit tests

* [ACA-2259] refactor - check allowableOperationsOnTarget from service

* [ACA-2259] add back check only on target for SharedFiles

* [ACA-2259] SharedLinks are not folders

* type-safe api for node permissions

* workaround for shared files

* use hasOwnProperty function
  • Loading branch information
suzanadirla authored and DenysVuika committed Mar 14, 2019
1 parent 07f45e0 commit 88e74b1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 40 deletions.
22 changes: 22 additions & 0 deletions projects/adf-office-services-ext/src/lib/evaluators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,25 @@ 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 return [true] if all checks succeed', () => {
const context: any = {
selection: {
Expand All @@ -199,6 +218,9 @@ describe('evaluators', () => {
properties: {}
}
}
},
permissions: {
check: () => true
}
};

Expand Down
21 changes: 19 additions & 2 deletions projects/adf-office-services-ext/src/lib/evaluators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.hasOwnProperty('allowableOperationsOnTarget')) {
return context.permissions.check(file, ['update'], {
target: 'allowableOperationsOnTarget'
});
}
}

if (!file.entry.properties) {
return false;
}

Expand Down Expand Up @@ -63,5 +80,5 @@ export function canOpenWithOffice(
return false;
}

return true;
return context.permissions.check(file, ['update']);
}
6 changes: 0 additions & 6 deletions src/app/extensions/evaluators/app.evaluators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,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;
Expand Down
71 changes: 47 additions & 24 deletions src/app/services/node-permission.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,56 @@

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'
})
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
Expand All @@ -77,21 +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.allowableOperations) {
return entry.allowableOperations;
if ('entry' in node) {
entry = node.entry;
} else {
entry = node;
}

if (target && entry[target]) {
return entry[target];
if (property) {
return entry[property] || [];
}

return [];
}

private sanitize(selection): any[] {
return (selection || []).filter(item => item);
if ('allowableOperationsOnTarget' in entry) {
return entry.allowableOperationsOnTarget || [];
} else {
return entry.allowableOperations || [];
}
}
}
9 changes: 1 addition & 8 deletions src/app/store/reducers/app.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 88e74b1

Please sign in to comment.