mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 23:33:12 +09:00
Move ensure default roles onto Role class
This commit is contained in:
6 files changed
+194
-84
No files matched your search
@@ -1,15 +1,23 @@
|
||||
import { Static, Type } from "@sinclair/typebox";
|
||||
import { permissions as globalPermissions } from "../permissions";
|
||||
|
||||
interface RoleCollection {
|
||||
get(id: number): Role;
|
||||
set(role: Role): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a collection of granted permissions
|
||||
*/
|
||||
export default class Role {
|
||||
static DefaultAdminRoleId = 0 as const;
|
||||
static DefaultPlayerRoleId = 1 as const;
|
||||
|
||||
constructor(
|
||||
public id: number,
|
||||
public name: string,
|
||||
public description: string,
|
||||
public permissions: Set<string>,
|
||||
public permissions = new Set<string>(),
|
||||
public updatedAtMs = 0,
|
||||
public isDeleted = false,
|
||||
) { }
|
||||
@@ -45,6 +53,13 @@ export default class Role {
|
||||
};
|
||||
}
|
||||
|
||||
static ensureDefaultPlayerRole(roles: RoleCollection) {
|
||||
const playerRole = roles.get(this.DefaultPlayerRoleId)
|
||||
?? new this(this.DefaultPlayerRoleId, "Player", "Default player role.");
|
||||
playerRole.grantDefaultPermissions();
|
||||
roles.set(playerRole);
|
||||
}
|
||||
|
||||
grantDefaultPermissions() {
|
||||
for (let permission of globalPermissions.values()) {
|
||||
if (permission.grantByDefault) {
|
||||
@@ -52,4 +67,15 @@ export default class Role {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static ensureDefaultAdminRole(roles: RoleCollection) {
|
||||
const adminRole = roles.get(this.DefaultAdminRoleId)
|
||||
?? new this(this.DefaultAdminRoleId, "Cluster Admin", "Cluster wide administrator.");
|
||||
adminRole.grantAdminPermissions();
|
||||
roles.set(adminRole);
|
||||
}
|
||||
|
||||
grantAdminPermissions() {
|
||||
this.permissions.add("core.admin");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user