Files
factorio-clusterio/packages/lib/src/data/Role.ts
T
Hornwitser e453a80155 Refactor User and Role into data classes (#531)
Use the JSON data class pattern for User, Role, and PlayerStats and move
them into lib/src/data.  This means that User is purely data now and
can't reference role objects directly.  To make it possible easily check
permissions attached to roles and store data only visible on the
controller on user objects a ControllerUser type is introduced which is
linked with the UserManager and can therefore reference role objects
directly.

Resolves #525.
2023-12-02 01:41:43 +01:00

48 lines
972 B
TypeScript

import { Static, Type } from "@sinclair/typebox";
import { permissions as globalPermissions } from "../permissions";
/**
* Represents a collection of granted permissions
*/
export default class Role {
constructor(
public id: number,
public name: string,
public description: string,
public permissions: Set<string>,
) { }
static jsonSchema = Type.Object({
id: Type.Number(),
name: Type.String(),
description: Type.String(),
permissions: Type.Array(Type.String()),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(
json.id,
json.name,
json.description,
new Set(json.permissions),
);
}
toJSON() {
return {
id: this.id,
name: this.name,
description: this.description,
permissions: [...this.permissions],
};
}
grantDefaultPermissions() {
for (let permission of globalPermissions.values()) {
if (permission.grantByDefault) {
this.permissions.add(permission.name);
}
}
}
}