Skip to content

Commit

Permalink
fix: using initial username as id and preventing later edits (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlimvReal authored Oct 27, 2022
1 parent 8248223 commit df76bca
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/app/core/entity/model/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/app/core/permissions/ability/ability.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
11 changes: 10 additions & 1 deletion src/app/core/user/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
18 changes: 17 additions & 1 deletion src/app/core/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit df76bca

Please sign in to comment.