diff --git a/docs/content-services/services/security-controls.service.md b/docs/content-services/services/security-controls.service.md new file mode 100644 index 0000000000..be27af8ba8 --- /dev/null +++ b/docs/content-services/services/security-controls.service.md @@ -0,0 +1,79 @@ +--- +Title: Security Controls service +Added: v2.0.0 +Status: Active +Last reviewed: 2023-02-21 +--- + +# [Security Controls service](../../../lib/content-services/src/lib/security/services/security-controls-groups-marks-security.service.ts "Defined in security-controls-groups-marks-security.service.ts") + +Manages security groups & marks in Content Services. + +## Class members + +### Methods + +- **getSecurityGroup**(skipCount?: `number`, maxItems?: `number`, include?: `string`): [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)``
+ Get security groups. + - _skipCount:_ `number` - The number of entities that exist in the collection before those included in this list. + - _maxItems:_ `number` - The maximum number of items to return in the list. + - _include:_ `string` - Additional information about the security group. + - **Returns** [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)`` +- **createSecurityGroup**(input: `SecurityGroupBody`): [`Observable`](http://reactivex.io/documentation/observable.html)``
+ Creates a security group. + - _input:_ `SecurityGroupBody` - Security group. + - **Returns** [`Observable`](http://reactivex.io/documentation/observable.html)`` +- **createSecurityMarks**(securityGroupId: `string`, input: `SecurityMarkBody[]`): [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)``
+ Create security marks. + - _securityGroupId:_ `string` - The key for the security group id. + - _SecurityMarkBody:_ `SecurityMarkBody[]` - Node security marks list. + - **Returns** [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)`` +- **getSecurityMark**(securityGroupId: `string`, skipCount?: `number`, include?: `string`): [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)``
+ Get security mark value. + - _securityGroupId:_ `string` - The key for the security group id. + - _skipCount:_ `number` - The number of entities that exist in the collection before those included in this list. + - _include:_ `string` - The key for the security mark is in use or not. + - **Returns** [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)`` +- **updateSecurityGroup**(securityGroupId: `string`, input: `SecurityGroupBody`, opts?: `any`): [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)``
+ Update a security groups information. + - _securityGroupId:_ `string` - The key of security group id for which info is required. + - _input:_ `SecurityGroupBody` - Security group. + - _opts:_ `any` - (Optional) Extra options supported by JS-API. + - **Returns** [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)`` +- **updateSecurityMark**(securityGroupId: `string`, securityMarkId: `string`, input: `SecurityMarkBody`): [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)``
+ Updates Security Mark value. + - _securityGroupId:_ `string` - The key for the security group id. + - _securityMarkId:_ `string` - The key for the security mark is in use or not. + - _input:_ `SecurityMarkBody` - Security mark. + - **Returns** [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)`` +- **deleteSecurityGroup**(securityGroupId: `string`): [`Observable`](http://reactivex.io/documentation/observable.html)``
+ Delete security group. + - _securityGroupId:_ `string` - The key for the security group id. + - **Returns** [`Observable`](http://reactivex.io/documentation/observable.html)`` +- **deleteSecurityMark**(securityGroupId: `string`, securityMarkId: `string`): [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)``
+ Delete security mark. + - _securityGroupId:_ `string` - The key for the security group id. + - _securityMarkId:_ `string` - The key for the security mark id. + - **Returns** [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)`` +- **getClearancesForAuthority**(authorityName: `string`, skipCount?: `number`, maxItems?: `number`): [`Observable`](http://reactivex.io/documentation/observable.html)``
+ Get the authority clearances for a single user/group. + - _authorityName:_ `string` - The name for the authority for which the clearance is to be fetched. + - _skipCount:_ `number` - The number of entities that exist in the collection before those included in this list. + - _maxItems:_ `number` - The maximum number of items to return in the list. + - **Returns** [`Observable`](http://reactivex.io/documentation/observable.html)`` +- **updateClearancesForAuthority**(authorityName: `string`, securityMarksList: `NodeSecurityMarkBody[]`): [`Observable`](http://reactivex.io/documentation/observable.html)``
+ Updates the authority clearance. + - _authorityName:_ `string` - The name for the authority for which the clearance is to be updated. + - _securityMarksList:_ `NodeSecurityMarkBody[]` - Node security marks list. + - **Returns** [`Observable`](http://reactivex.io/documentation/observable.html)`` + + +## Properties + +| Name | Type | Description | +| ---- | ---- |--------------------------| +| groupsPaginated$ | [`Observable`](http://reactivex.io/documentation/observable.html)`` | Current paginated groups. | +| marksPaginated$ | [`Observable`](http://reactivex.io/documentation/observable.html)`` | Current paginated marks. | +| reloadSecurityControls$ | [`Observable`](http://reactivex.io/documentation/observable.html)`` | | +| reloadAuthorityClearance$ | [`Observable`](http://reactivex.io/documentation/observable.html)`` | | +| loading$ | [`Observable`](http://reactivex.io/documentation/observable.html)`` | Current loading state. | diff --git a/lib/content-services/src/lib/content.module.ts b/lib/content-services/src/lib/content.module.ts index d288f143db..a714add5e2 100644 --- a/lib/content-services/src/lib/content.module.ts +++ b/lib/content-services/src/lib/content.module.ts @@ -49,6 +49,7 @@ import { NodeCommentsModule } from './node-comments/node-comments.module'; import { TreeModule } from './tree/tree.module'; import { AlfrescoViewerModule } from './viewer/alfresco-viewer.module'; import { ContentUserInfoModule } from './content-user-info/content-user-info.module'; +import { SecurityControlsServiceModule } from './security/services/security-controls-service.module'; @NgModule({ imports: [ @@ -82,7 +83,8 @@ import { ContentUserInfoModule } from './content-user-info/content-user-info.mod NodeCommentsModule, TreeModule, SearchTextModule, - AlfrescoViewerModule + AlfrescoViewerModule, + SecurityControlsServiceModule ], providers: [ { @@ -120,7 +122,8 @@ import { ContentUserInfoModule } from './content-user-info/content-user-info.mod NodeCommentsModule, TreeModule, SearchTextModule, - AlfrescoViewerModule + AlfrescoViewerModule, + SecurityControlsServiceModule ] }) export class ContentModule { diff --git a/lib/content-services/src/lib/security/index.ts b/lib/content-services/src/lib/security/index.ts new file mode 100644 index 0000000000..a7e30cc675 --- /dev/null +++ b/lib/content-services/src/lib/security/index.ts @@ -0,0 +1,18 @@ +/*! + * @license + * Copyright 2019 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. + */ + +export * from './public-api'; diff --git a/lib/content-services/src/lib/security/public-api.ts b/lib/content-services/src/lib/security/public-api.ts new file mode 100644 index 0000000000..dd452fe779 --- /dev/null +++ b/lib/content-services/src/lib/security/public-api.ts @@ -0,0 +1,19 @@ +/*! + * @license + * Copyright 2019 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. + */ + +export * from './services/security-controls-groups-marks-security.service'; +export * from './services/security-controls-service.module'; diff --git a/lib/content-services/src/lib/security/services/mock/security-authorities.mock.ts b/lib/content-services/src/lib/security/services/mock/security-authorities.mock.ts new file mode 100644 index 0000000000..252c260f78 --- /dev/null +++ b/lib/content-services/src/lib/security/services/mock/security-authorities.mock.ts @@ -0,0 +1,41 @@ +/*! + * @license + * Copyright 2019 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 { AuthorityClearanceGroupPaging } from '@alfresco/js-api'; + +export const fakeAuthorityClearanceApiResponse: AuthorityClearanceGroupPaging = { + 'list': { + 'pagination': { + 'count': 1, + 'hasMoreItems': false, + 'totalItems': 1, + 'skipCount': 0, + 'maxItems': 10 + }, + 'entries': [ + { + 'entry': { + 'id': 'test-id', + 'displayLabel': 'test-displayLabel', + 'systemGroup': false, + 'type': 'test-type', + 'marks': [] + } + } + ] + } +}; diff --git a/lib/content-services/src/lib/security/services/mock/security-groups.mock.ts b/lib/content-services/src/lib/security/services/mock/security-groups.mock.ts new file mode 100644 index 0000000000..eaa6b5f8db --- /dev/null +++ b/lib/content-services/src/lib/security/services/mock/security-groups.mock.ts @@ -0,0 +1,74 @@ +/*! + * @license + * Copyright 2019 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 { SecurityGroupBody, SecurityGroupPaging } from '@alfresco/js-api'; + +export const fakeGroupsApiResponse: SecurityGroupPaging = { + list: { + pagination: { + count: 4, + hasMoreItems: false, + maxItems: 5, + skipCount: 0, + totalItems: 4 + }, + + entries: [ + { + entry: { + groupName: 'Classification', + groupType: 'HIERARCHICAL', + id: 'classification', + inUse: true, + systemGroup: true + } + }, + { + entry: { + groupName: 'SG1', + groupType: 'USER_REQUIRES_ALL', + id: 'd2b11d9f-2707-439f-a7c6-e7872f395553', + inUse: true, + systemGroup: true + } + }, + { + entry: { + groupName: 'SG2', + groupType: 'USER_REQUIRES_ALL', + id: '1b77a32d-6b8b-4a37-b195-7f2ff2fe4ed3', + inUse: true, + systemGroup: true + } + }, + { + entry: { + groupName: 'SG3', + groupType: 'USER_REQUIRES_ALL', + id: '709791f8-22dc-428a-82dd-daf3e1aa8a60', + inUse: true, + systemGroup: true + } + } + ] + } +}; + +export const createNewSecurityGroupMock: SecurityGroupBody = { + groupName: 'TestGroup', + groupType: 'HIERARCHICAL' +}; diff --git a/lib/content-services/src/lib/security/services/mock/security-marks.mock.ts b/lib/content-services/src/lib/security/services/mock/security-marks.mock.ts new file mode 100644 index 0000000000..c758d7c3d8 --- /dev/null +++ b/lib/content-services/src/lib/security/services/mock/security-marks.mock.ts @@ -0,0 +1,51 @@ +/*! + * @license + * Copyright 2019 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 { SecurityMarkPaging, SecurityMarkEntry, SecurityMarkBody } from '@alfresco/js-api'; + +export const fakeMarksApiResponse: SecurityMarkPaging = { + 'list': { + 'pagination': { + 'count': 1, + 'hasMoreItems': false, + 'totalItems': 1, + 'skipCount': 0, + 'maxItems': 10 + }, + 'entries': [ + { + 'entry': { + 'groupId': 'eddf6269-ceba-42c6-b979-9ac445d29a94', + 'name': 'securityMark1', + 'id': 'ffBOeOJJ' + } + } + ] + } +}; + +export const fakeCreateMarksApiResponse: SecurityMarkEntry = { + entry: { + groupId: 'e7b83b84-bb48-46fc-8e64-4b6be910906c', + name: 'securityMark1', + id: 'ffBOeOJJ' + } +}; + +export const createNewSecurityMarkMock: SecurityMarkBody[] = [{ + name: 'securityMark1' +}]; diff --git a/lib/content-services/src/lib/security/services/models/security-controls-group-info-response.interface.ts b/lib/content-services/src/lib/security/services/models/security-controls-group-info-response.interface.ts new file mode 100644 index 0000000000..689172e28e --- /dev/null +++ b/lib/content-services/src/lib/security/services/models/security-controls-group-info-response.interface.ts @@ -0,0 +1,26 @@ +/*! + * @license + * Copyright 2019 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 { SecurityMark } from '@alfresco/js-api'; + +export interface SecurityControlsGroupInfoResponse { + id: string; + groupName: string; + groupType: string; + inUse: boolean; + marks: SecurityMark[]; +} diff --git a/lib/content-services/src/lib/security/services/models/security-controls-group-list-response.interface.ts b/lib/content-services/src/lib/security/services/models/security-controls-group-list-response.interface.ts new file mode 100644 index 0000000000..6891dd9023 --- /dev/null +++ b/lib/content-services/src/lib/security/services/models/security-controls-group-list-response.interface.ts @@ -0,0 +1,25 @@ +/*! + * @license + * Copyright 2019 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 { PaginationModel } from '@alfresco/adf-core'; +import { Observable } from 'rxjs'; +import { SecurityControlsGroupInfoResponse } from './security-controls-group-info-response.interface'; + +export interface SecurityControlsGroupListResponse { + pagination: PaginationModel; + entries: Observable; +} diff --git a/lib/content-services/src/lib/security/services/models/security-controls-group-response.interface.ts b/lib/content-services/src/lib/security/services/models/security-controls-group-response.interface.ts new file mode 100644 index 0000000000..cd12653fcc --- /dev/null +++ b/lib/content-services/src/lib/security/services/models/security-controls-group-response.interface.ts @@ -0,0 +1,24 @@ +/*! + * @license + * Copyright 2019 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 { PaginationModel } from '@alfresco/adf-core'; +import { SecurityGroup } from '@alfresco/js-api'; + +export interface SecurityControlsGroupResponse { + pagination: PaginationModel; + entries: SecurityGroup[]; +} diff --git a/lib/content-services/src/lib/security/services/models/security-controls-mark-response.interface.ts b/lib/content-services/src/lib/security/services/models/security-controls-mark-response.interface.ts new file mode 100644 index 0000000000..e9ae824f2d --- /dev/null +++ b/lib/content-services/src/lib/security/services/models/security-controls-mark-response.interface.ts @@ -0,0 +1,24 @@ +/*! + * @license + * Copyright 2019 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 { PaginationModel } from '@alfresco/adf-core'; +import { SecurityMark } from '@alfresco/js-api'; + +export interface SecurityControlsMarkResponse { + pagination: PaginationModel; + entries: SecurityMark[]; +} diff --git a/lib/content-services/src/lib/security/services/security-controls-groups-marks-security.service.spec.ts b/lib/content-services/src/lib/security/services/security-controls-groups-marks-security.service.spec.ts new file mode 100644 index 0000000000..170616dce3 --- /dev/null +++ b/lib/content-services/src/lib/security/services/security-controls-groups-marks-security.service.spec.ts @@ -0,0 +1,307 @@ +/*! + * @license + * Copyright 2019 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 { TestBed } from '@angular/core/testing'; +import { CoreTestingModule } from '@alfresco/adf-core'; +import { SecurityControlsService } from './security-controls-groups-marks-security.service'; +import { fakeAuthorityClearanceApiResponse } from './mock/security-authorities.mock'; +import { + fakeGroupsApiResponse, + createNewSecurityGroupMock +} from './mock/security-groups.mock'; +import { + fakeMarksApiResponse, + createNewSecurityMarkMock +} from './mock/security-marks.mock'; +import { + SecurityGroupBody, + SecurityGroupEntry, + SecurityMarkBody, + SecurityMarkEntry +} from '@alfresco/js-api'; + +describe('SecurityControlsService', () => { + let service: SecurityControlsService; + let securityGroupId; + let securityMarkId; + const securityGroupBody: SecurityGroupBody = { + groupName: 'TestGroup', + groupType: 'HIERARCHICAL' + }; + const securityMarkBody: SecurityMarkBody = { + name: 'securityMark1' + }; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [CoreTestingModule] + }); + + service = TestBed.inject(SecurityControlsService); + }); + + it('should be able to get the list of Security groups', async () => { + const getGroupSpy = spyOn( + service.groupsApi, + 'getSecurityGroups' + ).and.returnValue(Promise.resolve(fakeGroupsApiResponse)); + const groupPromise = service.getSecurityGroup( 0, 5, 'inUse'); + const group = await groupPromise; + expect(getGroupSpy).toHaveBeenCalledWith({ + skipCount: 0, + maxItems: 5, + include: 'inUse' + }); + + expect(group.pagination.skipCount).toBe(0); + expect(group.pagination.maxItems).toBe(5); + expect(group.entries[0].id).toBe('classification'); + expect(group.entries[0].groupName).toBe('Classification'); + expect(group.entries[0].groupType).toBe('HIERARCHICAL'); + + expect(group.entries[1].id).toBe( + 'd2b11d9f-2707-439f-a7c6-e7872f395553' + ); + expect(group.entries[1].groupName).toBe('SG1'); + expect(group.entries[1].groupType).toBe('USER_REQUIRES_ALL'); + + expect(group.entries[2].id).toBe( + '1b77a32d-6b8b-4a37-b195-7f2ff2fe4ed3' + ); + expect(group.entries[2].groupName).toBe('SG2'); + expect(group.entries[2].groupType).toBe('USER_REQUIRES_ALL'); + + expect(group.entries[3].id).toBe( + '709791f8-22dc-428a-82dd-daf3e1aa8a60' + ); + expect(group.entries[3].groupName).toBe('SG3'); + expect(group.entries[3].groupType).toBe('USER_REQUIRES_ALL'); + }); + + it('should create new security group', async () => { + spyOn(service.groupsApi, 'createSecurityGroup').and.returnValue( + Promise.resolve( + new SecurityGroupEntry({ + entry: { + groupName: 'TestGroup', + groupType: 'HIERARCHICAL', + id: 'eddf6269-ceba-42c6-b979-9ac445d29a94' + } + }) + ) + ); + const response = await service + .createSecurityGroup(createNewSecurityGroupMock) + .toPromise(); + + securityGroupId = response.entry.id; + expect(response.entry.groupName).toEqual('TestGroup'); + expect(response.entry.groupType).toEqual('HIERARCHICAL'); + expect(response.entry.id).toEqual( + 'eddf6269-ceba-42c6-b979-9ac445d29a94' + ); + }); + + it('should be able to get the list of Security Marks', async () => { + const getMarkSpy = spyOn( + service.marksApi, + 'getSecurityMarks' + ).and.returnValue(Promise.resolve(fakeMarksApiResponse)); + const markPromise = service.getSecurityMark( + securityGroupId, + 0, + 'inUse' + ); + const mark = await markPromise; + + expect(getMarkSpy).toHaveBeenCalledWith(securityGroupId, { + skipCount: 0, + include: 'inUse' + }); + + expect(mark.pagination.skipCount).toBe(0); + expect(mark.pagination.maxItems).toBe(10); + expect(mark.entries[0].groupId).toBe( + 'eddf6269-ceba-42c6-b979-9ac445d29a94' + ); + expect(mark.entries[0].name).toBe('securityMark1'); + expect(mark.entries[0].id).toBe('ffBOeOJJ'); + }); + + it('should create new security mark', async () => { + spyOn(service.marksApi, 'createSecurityMarks').and.returnValue( + Promise.resolve( + new SecurityMarkEntry({ + entry: { + groupId: 'eddf6269-ceba-42c6-b979-9ac445d29a94', + name: 'securityMark1', + id: 'ffBOeOJJ' + } + }) + ) + ); + + const response = await service.createSecurityMarks(securityGroupId, createNewSecurityMarkMock); + if (response instanceof SecurityMarkEntry) { + securityMarkId = response.entry.id; + expect(response.entry.groupId).toEqual('eddf6269-ceba-42c6-b979-9ac445d29a94'); + expect(response.entry.name).toEqual('securityMark1'); + expect(response.entry.id).toEqual('ffBOeOJJ'); + } + }); + + it('should edit a security mark', async () => { + spyOn(service.marksApi, 'updateSecurityMark').and.returnValue( + Promise.resolve( + new SecurityMarkEntry({ + entry: { + groupId: 'eddf6269-ceba-42c6-b979-9ac445d29a94', + name: 'securityMark1', + id: 'ffBOeOJJ' + } + }) + ) + ); + const response = await service + .updateSecurityMark( + securityGroupId, + securityMarkId, + securityMarkBody + ); + + securityGroupId = response.entry.groupId; + securityMarkId = response.entry.id; + expect(response.entry.groupId).toEqual(securityGroupId); + expect(response.entry.name).toEqual('securityMark1'); + expect(response.entry.id).toEqual(securityMarkId); + }); + + it('should update a security group', async () => { + spyOn(service.groupsApi, 'updateSecurityGroup').and.returnValue( + Promise.resolve( + new SecurityGroupEntry({ + entry: { + groupName: 'TestGroup', + groupType: 'HIERARCHICAL', + id: 'eddf6269-ceba-42c6-b979-9ac445d29a94' + } + }) + ) + ); + const opts = {}; + const response = await service.updateSecurityGroup(securityGroupId, securityGroupBody, opts); + expect(response.entry.groupName).toEqual('TestGroup'); + expect(response.entry.groupType).toEqual('HIERARCHICAL'); + expect(response.entry.id).toEqual(securityGroupId); + }); + + it('should delete a security mark', async () => { + spyOn(service.marksApi, 'deleteSecurityMark').and.returnValue( + Promise.resolve( + new SecurityMarkEntry({ + entry: { + groupId: 'eddf6269-ceba-42c6-b979-9ac445d29a94', + name: 'securityMark1', + id: 'ffBOeOJJ' + } + }) + ) + ); + const response = await service.deleteSecurityMark(securityGroupId, securityMarkId); + securityMarkId = response.entry.id; + securityGroupId = response.entry.groupId; + expect(response.entry.groupId).toEqual('eddf6269-ceba-42c6-b979-9ac445d29a94'); + expect(response.entry.name).toEqual('securityMark1'); + expect(response.entry.id).toEqual('ffBOeOJJ'); + }); + + it('should delete a security group', async () => { + spyOn(service.groupsApi, 'deleteSecurityGroup').and.returnValue( + Promise.resolve( + new SecurityGroupEntry({ + entry: { + groupName: 'TestGroup', + groupType: 'HIERARCHICAL', + id: 'eddf6269-ceba-42c6-b979-9ac445d29a94' + } + }) + ) + ); + const response = await service + .deleteSecurityGroup(securityGroupId) + .toPromise(); + + expect(response.entry.groupName).toEqual('TestGroup'); + expect(response.entry.groupType).toEqual('HIERARCHICAL'); + expect(response.entry.id).toEqual('eddf6269-ceba-42c6-b979-9ac445d29a94'); + }); + + it('should be able to get clearances for authority', async () => { + const getClearancesForAuthoritySpy = spyOn( + service.authorityClearanceApi, + 'getAuthorityClearanceForAuthority' + ).and.returnValue(Promise.resolve(fakeAuthorityClearanceApiResponse)); + const clearancePromise = service.getClearancesForAuthority('test-id', 0, 10); + const clearance = await clearancePromise.toPromise(); + + expect(getClearancesForAuthoritySpy).toHaveBeenCalledWith('test-id', { + skipCount: 0, + maxItems: 10 + }); + + expect(clearance.list.pagination.skipCount).toBe(0); + expect(clearance.list.pagination.maxItems).toBe(10); + expect(clearance.list.entries[0].entry.id).toBe('test-id'); + expect(clearance.list.entries[0].entry.displayLabel).toBe('test-displayLabel'); + expect(clearance.list.entries[0].entry.systemGroup).toBe(false); + expect(clearance.list.entries[0].entry.type).toBe('test-type'); + }); + + it('should update a clearances for authority', async () => { + spyOn(service.authorityClearanceApi, 'updateAuthorityClearance').and.returnValue( + Promise.resolve( + new SecurityMarkEntry({ + entry: { + id: 'test-id', + name: 'test-name', + groupId: 'test-groupId' + } + }) + ) + ); + const response = await service.updateClearancesForAuthority('test-id', [{ + groupId: 'test-group-id', + op: 'test-op', + id: 'test-id' + }]).toPromise(); + + if (response instanceof SecurityMarkEntry) { + expect(response.entry.id).toEqual('test-id'); + expect(response.entry.groupId).toEqual('test-groupId'); + expect(response.entry.name).toEqual('test-name'); + } + }); + + it('should reload security groups', doneCallback => { + service.reloadSecurityControls$.subscribe(res => { + expect(res).toBeUndefined(); + doneCallback(); + }); + + service.reloadSecurityGroups(); + }); +}); diff --git a/lib/content-services/src/lib/security/services/security-controls-groups-marks-security.service.ts b/lib/content-services/src/lib/security/services/security-controls-groups-marks-security.service.ts new file mode 100644 index 0000000000..70a86505a0 --- /dev/null +++ b/lib/content-services/src/lib/security/services/security-controls-groups-marks-security.service.ts @@ -0,0 +1,367 @@ +/*! + * @license + * Copyright 2019 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 { BehaviorSubject, from, Observable, Subject } from 'rxjs'; +import { SecurityControlsGroupResponse } from './models/security-controls-group-response.interface'; +import { SecurityControlsMarkResponse } from './models/security-controls-mark-response.interface'; +import { + SecurityGroupsApi, + SecurityMarksApi, + SecurityGroupEntry, + SecurityMarkEntry, + SecurityGroupBody, + SecurityMarkPaging, + SecurityMarkBody, + SecurityGroupPaging, + AuthorityClearanceApi, + AuthorityClearanceGroupPaging, + NodeSecurityMarkBody +} from '@alfresco/js-api'; +import { AlfrescoApiService, UserPreferencesService } from '@alfresco/adf-core'; +import { finalize } from 'rxjs/operators'; + +const DEFAULT_SKIP_COUNT = 0; +const DEFAULT_INCLUDE = 'inUse'; + +@Injectable({ providedIn: 'root' }) +export class SecurityControlsService { + private groupsPaginatedSource = new Subject(); + groupsPaginated$ = this.groupsPaginatedSource.asObservable(); + + private reloadSecurityControls: Subject = new Subject(); + reloadSecurityControls$ = this.reloadSecurityControls.asObservable(); + + private _reloadAuthorityClearance: Subject = new Subject(); + reloadAuthorityClearance$ = this._reloadAuthorityClearance.asObservable(); + + private marksPaginatedSource = new Subject(); + marksPaginated$ = this.marksPaginatedSource.asObservable(); + + private loadingSource = new BehaviorSubject(true); + loading$ = this.loadingSource.asObservable(); + + private securityGroup: SecurityGroupsApi; + private securityMark: SecurityMarksApi; + private authorityClearance: AuthorityClearanceApi; + constructor(private apiService: AlfrescoApiService, private userPreferencesService: UserPreferencesService) {} + + get groupsApi(): SecurityGroupsApi { + return this.securityGroup || (this.securityGroup = new SecurityGroupsApi(this.apiService.getInstance())); + } + + get marksApi(): SecurityMarksApi { + return this.securityMark || (this.securityMark = new SecurityMarksApi(this.apiService.getInstance())); + } + + get authorityClearanceApi(): AuthorityClearanceApi { + return this.authorityClearance || (this.authorityClearance = new AuthorityClearanceApi(this.apiService.getInstance())); + } + + get reloadAuthorityClearance(): Subject { + return this._reloadAuthorityClearance; + } + + /** + * Get All security groups + * + * @param include Additional information about the security group + * @param skipCount The number of entities that exist in the collection before those included in this list. + * @param maxItems The maximum number of items to return in the list. Default is specified by UserPreferencesService. + * @return Promise + */ + getSecurityGroup( + skipCount = DEFAULT_SKIP_COUNT, + maxItems = this.userPreferencesService.paginationSize, + include = DEFAULT_INCLUDE + ): Promise { + let securityControlsGroupResponse: SecurityControlsGroupResponse; + return new Promise((resolve, reject) => { + this.groupsApi + .getSecurityGroups({ + include, + skipCount, + maxItems + }) + .then((response: SecurityGroupPaging) => { + this.groupsPaginatedSource.next( + (securityControlsGroupResponse = { + pagination: response.list.pagination, + entries: response.list.entries.map( + (group: SecurityGroupEntry) => group.entry + ) + }) + ); + resolve(securityControlsGroupResponse); + }) + .catch((error) => reject(error)); + }); + } + + /** + * Create security group + * + * @param input securityGroupBody. + * @return Observable + */ + createSecurityGroup( + input: SecurityGroupBody + ): Observable { + this.loadingSource.next(true); + const payload: SecurityGroupBody = { + ...input + }; + const opts = { + DEFAULT_INCLUDE + }; + const promise = this.groupsApi.createSecurityGroup(payload, opts); + + return from(promise).pipe( + finalize(() => this.loadingSource.next(false)) + ); + } + + /** + * Create security marks + * + * @param securityGroupId The key for the security group id. + * @param input securityMarkBody[]. + * @return Promise + */ + createSecurityMarks( + securityGroupId: string, + input: SecurityMarkBody[] + ): Promise { + this.loadingSource.next(true); + const promise = this.marksApi.createSecurityMarks( + securityGroupId, + input + ) + .then((result) => { + this.loadingSource.next(false); + return result; + }) + .catch((error) => { + this.loadingSource.next(false); + return error; + }); + + return promise; + } + + /** + * Get security mark value + * Gets the value for a selected **securityGroupId**. + * + * @param securityGroupId The key for the security group id. + * @param skipCount The number of entities that exist in the collection before those included in this list. + * @param include The key for the security mark is in use or not + * @return Promise + */ + getSecurityMark( + securityGroupId: string, + skipCount = DEFAULT_SKIP_COUNT, + include = DEFAULT_INCLUDE + ): Promise { + let securityControlsMarkResponse: SecurityControlsMarkResponse; + return new Promise((resolve, reject) => { + this.marksApi + .getSecurityMarks(securityGroupId, { + include, + skipCount + }) + .then((response: SecurityMarkPaging) => { + this.marksPaginatedSource.next( + (securityControlsMarkResponse = { + pagination: response.list.pagination, + entries: response.list.entries.map( + (mark: SecurityMarkEntry) => mark.entry + ) + }) + ); + resolve(securityControlsMarkResponse); + }) + .catch((error) => reject(error)); + }); + } + + /** + * Update a security groups information + * + * @param securityGroupId The Key of Security Group id for which info is required + * @param input SecurityGroupBody + * @param opts additional information about the security group + * @return Promise + */ + updateSecurityGroup( + securityGroupId: string, + input: SecurityGroupBody, + opts?: any + ): Promise { + this.loadingSource.next(true); + const payload: SecurityGroupBody = { + ...input + }; + if (!opts) { + opts = { + DEFAULT_INCLUDE + }; + } + const promise = this.groupsApi.updateSecurityGroup( + securityGroupId, + payload, + opts + ) + .then((result) => { + this.loadingSource.next(false); + return result; + }) + .catch((error) => { + this.loadingSource.next(false); + return error; + }); + + return promise; + } + + /** + * Updates Security Mark value + * + * @param securityGroupId The key for the security group id. + * @param securityMarkId The key for the security mark is in use or not. + * @param input securityMarkBody. + * @return Promise + */ + updateSecurityMark( + securityGroupId: string, + securityMarkId: string, + input: SecurityMarkBody + ): Promise { + this.loadingSource.next(true); + const payload: SecurityMarkBody = { + ...input + }; + const promise = this.marksApi.updateSecurityMark( + securityGroupId, + securityMarkId, + payload + ) + .then((result) => { + this.loadingSource.next(false); + return result; + }) + .catch((error) => { + this.loadingSource.next(false); + return error; + }); + + return promise; + } + + /** + * Delete security group + * + * @param securityGroupId The key for the security group id. + * @return Observable + */ + deleteSecurityGroup( + securityGroupId: string + ): Observable { + this.loadingSource.next(true); + const promise = this.groupsApi.deleteSecurityGroup(securityGroupId); + + return from(promise).pipe( + finalize(() => this.loadingSource.next(false)) + ); + } + + /** + * Delete security mark + * + * @param securityGroupId The key for the security group id. + * @param securityMarkId The key for the security mark id. + * @return Promise + */ + deleteSecurityMark( + securityGroupId: string, + securityMarkId: string + ): Promise { + this.loadingSource.next(true); + const promise = this.marksApi.deleteSecurityMark( + securityGroupId, + securityMarkId + ) + .then((result) => { + this.loadingSource.next(false); + return result; + }) + .catch((error) => { + this.loadingSource.next(false); + return error; + }); + + return promise; + } + + /** + * Get the authority clearances for a single user/group + * + * @param authorityName The name for the authority for which the clearance is to be fetched. Can be left blank in which case it will fetch it for all users with pagination + * @param skipCount The number of entities that exist in the collection before those included in this list. + * @param maxItems The maximum number of items to return in the list. Default is specified by UserPreferencesService. + * @return Observable + */ + getClearancesForAuthority( + authorityName: string, + skipCount = DEFAULT_SKIP_COUNT, + maxItems = this.userPreferencesService.paginationSize + ): Observable { + this.loadingSource.next(true); + const opts = { + skipCount, maxItems + }; + const promise = + this.authorityClearanceApi.getAuthorityClearanceForAuthority(authorityName, opts); + + return from(promise).pipe( + finalize(() => this.loadingSource.next(false)) + ); + } + + /** + * Updates the authority clearance. + * + * @param authorityName The name for the authority for which the clearance is to be updated + * @param securityMarksList NodeSecurityMarkBody[] + * @return Observable + */ + updateClearancesForAuthority(authorityName: string, securityMarksList: NodeSecurityMarkBody[]): Observable { + this.loadingSource.next(true); + const promise = this.authorityClearanceApi.updateAuthorityClearance(authorityName, securityMarksList); + + return from(promise).pipe( + finalize(() => { + this.loadingSource.next(false); + this._reloadAuthorityClearance.next(); + }) + ); + } + + reloadSecurityGroups() { + this.reloadSecurityControls.next(); + } +} diff --git a/lib/content-services/src/lib/security/services/security-controls-service.module.ts b/lib/content-services/src/lib/security/services/security-controls-service.module.ts new file mode 100644 index 0000000000..0d8aeee4d5 --- /dev/null +++ b/lib/content-services/src/lib/security/services/security-controls-service.module.ts @@ -0,0 +1,24 @@ +/*! + * @license + * Copyright 2019 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 { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; + +@NgModule({ + imports: [CommonModule] +}) +export class SecurityControlsServiceModule {} diff --git a/lib/content-services/src/public-api.ts b/lib/content-services/src/public-api.ts index f08cf2f20e..8229bee3be 100644 --- a/lib/content-services/src/public-api.ts +++ b/lib/content-services/src/public-api.ts @@ -45,5 +45,6 @@ export * from './lib/common/index'; export * from './lib/tree/index'; export * from './lib/category/index'; export * from './lib/viewer/index'; +export * from './lib/security/index'; export * from './lib/content.module';