mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-06-30 18:15:11 +00:00
[ADF-5148] ContentService.hasPermissions() should check inhertied permission (#7059)
* [ADF-5148] ContentService.hasPermissions() should check inherited permission * * fixed permission method
This commit is contained in:
parent
e94b2f99bd
commit
1a0f2f5bcc
@ -139,34 +139,45 @@ describe('ContentService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should havePermission be true if permissions is present and you have the permission for the request operation', () => {
|
it('should havePermission be true if permissions is present and you have the permission for the request operation', () => {
|
||||||
const permissionNode = new Node({ permissions: { locallySet: [{ name: 'manager' }, { name: 'collaborator' }, { name: 'consumer' }] } });
|
const permissionNode = new Node({ permissions: { locallySet: [{ name: 'manager', authorityId: 'user1' }, { name: 'collaborator', authorityId: 'user2' }, { name: 'consumer', authorityId: 'user3' }] } });
|
||||||
|
|
||||||
expect(contentService.hasPermissions(permissionNode, 'manager')).toBeTruthy();
|
expect(contentService.hasPermissions(permissionNode, 'manager', 'user1')).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should havePermission be false if permissions is present but you don\'t have the permission for the request operation', () => {
|
it('should havePermission be false if permissions is present but you don\'t have the permission for the request operation', () => {
|
||||||
const permissionNode = new Node({ permissions: { locallySet: [{ name: 'collaborator' }, { name: 'consumer' }] } });
|
const permissionNode = new Node({ permissions: { locallySet: [{ name: 'collaborator', authorityId: 'user1' }, { name: 'consumer', authorityId: 'user2' }] } });
|
||||||
expect(contentService.hasPermissions(permissionNode, 'manager')).toBeFalsy();
|
expect(contentService.hasPermissions(permissionNode, 'manager', 'user1')).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should havePermission works in the opposite way with negate value', () => {
|
it('should havePermission works in the opposite way with negate value', () => {
|
||||||
const permissionNode = new Node({ permissions: { locallySet: [{ name: 'collaborator' }, { name: 'consumer' }] } });
|
const permissionNode = new Node({ permissions: { locallySet: [{ name: 'collaborator', authorityId: 'user1' }, { name: 'consumer', authorityId: 'user2' }] } });
|
||||||
expect(contentService.hasPermissions(permissionNode, '!manager')).toBeTruthy();
|
expect(contentService.hasPermissions(permissionNode, '!manager', 'user1')).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should havePermission return false if no permission parameter are passed', () => {
|
it('should havePermission return false if no permission parameter are passed', () => {
|
||||||
const permissionNode = new Node({ permissions: { locallySet: [{ name: 'collaborator' }, { name: 'consumer' }] } });
|
const permissionNode = new Node({ permissions: { locallySet: [{ name: 'collaborator', authorityId: 'user1' }, { name: 'consumer', authorityId: 'user2' }] } });
|
||||||
expect(contentService.hasPermissions(permissionNode, null)).toBeFalsy();
|
expect(contentService.hasPermissions(permissionNode, null, 'user1')).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should havePermission return true if the permissions is empty and the permission to check is Consumer', () => {
|
it('should havePermission return true if the permissions is empty and the permission to check is Consumer', () => {
|
||||||
const permissionNode = new Node({ permissions: [] });
|
const permissionNode = new Node({ permissions: [] });
|
||||||
expect(contentService.hasPermissions(permissionNode, 'Consumer')).toBeTruthy();
|
expect(contentService.hasPermissions(permissionNode, 'Consumer', 'user1')).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should havePermission return false if the permissions is empty and the permission to check is not Consumer', () => {
|
it('should havePermission return false if the permissions is empty and the permission to check is not Consumer', () => {
|
||||||
const permissionNode = new Node({ permissions: [] });
|
const permissionNode = new Node({ permissions: [] });
|
||||||
expect(contentService.hasPermissions(permissionNode, '!Consumer')).toBeFalsy();
|
expect(contentService.hasPermissions(permissionNode, '!Consumer', 'user1')).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should havePermission be true if inherited permissions is present and you have the permission for the request operation', () => {
|
||||||
|
const permissionNode = new Node({ permissions: { inherited: [{ name: 'manager', authorityId: 'user1' }, { name: 'collaborator', authorityId: 'user2' } ] } });
|
||||||
|
expect(contentService.hasPermissions(permissionNode, 'manager', 'user1')).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should take current logged user id if userId undefined ', () => {
|
||||||
|
spyOn(authService, 'getEcmUsername').and.returnValue('user1');
|
||||||
|
const permissionNode = new Node({ permissions: { inherited: [{ name: 'manager', authorityId: 'user1' }, { name: 'collaborator', authorityId: 'user2' } ] } });
|
||||||
|
expect(contentService.hasPermissions(permissionNode, 'manager')).toBeTruthy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -152,16 +152,20 @@ export class ContentService {
|
|||||||
* Checks if the user has permission on that node
|
* Checks if the user has permission on that node
|
||||||
* @param node Node to check permissions
|
* @param node Node to check permissions
|
||||||
* @param permission Required permission type
|
* @param permission Required permission type
|
||||||
|
* @param userId Optional current user id will be taken by default
|
||||||
* @returns True if the user has the required permissions, false otherwise
|
* @returns True if the user has the required permissions, false otherwise
|
||||||
*/
|
*/
|
||||||
hasPermissions(node: Node, permission: PermissionsEnum | string): boolean {
|
hasPermissions(node: Node, permission: PermissionsEnum | string, userId?: string): boolean {
|
||||||
let hasPermissions = false;
|
let hasPermissions = false;
|
||||||
|
userId = userId ?? this.authService.getEcmUsername();
|
||||||
|
|
||||||
if (node && node.permissions && node.permissions.locallySet) {
|
const permissions = [ ...(node.permissions?.locallySet || []), ...(node.permissions?.inherited || []) ]
|
||||||
|
.filter((currentPermission) => currentPermission.authorityId === userId);
|
||||||
|
if (permissions.length) {
|
||||||
if (permission && permission.startsWith('!')) {
|
if (permission && permission.startsWith('!')) {
|
||||||
hasPermissions = node.permissions.locallySet.find((currentPermission) => currentPermission.name === permission.replace('!', '')) ? false : true;
|
hasPermissions = permissions.find((currentPermission) => currentPermission.name === permission.replace('!', '')) ? false : true;
|
||||||
} else {
|
} else {
|
||||||
hasPermissions = node.permissions.locallySet.find((currentPermission) => currentPermission.name === permission) ? true : false;
|
hasPermissions = permissions.find((currentPermission) => currentPermission.name === permission) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user