mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-09-21 17:04:00 +00:00
Move the seed into exp_scenario and seed the permission groups (#457)
The seed roles were owned by exp_roles, but they describe the scenario, so they now live in exp_scenario/seed.ts next to the permissions they grant. Each seed role names the permission group its holders belong to, and the five groups the legacy config defined (Admin, Trusted, Standard, Guest, Restricted) are seeded through exp_groups along with one role mapping per role. Mapping priorities follow how exp_roles ranks a player's highest role, so Jail lands in Restricted regardless of other roles. SeedRolesRequest becomes exp_scenario's SeedRequest behind a new exp_scenario.seed permission, and the button moves to the scenario's web plugin, which gets a web entrypoint for it. exp_roles no longer depends on exp_scenario, and exp_scenario gains controller tests around the seed. With groups seeded from the controller the legacy expcore.permission_groups module and its config are removed, along with the Group rcon static.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { BaseControllerPlugin, InstanceRecord } from "@clusterio/controller";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import * as messages from "./messages";
|
||||
import { SeedRole, seedRoles, flattenSeedPermissions } from "./seed";
|
||||
import * as path from "node:path";
|
||||
|
||||
export class ControllerPlugin extends BaseControllerPlugin {
|
||||
@@ -35,7 +34,6 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
|
||||
this.controller.handle(messages.RoleListRequest, this.handleRoleListRequest.bind(this));
|
||||
this.controller.handle(messages.RoleMetaUpdateRequest, this.handleRoleMetaUpdateRequest.bind(this));
|
||||
this.controller.handle(messages.SeedRolesRequest, this.handleSeedRolesRequest.bind(this));
|
||||
|
||||
this.controller.handle(messages.AssignmentListRequest, this.handleAssignmentListRequest.bind(this));
|
||||
this.controller.handle(messages.AssignmentUpdateRequest, this.handleAssignmentUpdateRequest.bind(this));
|
||||
@@ -45,68 +43,6 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
await this.roleMeta.save();
|
||||
}
|
||||
|
||||
/*
|
||||
Seeding
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create the roles the scenario shipped with. Roles which already exist
|
||||
* by name are reused and only gain the seed permissions.
|
||||
*/
|
||||
async handleSeedRolesRequest() {
|
||||
for (const [index, seedRole] of seedRoles.entries()) {
|
||||
const role = this.seedRole(seedRole);
|
||||
if (!role) {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.roleMeta.set(new messages.RoleMetaRecord(
|
||||
role.id,
|
||||
index + 1,
|
||||
seedRole.priority ?? 0,
|
||||
seedRole.shortHand,
|
||||
"",
|
||||
seedRole.color,
|
||||
seedRole.autoAssignHours === undefined ? null : seedRole.autoAssignHours * 3600000,
|
||||
seedRole.blockAutoAssign ?? false,
|
||||
));
|
||||
}
|
||||
|
||||
this.logger.info(`Seeded ${seedRoles.length} roles`);
|
||||
}
|
||||
|
||||
/** Find or create the clusterio role for a seed role, returns undefined if it has no role to use. */
|
||||
seedRole(seedRole: SeedRole) {
|
||||
const roles = this.controller.roles;
|
||||
if (seedRole.isAdmin) {
|
||||
return roles.get(lib.Role.DefaultAdminRoleId);
|
||||
}
|
||||
if (seedRole.isDefault) {
|
||||
const defaultRoleId = this.controller.config.get("controller.default_role_id");
|
||||
return defaultRoleId !== null ? roles.get(defaultRoleId) : undefined;
|
||||
}
|
||||
|
||||
const permissions = flattenSeedPermissions(seedRole);
|
||||
for (const permission of permissions) {
|
||||
if (!lib.permissions.has(permission)) {
|
||||
this.logger.warn(`Seed role ${seedRole.name} grants unknown permission ${permission}`);
|
||||
}
|
||||
}
|
||||
|
||||
let role = [...roles.valuesMutable()].find(other => other.name === seedRole.name);
|
||||
if (role) {
|
||||
for (const permission of permissions) {
|
||||
role.permissions.add(permission);
|
||||
}
|
||||
} else {
|
||||
const id = Math.max(5, ...[...roles.keys()].map(other => other + 1));
|
||||
role = new lib.Role(id, seedRole.name, "", permissions);
|
||||
this.logger.info(`Created role ${seedRole.name}`);
|
||||
}
|
||||
roles.set(role);
|
||||
return role;
|
||||
}
|
||||
|
||||
/*
|
||||
Role properties
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user