DocumentList - Check permissions on delete folder/file (#1808)

* Add check permission on delete folder/file

* Provide a way to disable the action when if there is no permission

* Improve the code using the external permission inside the folder/document action service

* Add basic documentation.
 - How disable the button when the permission is missing
 - How to show a notification message when the permission is missing

* Resize the image

* Change the value to true for demo purpose

* Update folder-actions.service.ts

* Update document-actions.service.ts
This commit is contained in:
Maurizio Vitale
2017-04-11 13:43:33 +01:00
committed by Mario Romano
parent ab3d18e5c1
commit f6102dfc07
20 changed files with 460 additions and 32 deletions

View File

@@ -34,14 +34,14 @@
[class.alfresco-datatable__row--selected]="selectedRow === row"
[adf-upload]="allowDropFiles" [adf-upload-data]="row">
<!-- Actions (right) -->
<!-- Actions (left) -->
<td *ngIf="actions && actionsPosition === 'left'" class="alfresco-datatable__actions-cell">
<button [id]="'action_menu_' + idx" alfresco-mdl-button class="mdl-button--icon" [attr.data-automation-id]="actions_menu">
<i class="material-icons">more_vert</i>
</button>
<ul alfresco-mdl-menu class="mdl-menu--bottom-left"
[attr.for]="'action_menu_' + idx">
<li class="mdl-menu__item"
<li class="mdl-menu__item" [attr.disabled]="action.disabled"
[attr.data-automation-id]="action.title"
*ngFor="let action of getRowActions(row)"
(click)="onExecuteRowAction(row, action)">
@@ -90,7 +90,7 @@
</button>
<ul alfresco-mdl-menu class="mdl-menu--bottom-right"
[attr.for]="'action_menu_' + idx">
<li class="mdl-menu__item"
<li class="mdl-menu__item" [attr.disabled]="action.disabled"
[attr.data-automation-id]="action.title"
*ngFor="let action of getRowActions(row)"
(click)="onExecuteRowAction(row, action)">

View File

@@ -414,4 +414,87 @@ describe('DataTable', () => {
dataTable.onImageLoadingError(event);
expect(event.target.src).toBe(originalSrc);
});
it('should disable the action if there is no permission and disableWithNoPermission true', () => {
dataTable.data = new ObjectDataTableAdapter(
[{id: 1, name: 'xyz', allowableOperations: ['create', 'update']}],
[]
);
let row = dataTable.data.getRows();
let actions = [
{
disableWithNoPermission: true,
permission: 'delete',
target: 'folder',
title: 'action2'
}
];
let updateActions = dataTable.checkPermissions(row[0], actions);
expect(updateActions[0].disabled).toBe(true);
});
it('should not disable the action if there is no permission and disableWithNoPermission false', () => {
dataTable.data = new ObjectDataTableAdapter(
[{id: 1, name: 'xyz', allowableOperations: ['create', 'update']}],
[]
);
let row = dataTable.data.getRows();
let actions = [
{
disableWithNoPermission: false,
permission: 'delete',
target: 'folder',
title: 'action2'
}
];
let updateActions = dataTable.checkPermissions(row[0], actions);
expect(updateActions[0].disabled).toBeUndefined();
});
it('should not disable the action if there is the right permission', () => {
dataTable.data = new ObjectDataTableAdapter(
[{ id: 1, name: 'xyz', allowableOperations: ['create', 'update', 'delete'] }],
[]
);
let row = dataTable.data.getRows();
let actions = [
{
permission: 'delete',
target: 'folder',
title: 'action2'
}
];
let updateActions = dataTable.checkPermissions(row[0], actions);
expect(updateActions[0].disabled).toBeUndefined();
});
it('should not disable the action if there are no permissions', () => {
dataTable.data = new ObjectDataTableAdapter(
[{id: 1, name: 'xyz', allowableOperations: null}],
[]
);
let row = dataTable.data.getRows();
let actions = [
{
permission: 'delete',
target: 'folder',
title: 'action2'
}
];
let updateActions = dataTable.checkPermissions(row[0], actions);
expect(updateActions[0].disabled).toBeUndefined();
});
});

View File

@@ -236,10 +236,40 @@ export class DataTableComponent implements AfterContentInit, OnChanges {
getRowActions(row: DataRow, col: DataColumn): any[] {
let event = new DataCellEvent(row, col, []);
this.showRowActionsMenu.emit(event);
return event.value.actions;
return this.checkPermissions(row, event.value.actions);
}
checkPermissions(row: DataRow, actions: any[]) {
let actionsPermission = [];
actions.forEach((action) => {
actionsPermission.push(this.checkPermission(row, action));
});
return actionsPermission;
}
checkPermission(row: DataRow, action) {
if (action.permission) {
if (this.hasPermissions(row)) {
let permissions = row.getValue('allowableOperations');
let findPermission = permissions.find(permission => permission === action.permission);
if (!findPermission && action.disableWithNoPermission === true) {
action.disabled = true;
}
}
}
return action;
}
private hasPermissions(row: DataRow): boolean {
return row.getValue('allowableOperations') ? true : false;
}
onExecuteRowAction(row: DataRow, action: any) {
this.executeRowAction.emit(new DataRowActionEvent(row, action));
if (action.disabled) {
event.stopPropagation();
} else {
this.executeRowAction.emit(new DataRowActionEvent(row, action));
}
}
}