From e884a02bca177eeb20c76e9e5a4c6a6aaefb34dc Mon Sep 17 00:00:00 2001 From: minottic Date: Thu, 21 Mar 2024 16:02:02 +0100 Subject: [PATCH] Avoid checking for expiration on tasks --- .../logbook/core/task/task.component.spec.ts | 4 ++-- .../app/logbook/core/task/task.component.ts | 4 ++-- .../app/overview/is-allowed.service.spec.ts | 24 ++++++++++++++++++- scilog/src/app/overview/is-allowed.service.ts | 10 ++++---- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/scilog/src/app/logbook/core/task/task.component.spec.ts b/scilog/src/app/logbook/core/task/task.component.spec.ts index c59ec093..5285c65c 100644 --- a/scilog/src/app/logbook/core/task/task.component.spec.ts +++ b/scilog/src/app/logbook/core/task/task.component.spec.ts @@ -54,8 +54,8 @@ describe('TaskComponent', () => { const canUpdateSpy = spyOn(component['isActionAllowed'], 'canUpdate'); const canDeleteSpy = spyOn(component['isActionAllowed'], 'canDelete'); component.isAllowed(); - expect(canUpdateSpy).toHaveBeenCalledTimes(1); - expect(canDeleteSpy).toHaveBeenCalledTimes(1); + expect(canUpdateSpy).toHaveBeenCalledOnceWith(false); + expect(canDeleteSpy).toHaveBeenCalledOnceWith(false); }); }); diff --git a/scilog/src/app/logbook/core/task/task.component.ts b/scilog/src/app/logbook/core/task/task.component.ts index e18d0358..4329a380 100644 --- a/scilog/src/app/logbook/core/task/task.component.ts +++ b/scilog/src/app/logbook/core/task/task.component.ts @@ -38,8 +38,8 @@ export class TaskComponent implements OnInit { } isAllowed() { - this.isActionAllowed.canDelete(); - this.isActionAllowed.canUpdate(); + this.isActionAllowed.canDelete(false); + this.isActionAllowed.canUpdate(false); } } diff --git a/scilog/src/app/overview/is-allowed.service.spec.ts b/scilog/src/app/overview/is-allowed.service.spec.ts index 95834551..cdc22c67 100644 --- a/scilog/src/app/overview/is-allowed.service.spec.ts +++ b/scilog/src/app/overview/is-allowed.service.spec.ts @@ -81,5 +81,27 @@ describe('IsAllowedService', () => { service['cascadeExpiration']('update'); }); }); - + + [ + [true, 1], + [false, 0] + ].forEach((t, i) => { + it(`should test canDelete ${i}`, () => { + const cascadeExpirationSpy = spyOn(service, 'cascadeExpiration'); + service.canDelete(t[0] as boolean); + expect(cascadeExpirationSpy).toHaveBeenCalledTimes(t[1] as number); + }); + }); + + [ + [true, 1], + [false, 0] + ].forEach((t, i) => { + it(`should test canUpdate ${i}`, () => { + const cascadeExpirationSpy = spyOn(service, 'cascadeExpiration'); + service.canUpdate(t[0] as boolean); + expect(cascadeExpirationSpy).toHaveBeenCalledTimes(t[1] as number); + }); + }); + }); diff --git a/scilog/src/app/overview/is-allowed.service.ts b/scilog/src/app/overview/is-allowed.service.ts index b8be7403..8ba827d9 100644 --- a/scilog/src/app/overview/is-allowed.service.ts +++ b/scilog/src/app/overview/is-allowed.service.ts @@ -35,7 +35,7 @@ export class IsAllowedService { private otherEnabledMembers(action: string): string[] { const aclMembers = (this.snippet?.[`${action}ACL`] ?? []).filter( (m: string) => m != 'admin').concat('admin'); - if (this.userPreferences.userInfo?.roles.some((entry: string) => + if (this.userPreferences.userInfo?.roles?.some((entry: string) => aclMembers?.includes?.(entry) )) return []; @@ -77,8 +77,8 @@ export class IsAllowedService { return false } - canUpdate() { - return this.cascadeExpiration('update') + canUpdate(checkExpiration=true) { + return checkExpiration? this.cascadeExpiration('update'): this.isUserAllowed('update') } private cascadeExpiration(action: string) { @@ -90,7 +90,7 @@ export class IsAllowedService { return false; } - canDelete() { - return this.cascadeExpiration('delete') + canDelete(checkExpiration=true) { + return checkExpiration? this.cascadeExpiration('delete'): this.isUserAllowed('delete') } }