From ffc43c87bc37b382e3f0977dfc72b6a8bc87259a Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 26 Oct 2022 21:26:34 +0200 Subject: [PATCH 1/2] fix: using initial username as id and preventing later edits --- src/app/core/entity/model/entity.ts | 3 ++- src/app/core/user/user.spec.ts | 11 ++++++++++- src/app/core/user/user.ts | 18 +++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/app/core/entity/model/entity.ts b/src/app/core/entity/model/entity.ts index c7c89288ae..4cd6989401 100644 --- a/src/app/core/entity/model/entity.ts +++ b/src/app/core/entity/model/entity.ts @@ -139,12 +139,13 @@ export class Entity { let indices = []; // default indices generated from "name" property - if (this.hasOwnProperty("name")) { + if (typeof this["name"] === "string") { indices = this["name"].split(" "); } return indices; } + set searchIndices(value) { // do nothing, always generated on the fly // searchIndices is only saved to database so it can be used internally for database indexing diff --git a/src/app/core/user/user.spec.ts b/src/app/core/user/user.spec.ts index 845135a414..b3af54acee 100644 --- a/src/app/core/user/user.spec.ts +++ b/src/app/core/user/user.spec.ts @@ -20,11 +20,20 @@ import { testEntitySubclass } from "../entity/model/entity.spec"; describe("User", () => { testEntitySubclass("User", User, { - _id: "User:some-id", + _id: "User:tester", name: "tester", paginatorSettingsPageSize: {}, searchIndices: ["tester"], }); + + it("should not allow to change the name after initialization and set it as the ID", () => { + const user = new User(); + user.name = "test-name"; + + expect(user.name).toBe("test-name"); + expect(user.getId()).toBe("test-name"); + expect(() => (user.name = "another-name")).toThrowError(); + }); }); diff --git a/src/app/core/user/user.ts b/src/app/core/user/user.ts index 9d57c8be10..5f28721864 100644 --- a/src/app/core/user/user.ts +++ b/src/app/core/user/user.ts @@ -34,7 +34,23 @@ export class User extends Entity { label: $localize`:Label of username:Username`, validators: { required: true }, }) - name: string; + set name(value: string) { + if (this._name) { + // Throwing error if trying to change existing username + const label = User.schema.get("name").label; + throw new Error( + $localize`:Error message when trying to change the username|e.g. username cannot be changed after initialization:${label} cannot be changed after initialization` + ); + } + this.entityId = value; + this._name = value; + } + + get name(): string { + return this._name; + } + + private _name: string; /** * settings for the mat-paginator for tables. From 182ed86c24a9a54e424af80f042b8edbbc4f7787 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 27 Oct 2022 08:25:27 +0200 Subject: [PATCH 2/2] fixed test --- src/app/core/permissions/ability/ability.service.spec.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/app/core/permissions/ability/ability.service.spec.ts b/src/app/core/permissions/ability/ability.service.spec.ts index 9065b9083d..1555ca7605 100644 --- a/src/app/core/permissions/ability/ability.service.spec.ts +++ b/src/app/core/permissions/ability/ability.service.spec.ts @@ -212,8 +212,9 @@ describe("AbilityService", () => { const userEntity = new User(); userEntity.name = user.name; expect(ability.can("manage", userEntity)).toBeTrue(); - userEntity.name = "another user"; - expect(ability.cannot("manage", userEntity)).toBeTrue(); + const anotherUser = new User(); + anotherUser.name = "another user"; + expect(ability.cannot("manage", anotherUser)).toBeTrue(); }); it("should allow to check conditions with complex data types", fakeAsync(() => {