mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +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");
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,7 @@
|
||||
* @author Hornwitser
|
||||
* @module lib/users
|
||||
*/
|
||||
import { Permission, Role } from "./data";
|
||||
import { SubscribableDatastore } from "./datastore";
|
||||
|
||||
import { Permission } from "./data";
|
||||
|
||||
export const permissions = new Map<string, Permission>();
|
||||
|
||||
@@ -58,54 +56,6 @@ export function definePermission({
|
||||
permissions.set(name, new Permission(name, title, description, grantByDefault));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ensure the default admin role has full access
|
||||
*
|
||||
* Ensures the role with id 0 exisits and has the core.admin permission.
|
||||
*
|
||||
* @param roles - role storage to modify.
|
||||
* @returns the default admin role
|
||||
*/
|
||||
export function ensureDefaultAdminRole(roles: SubscribableDatastore<Role>) {
|
||||
let admin = roles.get(0);
|
||||
if (!admin) {
|
||||
admin = Role.fromJSON({
|
||||
id: 0,
|
||||
name: "Cluster Admin",
|
||||
description: "Cluster wide administrator.",
|
||||
permissions: [],
|
||||
});
|
||||
}
|
||||
admin.permissions.add("core.admin");
|
||||
roles.set(admin);
|
||||
return admin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the default player role has default accesses
|
||||
*
|
||||
* Ensures the role with id 1 exists and has all the permissions that are
|
||||
* assigned by default. Note that this may not be the same role as the one
|
||||
* that is assigned to new users by default.
|
||||
*
|
||||
* @param roles - role storage to modify.
|
||||
*/
|
||||
export function ensureDefaultPlayerRole(roles: SubscribableDatastore<Role>) {
|
||||
let player = roles.get(1);
|
||||
if (!player) {
|
||||
player = Role.fromJSON({
|
||||
id: 1,
|
||||
name: "Player",
|
||||
description: "Default player role.",
|
||||
permissions: [],
|
||||
});
|
||||
}
|
||||
player.grantDefaultPermissions();
|
||||
roles.set(player);
|
||||
}
|
||||
|
||||
|
||||
// Definitions for the built in permissions used in Clusterio.
|
||||
// description should answer "this permission allows you to ___"
|
||||
definePermission({
|
||||
|
||||
Reference in new issue
Block a user