mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[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
This commit is contained in:
166
lib/core/services/lock.service.spec.ts
Normal file
166
lib/core/services/lock.service.spec.ts
Normal file
@@ -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 = <Node> { name: 'unlocked', isLocked: false, isFile: true };
|
||||
const fakeFolderNode: Node = <Node> { name: 'unlocked', isLocked: false, isFile: false, isFolder: true };
|
||||
const fakeNodeNoProperty: Node = <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 = <Node> {
|
||||
name: 'readonly-lock-node',
|
||||
isLocked: true,
|
||||
isFile: true,
|
||||
properties:
|
||||
{ 'cm:lockType': 'READ_ONLY_LOCK',
|
||||
'cm:lockLifetime': 'PERSISTENT' }
|
||||
};
|
||||
|
||||
const nodeReadOnlyWithExpiredDate: Node = <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 = <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 = <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 = <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 = <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();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
72
lib/core/services/lock.service.ts
Normal file
72
lib/core/services/lock.service.ts
Normal file
@@ -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);
|
||||
}
|
||||
}
|
@@ -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';
|
||||
|
Reference in New Issue
Block a user