mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-2553] added select box and updating role (#3148)
* [ADF-2553] start adding dropdown * [ADF-2553] added select box and updating role * [ADF-2553] added some fixes * [ADF-2554] added test for component phase 1 * [ADF-2553] fixed error and added tests * [ADF-2553] added documentation and improved api call
This commit is contained in:
@@ -4,12 +4,27 @@
|
||||
<data-column key="icon" type="icon" [sortable]="false">
|
||||
</data-column>
|
||||
<data-column title="{{'PERMISSION_MANAGER.PERMISSION_DISPLAY.AUTHORITY_ID' | translate}}" key="authorityId"></data-column>
|
||||
<data-column title="{{'PERMISSION_MANAGER.PERMISSION_DISPLAY.NAME' | translate}}" key="name"></data-column>
|
||||
<data-column title="{{'PERMISSION_MANAGER.PERMISSION_DISPLAY.ROLE' | translate}}" key="name">
|
||||
<ng-template let-entry="$implicit">
|
||||
<mat-form-field *ngIf="!entry.row.getValue('isInherited') else show_only_label">
|
||||
<mat-select id="adf-select-role-permission"
|
||||
value="{{entry.data.getValue(entry.row, entry.col)}}"
|
||||
(selectionChange)="saveNewRole($event, entry.row.obj)">
|
||||
<mat-option *ngFor="let role of settableRoles" [value]="role">
|
||||
{{ role }}
|
||||
</mat-option>
|
||||
</mat-select>
|
||||
</mat-form-field>
|
||||
<ng-template #show_only_label>
|
||||
<span>{{entry.data.getValue(entry.row, entry.col)}}</span>
|
||||
</ng-template>
|
||||
</ng-template>
|
||||
</data-column>
|
||||
<data-column title="{{'PERMISSION_MANAGER.PERMISSION_DISPLAY.INHERITED' | translate}}" key="isInherited">
|
||||
<ng-template let-entry="$implicit">
|
||||
<mat-chip-list>
|
||||
<mat-chip *ngIf="!!entry.data.getValue(entry.row, entry.col) else locally_set_chip"
|
||||
id="adf-permission-inherited-label"
|
||||
<mat-chip *ngIf="!!entry.data.getValue(entry.row, entry.col) else locally_set_chip"
|
||||
id="adf-permission-inherited-label"
|
||||
color="primary" selected="true">{{'PERMISSION_MANAGER.PERMISSION_DISPLAY.INHERITED' | translate}}</mat-chip>
|
||||
</mat-chip-list>
|
||||
<ng-template #locally_set_chip>
|
||||
|
@@ -17,9 +17,16 @@
|
||||
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { PermissionListComponent } from './permission-list.component';
|
||||
import { NodesApiService } from '@alfresco/adf-core';
|
||||
import { By } from '@angular/platform-browser';
|
||||
import { NodesApiService, SearchService } from '@alfresco/adf-core';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { fakeNodeWithPermissions, fakeNodeInheritedOnly, fakeNodeWithOnlyLocally } from '../../../mock/permission-list.component.mock';
|
||||
import { NodePermissionService } from '../../services/node-permission.service';
|
||||
import { fakeNodeWithPermissions,
|
||||
fakeNodeInheritedOnly,
|
||||
fakeNodeWithOnlyLocally,
|
||||
fakeSiteNodeResponse,
|
||||
fakeSiteRoles,
|
||||
fakeEmptyResponse } from '../../../mock/permission-list.component.mock';
|
||||
|
||||
describe('PermissionDisplayComponent', () => {
|
||||
|
||||
@@ -27,18 +34,22 @@ describe('PermissionDisplayComponent', () => {
|
||||
let component: PermissionListComponent;
|
||||
let element: HTMLElement;
|
||||
let nodeService: NodesApiService;
|
||||
let nodePermissionService: NodePermissionService;
|
||||
let searchApiService: SearchService;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [
|
||||
PermissionListComponent
|
||||
],
|
||||
providers: [NodesApiService]
|
||||
providers: [NodesApiService, NodePermissionService]
|
||||
}).compileComponents().then(() => {
|
||||
fixture = TestBed.createComponent(PermissionListComponent);
|
||||
component = fixture.componentInstance;
|
||||
element = fixture.nativeElement;
|
||||
nodeService = TestBed.get(NodesApiService);
|
||||
nodePermissionService = TestBed.get(NodePermissionService);
|
||||
searchApiService = TestBed.get(SearchService);
|
||||
});
|
||||
}));
|
||||
|
||||
@@ -47,35 +58,126 @@ describe('PermissionDisplayComponent', () => {
|
||||
TestBed.resetTestingModule();
|
||||
}));
|
||||
|
||||
it('should be able to render the component', async() => {
|
||||
it('should be able to render the component', () => {
|
||||
fixture.detectChanges();
|
||||
expect(element.querySelector('#adf-permission-display-container')).not.toBeNull();
|
||||
});
|
||||
|
||||
it('should show the node permissions', async() => {
|
||||
it('should show the node permissions', () => {
|
||||
component.nodeId = 'fake-node-id';
|
||||
spyOn(nodeService, 'getNode').and.returnValue(Observable.of(fakeNodeWithPermissions));
|
||||
spyOn(searchApiService, 'searchByQueryBody').and.returnValue(Promise.resolve(fakeEmptyResponse));
|
||||
fixture.detectChanges();
|
||||
expect(element.querySelector('#adf-permission-display-container')).not.toBeNull();
|
||||
expect(element.querySelectorAll('.adf-datatable-row').length).toBe(4);
|
||||
});
|
||||
|
||||
it('should show inherited label for inherited permissions', async() => {
|
||||
it('should show inherited label for inherited permissions', () => {
|
||||
component.nodeId = 'fake-node-id';
|
||||
spyOn(nodeService, 'getNode').and.returnValue(Observable.of(fakeNodeInheritedOnly));
|
||||
spyOn(searchApiService, 'searchByQueryBody').and.returnValue(Promise.resolve(fakeEmptyResponse));
|
||||
fixture.detectChanges();
|
||||
expect(element.querySelector('#adf-permission-display-container')).not.toBeNull();
|
||||
expect(element.querySelector('#adf-permission-inherited-label')).toBeDefined();
|
||||
expect(element.querySelector('#adf-permission-inherited-label')).not.toBeNull();
|
||||
});
|
||||
|
||||
it('should show locally set label for locally set permissions', async() => {
|
||||
component.nodeId = 'fake-node-id';
|
||||
spyOn(nodeService, 'getNode').and.returnValue(Observable.of(fakeNodeWithOnlyLocally));
|
||||
fixture.detectChanges();
|
||||
expect(element.querySelector('#adf-permission-display-container')).not.toBeNull();
|
||||
expect(element.querySelector('#adf-permission-locallyset-label')).toBeDefined();
|
||||
expect(element.querySelector('#adf-permission-locallyset-label')).not.toBeNull();
|
||||
describe('when it is a locally set permission', () => {
|
||||
|
||||
it('should show locally set label for locally set permissions', () => {
|
||||
component.nodeId = 'fake-node-id';
|
||||
spyOn(nodeService, 'getNode').and.returnValue(Observable.of(fakeNodeWithOnlyLocally));
|
||||
spyOn(nodePermissionService, 'getGroupMemeberByGroupName').and.returnValue(Observable.of(fakeSiteRoles));
|
||||
spyOn(searchApiService, 'searchByQueryBody').and.returnValue(Promise.resolve(fakeSiteNodeResponse));
|
||||
fixture.detectChanges();
|
||||
expect(element.querySelector('#adf-permission-display-container')).not.toBeNull();
|
||||
expect(element.querySelector('#adf-permission-locallyset-label')).toBeDefined();
|
||||
expect(element.querySelector('#adf-permission-locallyset-label')).not.toBeNull();
|
||||
});
|
||||
|
||||
it('should show a dropdown with the possible roles', async(() => {
|
||||
component.nodeId = 'fake-node-id';
|
||||
spyOn(nodeService, 'getNode').and.returnValue(Observable.of(fakeNodeWithOnlyLocally));
|
||||
spyOn(nodePermissionService, 'getGroupMemeberByGroupName').and.returnValue(Observable.of(fakeSiteRoles));
|
||||
spyOn(searchApiService, 'searchByQueryBody').and.returnValue(Promise.resolve(fakeSiteNodeResponse));
|
||||
fixture.detectChanges();
|
||||
fixture.whenStable().then(() => {
|
||||
fixture.detectChanges();
|
||||
expect(element.querySelector('#adf-select-role-permission')).toBeDefined();
|
||||
expect(element.querySelector('#adf-select-role-permission')).not.toBeNull();
|
||||
const selectBox = fixture.debugElement.query(By.css(('#adf-select-role-permission .mat-select-trigger')));
|
||||
selectBox.triggerEventHandler('click', null);
|
||||
fixture.detectChanges();
|
||||
fixture.whenStable().then(() => {
|
||||
fixture.detectChanges();
|
||||
let options: any = fixture.debugElement.queryAll(By.css('mat-option'));
|
||||
expect(options).not.toBeNull();
|
||||
expect(options.length).toBe(4);
|
||||
expect(options[0].nativeElement.innerText).toContain('SiteCollaborator');
|
||||
expect(options[1].nativeElement.innerText).toContain('SiteConsumer');
|
||||
expect(options[2].nativeElement.innerText).toContain('SiteContributor');
|
||||
expect(options[3].nativeElement.innerText).toContain('SiteManager');
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should show the settable roles if the node is not in any site', async(() => {
|
||||
component.nodeId = 'fake-node-id';
|
||||
spyOn(nodeService, 'getNode').and.returnValue(Observable.of(fakeNodeWithOnlyLocally));
|
||||
spyOn(searchApiService, 'searchByQueryBody').and.returnValue(Promise.resolve(fakeEmptyResponse));
|
||||
fixture.detectChanges();
|
||||
fixture.whenStable().then(() => {
|
||||
fixture.detectChanges();
|
||||
expect(element.querySelector('#adf-select-role-permission')).toBeDefined();
|
||||
expect(element.querySelector('#adf-select-role-permission')).not.toBeNull();
|
||||
const selectBox = fixture.debugElement.query(By.css(('#adf-select-role-permission .mat-select-trigger')));
|
||||
selectBox.triggerEventHandler('click', null);
|
||||
fixture.detectChanges();
|
||||
fixture.whenStable().then(() => {
|
||||
fixture.detectChanges();
|
||||
let options: any = fixture.debugElement.queryAll(By.css('mat-option'));
|
||||
expect(options).not.toBeNull();
|
||||
expect(options.length).toBe(5);
|
||||
expect(options[0].nativeElement.innerText).toContain('Contributor');
|
||||
expect(options[1].nativeElement.innerText).toContain('Collaborator');
|
||||
expect(options[2].nativeElement.innerText).toContain('Coordinator');
|
||||
expect(options[3].nativeElement.innerText).toContain('Editor');
|
||||
expect(options[4].nativeElement.innerText).toContain('Consumer');
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should update the role when another value is chosen', async(() => {
|
||||
component.nodeId = 'fake-node-id';
|
||||
spyOn(nodeService, 'getNode').and.returnValue(Observable.of(fakeNodeWithOnlyLocally));
|
||||
spyOn(nodeService, 'updateNode').and.returnValue(Observable.of({id: 'fake-updated-node'}));
|
||||
spyOn(searchApiService, 'searchByQueryBody').and.returnValue(Promise.resolve(fakeEmptyResponse));
|
||||
component.update.subscribe((updatedPermission) => {
|
||||
expect(updatedPermission).not.toBeNull();
|
||||
expect(updatedPermission.name).toBe('Editor');
|
||||
expect(updatedPermission.authorityId).toBe('GROUP_EVERYONE');
|
||||
expect(updatedPermission.accessStatus).toBe('ALLOWED');
|
||||
});
|
||||
fixture.detectChanges();
|
||||
fixture.whenStable().then(() => {
|
||||
fixture.detectChanges();
|
||||
expect(element.querySelector('#adf-select-role-permission')).toBeDefined();
|
||||
expect(element.querySelector('#adf-select-role-permission')).not.toBeNull();
|
||||
const selectBox = fixture.debugElement.query(By.css(('#adf-select-role-permission .mat-select-trigger')));
|
||||
selectBox.triggerEventHandler('click', null);
|
||||
fixture.detectChanges();
|
||||
fixture.whenStable().then(() => {
|
||||
fixture.detectChanges();
|
||||
let options: any = fixture.debugElement.queryAll(By.css('mat-option'));
|
||||
expect(options).not.toBeNull();
|
||||
expect(options.length).toBe(5);
|
||||
options[3].triggerEventHandler('click', {});
|
||||
fixture.detectChanges();
|
||||
expect(nodeService.updateNode).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -15,10 +15,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Component, ViewEncapsulation, Input, OnInit } from '@angular/core';
|
||||
import { Component, ViewEncapsulation, Input, OnInit, EventEmitter, Output } from '@angular/core';
|
||||
import { NodesApiService } from '@alfresco/adf-core';
|
||||
import { MinimalNodeEntryEntity } from 'alfresco-js-api';
|
||||
import { MinimalNodeEntryEntity, PermissionElement } from 'alfresco-js-api';
|
||||
import { PermissionDisplayModel } from '../../models/permission.model';
|
||||
import { NodePermissionService } from '../../services/node-permission.service';
|
||||
|
||||
@Component({
|
||||
selector: 'adf-permission-list',
|
||||
@@ -31,9 +32,15 @@ export class PermissionListComponent implements OnInit {
|
||||
@Input()
|
||||
nodeId: string = '';
|
||||
|
||||
permissionList: PermissionDisplayModel[];
|
||||
@Output()
|
||||
update: EventEmitter<PermissionElement> = new EventEmitter();
|
||||
|
||||
constructor(private nodeService: NodesApiService) {
|
||||
permissionList: PermissionDisplayModel[];
|
||||
settableRoles: any[];
|
||||
actualNode: MinimalNodeEntryEntity;
|
||||
|
||||
constructor(private nodeService: NodesApiService,
|
||||
private nodePermissionService: NodePermissionService) {
|
||||
|
||||
}
|
||||
|
||||
@@ -47,7 +54,11 @@ export class PermissionListComponent implements OnInit {
|
||||
|
||||
private fetchNodePermissions() {
|
||||
this.nodeService.getNode(this.nodeId).subscribe((node: MinimalNodeEntryEntity) => {
|
||||
this.actualNode = node;
|
||||
this.permissionList = this.getPermissionList(node);
|
||||
this.nodePermissionService.getNodeRoles(node).subscribe((settableList: string[]) => {
|
||||
this.settableRoles = settableList;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -69,4 +80,20 @@ export class PermissionListComponent implements OnInit {
|
||||
return allPermissions;
|
||||
}
|
||||
|
||||
saveNewRole(event: any, permissionRow: PermissionDisplayModel) {
|
||||
let updatedPermissionRole: PermissionElement = this.buildUpdatedPermission(event.value, permissionRow);
|
||||
this.nodePermissionService.updatePermissionRoles(this.actualNode, updatedPermissionRole)
|
||||
.subscribe((node: MinimalNodeEntryEntity) => {
|
||||
this.update.emit(updatedPermissionRole);
|
||||
});
|
||||
}
|
||||
|
||||
private buildUpdatedPermission(newRole: string, permissionRow: PermissionDisplayModel): PermissionElement {
|
||||
let permissionRole: PermissionElement = {};
|
||||
permissionRole.accessStatus = permissionRow.accessStatus;
|
||||
permissionRole.name = newRole;
|
||||
permissionRole.authorityId = permissionRow.authorityId;
|
||||
return permissionRole;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -15,19 +15,22 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export class PermissionDisplayModel {
|
||||
accessStatus: string;
|
||||
authorityId: string;
|
||||
name: string;
|
||||
import { PermissionElement } from 'alfresco-js-api';
|
||||
|
||||
export class PermissionDisplayModel implements PermissionElement {
|
||||
|
||||
authorityId?: string;
|
||||
name?: string;
|
||||
accessStatus?: PermissionElement.AccessStatusEnum;
|
||||
isInherited: boolean = false;
|
||||
icon: string;
|
||||
|
||||
constructor(obj?: any) {
|
||||
if (obj) {
|
||||
this.accessStatus = obj.accessStatus;
|
||||
this.authorityId = obj.authorityId;
|
||||
this.name = obj.name;
|
||||
this.isInherited = obj.isInherited;
|
||||
this.accessStatus = obj.accessStatus;
|
||||
this.isInherited = obj.isInherited !== null && obj.isInherited !== undefined ? obj.isInherited : false;
|
||||
this.icon = obj.icon ? obj.icon : 'lock_open';
|
||||
}
|
||||
}
|
||||
|
@@ -23,6 +23,7 @@ import { MaterialModule } from '../material.module';
|
||||
import { PermissionListComponent } from './components/permission-list/permission-list.component';
|
||||
import { DataTableModule, DataColumnModule } from '@alfresco/adf-core';
|
||||
import { InheritPermissionDirective } from './components/inherited-button.directive';
|
||||
import { NodePermissionService } from './services/node-permission.service';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
@@ -38,6 +39,9 @@ import { InheritPermissionDirective } from './components/inherited-button.direct
|
||||
PermissionListComponent,
|
||||
InheritPermissionDirective
|
||||
],
|
||||
providers: [
|
||||
NodePermissionService
|
||||
],
|
||||
exports: [
|
||||
PermissionListComponent,
|
||||
InheritPermissionDirective
|
||||
|
@@ -17,5 +17,5 @@
|
||||
|
||||
export * from './components/permission-list/permission-list.component';
|
||||
export * from './components/inherited-button.directive';
|
||||
|
||||
export * from './services/node-permission.service';
|
||||
export * from './models/permission.model';
|
||||
|
@@ -0,0 +1,98 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { async, TestBed } from '@angular/core/testing';
|
||||
import { NodePermissionService } from './node-permission.service';
|
||||
import { AlfrescoApiService, SearchService, NodesApiService } from '@alfresco/adf-core';
|
||||
import { MinimalNodeEntryEntity, PermissionElement } from 'alfresco-js-api';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { fakeEmptyResponse, fakeNodeWithOnlyLocally, fakeSiteRoles, fakeSiteNodeResponse } from '../../mock/permission-list.component.mock';
|
||||
|
||||
describe('NodePermissionService', () => {
|
||||
|
||||
let service: NodePermissionService,
|
||||
nodeService: NodesApiService,
|
||||
searchApiService: SearchService;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
AlfrescoApiService,
|
||||
NodePermissionService, SearchService, NodesApiService
|
||||
]
|
||||
}).compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
service = TestBed.get(NodePermissionService);
|
||||
searchApiService = TestBed.get(SearchService);
|
||||
nodeService = TestBed.get(NodesApiService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
TestBed.resetTestingModule();
|
||||
});
|
||||
|
||||
function returnUpdatedNode(nodeId, nodeBody) {
|
||||
let fakeNode: MinimalNodeEntryEntity = {};
|
||||
fakeNode.id = 'fake-updated-node';
|
||||
fakeNode.permissions = nodeBody.permissions;
|
||||
return Observable.of(fakeNode);
|
||||
}
|
||||
|
||||
it('should return a list of roles taken from the site groups', async(() => {
|
||||
spyOn(searchApiService, 'searchByQueryBody').and.returnValue(Promise.resolve(fakeSiteNodeResponse));
|
||||
spyOn(service, 'getGroupMemeberByGroupName').and.returnValue(Observable.of(fakeSiteRoles));
|
||||
|
||||
service.getNodeRoles(fakeNodeWithOnlyLocally).subscribe((roleArray: string[]) => {
|
||||
expect(roleArray).not.toBeNull();
|
||||
expect(roleArray.length).toBe(4);
|
||||
expect(roleArray[0]).toBe('SiteCollaborator');
|
||||
});
|
||||
}));
|
||||
|
||||
it('should return a list of settable if node has no site', async(() => {
|
||||
spyOn(searchApiService, 'searchByQueryBody').and.returnValue(Promise.resolve(fakeEmptyResponse));
|
||||
|
||||
service.getNodeRoles(fakeNodeWithOnlyLocally).subscribe((roleArray: string[]) => {
|
||||
expect(roleArray).not.toBeNull();
|
||||
expect(roleArray.length).toBe(5);
|
||||
expect(roleArray[0]).toBe('Contributor');
|
||||
});
|
||||
}));
|
||||
|
||||
it('should be able to update a locally set permission role', async(() => {
|
||||
const fakeAccessStatus: any = 'DENIED';
|
||||
const fakePermission: PermissionElement = {
|
||||
'authorityId': 'GROUP_EVERYONE',
|
||||
'name': 'Contributor',
|
||||
'accessStatus' : fakeAccessStatus
|
||||
};
|
||||
|
||||
spyOn(nodeService, 'updateNode').and.callFake((nodeId, permissionBody) => returnUpdatedNode(nodeId, permissionBody));
|
||||
|
||||
service.updatePermissionRoles(fakeNodeWithOnlyLocally, fakePermission).subscribe((node: MinimalNodeEntryEntity) => {
|
||||
expect(node).not.toBeNull();
|
||||
expect(node.id).toBe('fake-updated-node');
|
||||
expect(node.permissions.locallySet.length).toBe(1);
|
||||
expect(node.permissions.locallySet[0].authorityId).toBe(fakePermission.authorityId);
|
||||
expect(node.permissions.locallySet[0].name).toBe(fakePermission.name);
|
||||
expect(node.permissions.locallySet[0].accessStatus).toBe(fakePermission.accessStatus);
|
||||
});
|
||||
}));
|
||||
|
||||
});
|
@@ -0,0 +1,97 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { AlfrescoApiService, SearchService, NodesApiService } from '@alfresco/adf-core';
|
||||
import { QueryBody, MinimalNodeEntryEntity, PathElement, GroupMemberEntry, GroupsPaging, GroupMemberPaging, PermissionElement } from 'alfresco-js-api';
|
||||
|
||||
@Injectable()
|
||||
export class NodePermissionService {
|
||||
|
||||
constructor(private apiService: AlfrescoApiService,
|
||||
private searchApiService: SearchService,
|
||||
private nodeService: NodesApiService) {
|
||||
}
|
||||
|
||||
getNodeRoles(node: MinimalNodeEntryEntity): Observable<string[]> {
|
||||
const retrieveSiteQueryBody: QueryBody = this.buildRetrieveSiteQueryBody(node.path.elements);
|
||||
return Observable.fromPromise(this.searchApiService.searchByQueryBody(retrieveSiteQueryBody))
|
||||
.switchMap((siteNodeList: any) => {
|
||||
if ( siteNodeList.list.entries.length > 0 ) {
|
||||
let siteName = siteNodeList.list.entries[0].entry.name;
|
||||
return this.getGroupMembersBySiteName(siteName);
|
||||
} else {
|
||||
return Observable.of(node.permissions.settable);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
updatePermissionRoles(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);
|
||||
if (index !== -1) {
|
||||
permissionBody.permissions.locallySet[index] = updatedPermissionRole;
|
||||
} else {
|
||||
permissionBody.permissions.locallySet.push(updatedPermissionRole);
|
||||
}
|
||||
return this.nodeService.updateNode(node.id, permissionBody);
|
||||
}
|
||||
|
||||
private getGroupMembersBySiteName(siteName: string): Observable<string[]> {
|
||||
const groupName = 'GROUP_site_' + siteName;
|
||||
return this.getGroupMemeberByGroupName(groupName)
|
||||
.map((res: GroupsPaging) => {
|
||||
let displayResult: string[] = [];
|
||||
res.list.entries.forEach((member: GroupMemberEntry) => {
|
||||
displayResult.push(this.formattedRoleName(member.entry.displayName, 'site_' + siteName));
|
||||
});
|
||||
return displayResult;
|
||||
});
|
||||
}
|
||||
|
||||
getGroupMemeberByGroupName(groupName: string, opts?: any): Observable<GroupMemberPaging> {
|
||||
return Observable.fromPromise(this.apiService.groupsApi.getGroupMembers(groupName, opts));
|
||||
}
|
||||
|
||||
private formattedRoleName(displayName, siteName): string {
|
||||
return displayName.replace(siteName + '_', '');
|
||||
}
|
||||
|
||||
private buildRetrieveSiteQueryBody(nodePath: PathElement[]): QueryBody {
|
||||
const pathNames = nodePath.map((node: PathElement) => 'name: "' + node.name + '"');
|
||||
const buildedPathNames = pathNames.join(' OR ');
|
||||
return {
|
||||
'query': {
|
||||
'query': buildedPathNames
|
||||
},
|
||||
'paging': {
|
||||
'maxItems': 100,
|
||||
'skipCount': 0
|
||||
},
|
||||
'include': ['aspectNames', 'properties'],
|
||||
'filterQueries': [
|
||||
{
|
||||
'query':
|
||||
"TYPE:'st:site'"
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user