[ADF-2826] added a check for duplicate permission add (#3265)

* [ADF-2556] first step to create add people or group to permissions

* [ADF-2556] creating a dialog with user results

* [ADF-2556]
integrated service for add and remove permission from node

* [ADF-2556] fixed behaviour and style for add user group

* [ADF-2556] added some refactoring for dialog service

* [ADF-2556] refactoring the dependencies of the components

* [ADF-2556] added some fix and a new key for dialog

* [ADF-2556] start adding test for node permission service

* [ADF-2556] added test for add permission panel component

* [ADf-2556] adding tests for new add permission component

* [ADF-2556] fixed tests and added documentation

* [ADF-2556] fixed documentation for add-node components

* [ADF-2556] added peer review changes

* [ADF-2826] added a check for duplicate permission add

* [ADF-2826] removed fdescribe

* [ADF-2826] applied peer review changes
This commit is contained in:
Vito 2018-05-10 14:31:29 +01:00 committed by Eugenio Romano
parent b7d11d7697
commit 18ebf9f2f7
6 changed files with 81 additions and 20 deletions

View File

@ -73,7 +73,7 @@ export class DemoPermissionComponent implements OnInit {
showErrorMessage(error) {
this.notificationService.openSnackMessage(
JSON.parse(error.response.text).error.errorKey,
error,
4000
);
}

View File

@ -1,12 +1,12 @@
---
Added: v2.4.0
Status: Active
Last reviewed: 2018-05-04
Last reviewed: 2018-05-03
---
# Add Permission Component
Searches for people or groups to add to the current node permissions.
Allow user to search people or group that could be added to the current node permissions.
![Add Permission Component](../docassets/images/add-permission-component.png)
@ -20,14 +20,18 @@ Searches for people or groups to add to the current node permissions.
## Class members
### Properties
| Name | Type | Default value | Description |
| -- | -- | -- | -- |
### Events
| Name | Type | Description |
| -- | -- | -- |
| select | `EventEmitter<any>` | Emitted when a permission list item is selected. |
| select | `EventEmitter<MinimalNodeEntryEntity>` | |
## Details
This component uses a [Search component](../search.component.md) to retrieve the
groups and people that could be added to the permission list of the current node.
The `select` event is emitted when a result is clicked from the list.
The `select` event will be emitted when a result is clicked from the list.

View File

@ -266,12 +266,15 @@
"NO_PERMISSIONS": "No permissions"
},
"ADD-PERMISSION": {
"SEARCH" : "Search",
"TYPE-MESSAGE" : "Type something to start searching groups or people",
"SEARCH": "Search",
"TYPE-MESSAGE": "Type something to start searching groups or people",
"NO-RESULT": "No result found for this search",
"ADD-ACTION" : "ADD",
"CLOSE-ACTION" : "CLOSE",
"BASE-DIALOG-TITLE" : "Search a group or people to add..."
"ADD-ACTION": "ADD",
"CLOSE-ACTION": "CLOSE",
"BASE-DIALOG-TITLE": "Search a group or people to add..."
},
"ERROR": {
"DUPLICATE-PERMISSION": "One or more of the permission you set is already present : {{list}}"
}
}
}

View File

@ -32,7 +32,7 @@ export class AddPermissionDialogComponent {
@ViewChild('addPermission')
addPermissionComponent: AddPermissionComponent;
currentSelection: MinimalNodeEntity[] = [];
private currentSelection: MinimalNodeEntity[] = [];
constructor(@Inject(MAT_DIALOG_DATA) public data: AddPermissionDialogData) {
}

View File

@ -148,4 +148,35 @@ describe('NodePermissionService', () => {
});
}));
it('should be fail when user select the same authority and role to add', async(() => {
const fakeNodeCopy = Object.assign(fakeNodeWithOnlyLocally);
const fakeDuplicateAuthority: any = [{
'entry': {
'isFolder': false,
'search': {
'score': 0.3541112
},
'isFile': false,
'name': 'GROUP_EVERYONE',
'location': 'nodes',
'id': 'GROUP_EVERYONE',
'nodeType': 'cm:authorityContainer',
'properties': {
'cm:authorityName': 'GROUP_EVERYONE'
},
'parentId': '030d833e-da8e-4f5c-8ef9-d809638bd04b'
}
}];
service.updateLocallySetPermissions(fakeNodeCopy, fakeDuplicateAuthority, ['Contributor'])
.subscribe((node: MinimalNodeEntryEntity) => {
}, (errorMessage) => {
expect(errorMessage).not.toBeNull();
expect(errorMessage).toBeDefined();
expect(errorMessage).toBe('PERMISSION_MANAGER.ERROR.DUPLICATE-PERMISSION');
});
}));
});

