From 8ae9d24eddb062d26a2aaa8e4af37c36f5c41a0c Mon Sep 17 00:00:00 2001 From: Max <53796487+dyedwiper@users.noreply.github.com> Date: Mon, 13 May 2024 16:15:42 +0200 Subject: [PATCH] BC-7314 Fix resolving of LERNSTORE_VIEW permission (#4997) --- .../shared/domain/entity/user.entity.spec.ts | 38 +++++++++++++++++++ .../src/shared/domain/entity/user.entity.ts | 4 +- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/apps/server/src/shared/domain/entity/user.entity.spec.ts b/apps/server/src/shared/domain/entity/user.entity.spec.ts index 29649394aaf..81b95654d36 100644 --- a/apps/server/src/shared/domain/entity/user.entity.spec.ts +++ b/apps/server/src/shared/domain/entity/user.entity.spec.ts @@ -261,6 +261,44 @@ describe('User Entity', () => { }); describe('when user is a student', () => { + describe('when LERNSTORE_VIEW permission is not set for school', () => { + describe('when user has LERNSTORE_VIEW permission from his role', () => { + const setup = () => { + const role = roleFactory.build({ name: RoleName.STUDENT, permissions: [Permission.LERNSTORE_VIEW] }); + const school = schoolEntityFactory.build(); + const user = userFactory.build({ roles: [role], school }); + + return { user }; + }; + + it('should return the unchanged permissions of the user', () => { + const { user } = setup(); + + const result = user.resolvePermissions(); + + expect(result.sort()).toEqual([Permission.LERNSTORE_VIEW].sort()); + }); + }); + + describe('when user does not have LERNSTORE_VIEW permission from his role', () => { + const setup = () => { + const role = roleFactory.build({ name: RoleName.STUDENT, permissions: [permissionA] }); + const school = schoolEntityFactory.build(); + const user = userFactory.build({ roles: [role], school }); + + return { user }; + }; + + it('should return the unchanged permissions of the user', () => { + const { user } = setup(); + + const result = user.resolvePermissions(); + + expect(result.sort()).toEqual([permissionA].sort()); + }); + }); + }); + describe('when school permissions `LERNSTORE_VIEW` is true', () => { const setup = () => { const role = roleFactory.build({ name: RoleName.STUDENT, permissions: [permissionA] }); diff --git a/apps/server/src/shared/domain/entity/user.entity.ts b/apps/server/src/shared/domain/entity/user.entity.ts index 53a32a1dbd7..1dbdcfa1e3e 100644 --- a/apps/server/src/shared/domain/entity/user.entity.ts +++ b/apps/server/src/shared/domain/entity/user.entity.ts @@ -215,9 +215,9 @@ export class User extends BaseEntityWithTimestamps implements EntityWithSchool { setOfPermissions: Set, schoolPermissions?: SchoolRoles ): Set { - if (schoolPermissions?.student?.LERNSTORE_VIEW) { + if (schoolPermissions?.student?.LERNSTORE_VIEW === true) { setOfPermissions.add(Permission.LERNSTORE_VIEW); - } else { + } else if (schoolPermissions?.student?.LERNSTORE_VIEW === false) { setOfPermissions.delete(Permission.LERNSTORE_VIEW); }