mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-14 23:24:52 +09:00
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.
48 lines
972 B
TypeScript
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);
|
|
}
|
|
}
|
|
}
|
|
}
|