View File

@ -17,7 +17,7 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { AlfrescoApiService, SearchService, NodesApiService } from '@alfresco/adf-core';
import { AlfrescoApiService, SearchService, NodesApiService, TranslationService } from '@alfresco/adf-core';
import { QueryBody, MinimalNodeEntryEntity, MinimalNodeEntity, PathElement, GroupMemberEntry, GroupsPaging, GroupMemberPaging, PermissionElement } from 'alfresco-js-api';
import 'rxjs/add/operator/switchMap';
import { of } from 'rxjs/observable/of';
@ -28,7 +28,8 @@ export class NodePermissionService {
constructor(private apiService: AlfrescoApiService,
private searchApiService: SearchService,
private nodeService: NodesApiService) {
private nodeService: NodesApiService,
private translation: TranslationService) {
}
/**
@ -56,7 +57,6 @@ export class NodePermissionService {
* @returns Node with updated permission
*/
updatePermissionRole(node: MinimalNodeEntryEntity, updatedPermissionRole: PermissionElement): Observable<MinimalNodeEntryEntity> {
let permissionBody = { permissions: { locallySet: []} };
const index = node.permissions.locallySet.map((permission) => permission.authorityId).indexOf(updatedPermissionRole.authorityId);
permissionBody.permissions.locallySet = permissionBody.permissions.locallySet.concat(node.permissions.locallySet);
@ -82,10 +82,33 @@ export class NodePermissionService {
updateLocallySetPermissions(node: MinimalNodeEntryEntity, nodes: MinimalNodeEntity[], nodeRole: string[]): Observable<MinimalNodeEntryEntity> {
let permissionBody = { permissions: { locallySet: []} };
const permissionList = this.transformNodeToPermissionElement(nodes, nodeRole[0]);
const duplicatedPermissions = this.getDuplicatedPermissions(node.permissions.locallySet, permissionList);
if (duplicatedPermissions.length > 0) {
const list = duplicatedPermissions.map((permission) => 'authority -> ' + permission.authorityId + ' / role -> ' + permission.name).join(', ');
const duplicatePermissionMessage: string = this.translation.instant('PERMISSION_MANAGER.ERROR.DUPLICATE-PERMISSION', {list});
return Observable.throw(duplicatePermissionMessage);
}
permissionBody.permissions.locallySet = node.permissions.locallySet ? node.permissions.locallySet.concat(permissionList) : permissionList;
return this.nodeService.updateNode(node.id, permissionBody);
}
private getDuplicatedPermissions(nodeLocallySet: PermissionElement[], permissionListAdded: PermissionElement[]): PermissionElement[] {
let duplicatePermissions: PermissionElement[] = [];
permissionListAdded.forEach((permission: PermissionElement) => {
const duplicate = nodeLocallySet.find((localPermission) => this.isEqualPermission(localPermission, permission));
if (duplicate) {
duplicatePermissions.push(duplicate);
}
});
return duplicatePermissions;
}
private isEqualPermission(oldPermission: PermissionElement, newPermission: PermissionElement): boolean {
return oldPermission.accessStatus === newPermission.accessStatus &&
oldPermission.authorityId === newPermission.authorityId &&
oldPermission.name === newPermission.name;
}
private transformNodeToPermissionElement(nodes: MinimalNodeEntity[], nodeRole: any): PermissionElement[] {
return nodes.map((node) => {
let newPermissionElement: PermissionElement = <PermissionElement> {