From 070bf020a7ad12760e382c30065fa932d266c2ac Mon Sep 17 00:00:00 2001 From: Vito Date: Wed, 27 Mar 2019 11:42:09 +0000 Subject: [PATCH] [ADF-3228] Added lock check for context actions (#4163) * [ADF-3228] Added lock check in content service * [ADF-3228] added unit test for lock check * [ADF-3228] fixed wrong line on rebase * [ADF-3228] fixed e2e related to new lock behaviour * [ADF-3228] externalised lock service and added more unit tests * [ADF-3228] added lock service to disable context actions * [ADF-3228] fixed e2e rebased to the latest --- .../version/version-permissions.e2e.ts | 65 ++----- .../document-list.component.spec.ts | 93 ++++++++++ .../components/document-list.component.ts | 15 +- lib/core/services/lock.service.spec.ts | 166 ++++++++++++++++++ lib/core/services/lock.service.ts | 72 ++++++++ lib/core/services/public-api.ts | 1 + 6 files changed, 356 insertions(+), 56 deletions(-) create mode 100644 lib/core/services/lock.service.spec.ts create mode 100644 lib/core/services/lock.service.ts diff --git a/e2e/content-services/version/version-permissions.e2e.ts b/e2e/content-services/version/version-permissions.e2e.ts index f132b3edf7..0fa20ddedf 100644 --- a/e2e/content-services/version/version-permissions.e2e.ts +++ b/e2e/content-services/version/version-permissions.e2e.ts @@ -178,39 +178,10 @@ describe('Version component permissions', () => { uploadDialog.clickOnCloseButton(); }); - it('[C277204] Should a user with Manager permission not be able to upload a new version for a locked file', () => { - contentServices.versionManagerContent(lockFileModel.name); - - versionManagePage.showNewVersionButton.click(); - - versionManagePage.uploadNewVersionFile(newVersionFile.location); - - versionManagePage.checkFileVersionNotExist('1.1'); - - versionManagePage.closeVersionDialog(); - - uploadDialog.clickOnCloseButton(); - }); - - it('[C277196] Should a user with Manager permission be able to upload a new version for the created file', () => { - contentServices.versionManagerContent(sameCreatorFile.name); - - versionManagePage.showNewVersionButton.click(); - - versionManagePage.uploadNewVersionFile(newVersionFile.location); - - versionManagePage.checkFileVersionExist('1.1'); - expect(versionManagePage.getFileVersionName('1.1')).toEqual(newVersionFile.name); - expect(versionManagePage.getFileVersionDate('1.1')).not.toBeUndefined(); - - versionManagePage.deleteFileVersion('1.1'); - versionManagePage.clickAcceptConfirm(); - - versionManagePage.checkFileVersionNotExist('1.1'); - - versionManagePage.closeVersionDialog(); - - uploadDialog.clickOnCloseButton(); + it('[C277204] Should be disabled the option for locked file', () => { + contentServices.getDocumentList().rightClickOnRow(lockFileModel.name); + const actionVersion = contentServices.checkContextActionIsVisible('Manage versions'); + expect(actionVersion.isEnabled()).toBeFalsy(); }); }); @@ -231,9 +202,9 @@ describe('Version component permissions', () => { }); it('[C277201] Should a user with Consumer permission not be able to upload a new version for a locked file', () => { - contentServices.versionManagerContent(lockFileModel.name); - - notificationPage.checkNotifyContains(`You don't have access to do this`); + contentServices.getDocumentList().rightClickOnRow(lockFileModel.name); + const actionVersion = contentServices.checkContextActionIsVisible('Manage versions'); + expect(actionVersion.isEnabled()).toBeFalsy(); }); }); @@ -291,10 +262,10 @@ describe('Version component permissions', () => { notificationPage.checkNotifyContains(`You don't have access to do this`); }); - it('[C277202] Should a user with Contributor permission not be able to upload a new version for a locked file', () => { - contentServices.versionManagerContent(lockFileModel.name); - - notificationPage.checkNotifyContains(`You don't have access to do this`); + it('[C277202] Should be disabled the option for a locked file', () => { + contentServices.getDocumentList().rightClickOnRow(lockFileModel.name); + const actionVersion = contentServices.checkContextActionIsVisible('Manage versions'); + expect(actionVersion.isEnabled()).toBeFalsy(); }); }); @@ -346,17 +317,9 @@ describe('Version component permissions', () => { }); it('[C277203] Should a user with Collaborator permission not be able to upload a new version for a locked file', () => { - contentServices.versionManagerContent(lockFileModel.name); - - versionManagePage.showNewVersionButton.click(); - - versionManagePage.uploadNewVersionFile(newVersionFile.location); - - versionManagePage.checkFileVersionNotExist('1.1'); - - versionManagePage.closeVersionDialog(); - - uploadDialog.clickOnCloseButton(); + contentServices.getDocumentList().rightClickOnRow(lockFileModel.name); + const actionVersion = contentServices.checkContextActionIsVisible('Manage versions'); + expect(actionVersion.isEnabled()).toBeFalsy(); }); it('[C277199] should a user with Collaborator permission be able to upload a new version for a file with different creator', () => { diff --git a/lib/content-services/document-list/components/document-list.component.spec.ts b/lib/content-services/document-list/components/document-list.component.spec.ts index a563bbcd20..d707c32023 100644 --- a/lib/content-services/document-list/components/document-list.component.spec.ts +++ b/lib/content-services/document-list/components/document-list.component.spec.ts @@ -443,6 +443,99 @@ describe('DocumentList', () => { expect(actions[0].disabled).toBeFalsy(); }); + it('should disable the action if a readonly lock is applied to the file', () => { + let documentMenu = new ContentActionModel({ + permission: 'delete', + target: 'document', + title: 'FileAction' + }); + + documentList.actions = [ + documentMenu + ]; + + let nodeFile = { + entry: { + isFile: true, + name: 'xyz', + isLocked: true, + allowableOperations: ['create', 'update', 'delete'], + properties: { 'cm:lockType': 'READ_ONLY_LOCK', 'cm:lockLifetime': 'PERSISTENT' } + } + }; + + let actions = documentList.getNodeActions(nodeFile); + expect(actions.length).toBe(1); + expect(actions[0].title).toEqual('FileAction'); + expect(actions[0].disabled).toBeTruthy(); + }); + + it('should not disable the action for the lock owner if write lock is applied', () => { + let documentMenu = new ContentActionModel({ + permission: 'delete', + target: 'document', + title: 'FileAction' + }); + + spyOn(apiService.getInstance(), 'getEcmUsername').and.returnValue('lockOwner'); + + documentList.actions = [ + documentMenu + ]; + + let nodeFile = { + entry: { + isFile: true, + name: 'xyz', + isLocked: true, + allowableOperations: ['create', 'update', 'delete'], + properties: { + 'cm:lockType': 'WRITE_LOCK', + 'cm:lockLifetime': 'PERSISTENT', + 'cm:lockOwner': { id: 'lockOwner', displayName: 'lockOwner' } + } + } + }; + + let actions = documentList.getNodeActions(nodeFile); + expect(actions.length).toBe(1); + expect(actions[0].title).toEqual('FileAction'); + expect(actions[0].disabled).toBeFalsy(); + }); + + it('should disable the action if write lock is applied and user is not the lock owner', () => { + let documentMenu = new ContentActionModel({ + permission: 'delete', + target: 'document', + title: 'FileAction' + }); + + spyOn(apiService.getInstance(), 'getEcmUsername').and.returnValue('jerryTheKillerCow'); + + documentList.actions = [ + documentMenu + ]; + + let nodeFile = { + entry: { + isFile: true, + name: 'xyz', + isLocked: true, + allowableOperations: ['create', 'update', 'delete'], + properties: { + 'cm:lockType': 'WRITE_LOCK', + 'cm:lockLifetime': 'PERSISTENT', + 'cm:lockOwner': { id: 'lockOwner', displayName: 'lockOwner' } + } + } + }; + + let actions = documentList.getNodeActions(nodeFile); + expect(actions.length).toBe(1); + expect(actions[0].title).toEqual('FileAction'); + expect(actions[0].disabled).toBeTruthy(); + }); + it('should not disable the action if there is the right permission for the folder', () => { const documentMenu = new ContentActionModel({ disableWithNoPermission: true, diff --git a/lib/content-services/document-list/components/document-list.component.ts b/lib/content-services/document-list/components/document-list.component.ts index 35f23419e6..620f9ea4cd 100644 --- a/lib/content-services/document-list/components/document-list.component.ts +++ b/lib/content-services/document-list/components/document-list.component.ts @@ -42,7 +42,8 @@ import { CustomEmptyContentTemplateDirective, RequestPaginationModel, AlfrescoApiService, - UserPreferenceValues + UserPreferenceValues, + LockService } from '@alfresco/adf-core'; import { Node, NodeEntry, NodePaging, Pagination } from '@alfresco/js-api'; @@ -325,7 +326,8 @@ export class DocumentListComponent implements OnInit, OnChanges, OnDestroy, Afte private customResourcesService: CustomResourcesService, private contentService: ContentService, private thumbnailService: ThumbnailService, - private alfrescoApiService: AlfrescoApiService) { + private alfrescoApiService: AlfrescoApiService, + private lockService: LockService) { this.userPreferencesService.select(UserPreferenceValues.PaginationSize).subscribe((pagSize) => { this.maxItems = this._pagination.maxItems = pagSize; @@ -532,11 +534,14 @@ export class DocumentListComponent implements OnInit, OnChanges, OnDestroy, Afte return action.disabled(node); } - if (action.permission && action.disableWithNoPermission && !this.contentService.hasAllowableOperations(node.entry, action.permission)) { + if ((action.permission && + action.disableWithNoPermission && + !this.contentService.hasAllowableOperations(node.entry, action.permission)) || + this.lockService.isLocked(node.entry)) { return true; + } else { + return action.disabled; } - - return action.disabled; } @HostListener('contextmenu', ['$event']) diff --git a/lib/core/services/lock.service.spec.ts b/lib/core/services/lock.service.spec.ts new file mode 100644 index 0000000000..53ce615a88 --- /dev/null +++ b/lib/core/services/lock.service.spec.ts @@ -0,0 +1,166 @@ +/*! + * @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 { LockService } from './lock.service'; +import { CoreTestingModule } from '../testing/core.testing.module'; +import { setupTestBed } from '../testing/setupTestBed'; +import { Node } from '@alfresco/js-api'; +import { AlfrescoApiServiceMock } from 'core/mock'; +import { AlfrescoApiService } from './alfresco-api.service'; +import moment from 'moment-es6'; + +describe('PeopleProcessService', () => { + + let service: LockService; + let apiService: AlfrescoApiServiceMock; + + const fakeNodeUnlocked: Node = { name: 'unlocked', isLocked: false, isFile: true }; + const fakeFolderNode: Node = { name: 'unlocked', isLocked: false, isFile: false, isFolder: true }; + const fakeNodeNoProperty: Node = { name: 'unlocked', isLocked: true, isFile: true, properties: {} }; + + setupTestBed({ + imports: [CoreTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(LockService); + apiService = TestBed.get(AlfrescoApiService); + }); + + it('should return false when no lock is configured', () => { + expect(service.isLocked(fakeNodeUnlocked)).toBeFalsy(); + }); + + it('should return false when isLocked is true but property `cm:lockType` is not present', () => { + expect(service.isLocked(fakeNodeNoProperty)).toBeFalsy(); + }); + + it('should return false when a node folder', () => { + expect(service.isLocked(fakeFolderNode)).toBeFalsy(); + }); + + describe('When the lock is readonly', () => { + const nodeReadonly: Node = { + name: 'readonly-lock-node', + isLocked: true, + isFile: true, + properties: + { 'cm:lockType': 'READ_ONLY_LOCK', + 'cm:lockLifetime': 'PERSISTENT' } + }; + + const nodeReadOnlyWithExpiredDate: Node = { + name: 'readonly-lock-node', + isLocked: true, + isFile: true, + properties: + { + 'cm:lockType': 'WRITE_LOCK', + 'cm:lockLifetime': 'PERSISTENT', + 'cm:lockOwner': { id: 'lock-owner-user' }, + 'cm:expiryDate': moment().subtract('days', '4') + } + }; + + const nodeReadOnlyWithActiveExpiration: Node = { + name: 'readonly-lock-node', + isLocked: true, + isFile: true, + properties: + { + 'cm:lockType': 'WRITE_LOCK', + 'cm:lockLifetime': 'PERSISTENT', + 'cm:lockOwner': { id: 'lock-owner-user' }, + 'cm:expiryDate': moment().add('days', '4') + } + }; + + it('should return true when readonly lock is active', () => { + expect(service.isLocked(nodeReadonly)).toBeTruthy(); + }); + + it('should return false when readonly lock is expired', () => { + expect(service.isLocked(nodeReadOnlyWithExpiredDate)).toBeFalsy(); + }); + + it('should return true when readonly lock is active and expiration date is active', () => { + expect(service.isLocked(nodeReadOnlyWithActiveExpiration)).toBeTruthy(); + }); + }); + + describe('When only the lock owner is allowed', () => { + const nodeOwnerAllowedLock: Node = { + name: 'readonly-lock-node', + isLocked: true, + isFile: true, + properties: + { + 'cm:lockType': 'WRITE_LOCK', + 'cm:lockLifetime': 'PERSISTENT', + 'cm:lockOwner': { id: 'lock-owner-user' } + } + }; + + const nodeOwnerAllowedLockWithExpiredDate: Node = { + name: 'readonly-lock-node', + isLocked: true, + isFile: true, + properties: + { + 'cm:lockType': 'WRITE_LOCK', + 'cm:lockLifetime': 'PERSISTENT', + 'cm:lockOwner': { id: 'lock-owner-user' }, + 'cm:expiryDate': moment().subtract('days', '4') + } + }; + + const nodeOwnerAllowedLockWithActiveExpiration: Node = { + name: 'readonly-lock-node', + isLocked: true, + isFile: true, + properties: + { + 'cm:lockType': 'WRITE_LOCK', + 'cm:lockLifetime': 'PERSISTENT', + 'cm:lockOwner': { id: 'lock-owner-user' }, + 'cm:expiryDate': moment().add('days', '4') + } + }; + + it('should return false when the user is the lock owner', () => { + spyOn(apiService.getInstance(), 'getEcmUsername').and.returnValue('lock-owner-user'); + expect(service.isLocked(nodeOwnerAllowedLock)).toBeFalsy(); + }); + + it('should return true when the user is not the lock owner', () => { + spyOn(apiService.getInstance(), 'getEcmUsername').and.returnValue('banana-user'); + expect(service.isLocked(nodeOwnerAllowedLock)).toBeTruthy(); + }); + + it('should return false when the user is not the lock owner but the lock is expired', () => { + spyOn(apiService.getInstance(), 'getEcmUsername').and.returnValue('banana-user'); + expect(service.isLocked(nodeOwnerAllowedLockWithExpiredDate)).toBeFalsy(); + }); + + it('should return true when is not the lock owner and the expiration date is valid', () => { + spyOn(apiService.getInstance(), 'getEcmUsername').and.returnValue('banana-user'); + expect(service.isLocked(nodeOwnerAllowedLockWithActiveExpiration)).toBeTruthy(); + }); + + }); +}); diff --git a/lib/core/services/lock.service.ts b/lib/core/services/lock.service.ts new file mode 100644 index 0000000000..56b3f24568 --- /dev/null +++ b/lib/core/services/lock.service.ts @@ -0,0 +1,72 @@ +/*! + * @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 { Node } from '@alfresco/js-api'; +import { AlfrescoApiService } from './alfresco-api.service'; +import moment from 'moment-es6'; +import { Moment } from 'moment'; + +@Injectable({ + providedIn: 'root' +}) +export class LockService { + + constructor(private alfrescoApiService: AlfrescoApiService) { + } + + isLocked(node: Node): boolean { + let isLocked = false; + if (this.hasLockConfigured(node)) { + if (this.isReadOnlyLock(node)) { + isLocked = true; + if (this.isLockExpired(node)) { + isLocked = false; + } + } else if (this.isLockOwnerAllowed(node)) { + isLocked = this.alfrescoApiService.getInstance().getEcmUsername() !== node.properties['cm:lockOwner'].id; + if (this.isLockExpired(node)) { + isLocked = false; + } + } + } + return isLocked; + } + + private hasLockConfigured(node: Node): boolean { + return node.isFile && node.isLocked && node.properties['cm:lockType']; + } + + private isReadOnlyLock(node: Node): boolean { + return node.properties['cm:lockType'] === 'READ_ONLY_LOCK' && node.properties['cm:lockLifetime'] === 'PERSISTENT'; + } + + private isLockOwnerAllowed(node: Node): boolean { + return node.properties['cm:lockType'] === 'WRITE_LOCK' && node.properties['cm:lockLifetime'] === 'PERSISTENT'; + } + + private getLockExpiryTime(node: Node): Moment { + if (node.properties['cm:expiryDate']) { + return moment(node.properties['cm:expiryDate'], 'yyyy-MM-ddThh:mm:ssZ'); + } + } + + private isLockExpired(node: Node): boolean { + let expiryLockTime = this.getLockExpiryTime(node); + return moment().isAfter(expiryLockTime); + } +} diff --git a/lib/core/services/public-api.ts b/lib/core/services/public-api.ts index 19f310b7e5..3af0268852 100644 --- a/lib/core/services/public-api.ts +++ b/lib/core/services/public-api.ts @@ -53,4 +53,5 @@ export * from './login-dialog.service'; export * from './external-alfresco-api.service'; export * from './jwt-helper.service'; export * from './download-zip.service'; +export * from './lock.service'; export * from './automation.service';