diff --git a/exp_groups/controller.ts b/exp_groups/controller.ts index a6034433..846fc0fd 100644 --- a/exp_groups/controller.ts +++ b/exp_groups/controller.ts @@ -1,15 +1,23 @@ -import { BaseControllerPlugin } from "@clusterio/controller"; +import { BaseControllerPlugin, type Controller } from "@clusterio/controller"; import * as lib from "@clusterio/lib"; import * as messages from "./messages.js"; import * as path from "node:path"; +const loaded = new WeakMap(); + export class ControllerPlugin extends BaseControllerPlugin { groups!: lib.SubscribableDatastore; roleMappings!: lib.SubscribableDatastore; manualAssignments!: lib.SubscribableDatastore; resolvedAssignments!: lib.SubscribableDatastore; + /** The plugin loaded on a controller, for the exp_scenario seed. */ + static get(controller: Controller) { + return loaded.get(controller); + } + async init() { + loaded.set(this.controller, this); const databaseDirectory = this.controller.config.get("controller.database_directory"); this.groups = new lib.SubscribableDatastore( diff --git a/exp_roles/controller.ts b/exp_roles/controller.ts index 96acc167..6d5b46f5 100644 --- a/exp_roles/controller.ts +++ b/exp_roles/controller.ts @@ -1,12 +1,20 @@ -import { BaseControllerPlugin, InstanceRecord } from "@clusterio/controller"; +import { BaseControllerPlugin, InstanceRecord, type Controller } from "@clusterio/controller"; import * as lib from "@clusterio/lib"; import * as messages from "./messages.js"; import * as path from "node:path"; +const loaded = new WeakMap(); + export class ControllerPlugin extends BaseControllerPlugin { roleMeta!: lib.SubscribableDatastore; + /** The plugin loaded on a controller, for the exp_scenario seed. */ + static get(controller: Controller) { + return loaded.get(controller); + } + async init() { + loaded.set(this.controller, this); const databaseDirectory = this.controller.config.get("controller.database_directory"); this.roleMeta = new lib.SubscribableDatastore( diff --git a/exp_scenario/controller.ts b/exp_scenario/controller.ts index e76d0c8e..cd19bd96 100644 --- a/exp_scenario/controller.ts +++ b/exp_scenario/controller.ts @@ -1,9 +1,9 @@ import * as lib from "@clusterio/lib"; import { BaseControllerPlugin } from "@clusterio/controller"; -import { RoleMetaRecord, type ControllerPlugin as RolesPlugin } from "@expcluster/roles"; -import { - GroupRecord, GroupPermissions, RoleMappingRecord, type ControllerPlugin as GroupsPlugin, -} from "@expcluster/permission-groups"; +import { RoleMetaRecord } from "@expcluster/roles"; +import { GroupRecord, GroupPermissions, RoleMappingRecord } from "@expcluster/permission-groups"; +import { ControllerPlugin as RolesPlugin } from "@expcluster/roles/dist/node/controller.js"; +import { ControllerPlugin as GroupsPlugin } from "@expcluster/permission-groups/dist/node/controller.js"; import * as messages from "./messages.js"; import { SeedRole, SeedGroup, seedRoles, seedGroups, flattenSeedPermissions } from "./seed.js"; @@ -19,10 +19,8 @@ export class ControllerPlugin extends BaseControllerPlugin { * permissions, groups which already exist by name are reset to the seed. */ async handleSeedRequest() { - throw new lib.RequestError("Seeding is temporarily disabled, please contact the server administrator to seed the roles and groups"); - /* - const rolesPlugin = this.controller.plugins.get("exp_roles") as RolesPlugin | undefined; - const groupsPlugin = this.controller.plugins.get("exp_groups") as GroupsPlugin | undefined; + const rolesPlugin = RolesPlugin.get(this.controller); + const groupsPlugin = GroupsPlugin.get(this.controller); if (!rolesPlugin || !groupsPlugin) { throw new lib.RequestError("Seeding requires the exp_roles and exp_groups plugins"); } @@ -54,7 +52,6 @@ export class ControllerPlugin extends BaseControllerPlugin { this.seedRoleMappings(groupsPlugin, roleIds, groupIds); this.logger.info(`Seeded ${roleIds.size} roles and ${groupIds.size} permission groups`); - */ } /** Find or create the clusterio role for a seed role, returns undefined if it has no role to use. */ diff --git a/exp_scenario/test/controller.test.js b/exp_scenario/test/controller.test.js index d7b96add..097365ab 100644 --- a/exp_scenario/test/controller.test.js +++ b/exp_scenario/test/controller.test.js @@ -42,7 +42,6 @@ async function startPlugin(t2, { withPlugins = true } = {}) { for (const [name, Plugin] of [["exp_roles", RolesPlugin], ["exp_groups", GroupsPlugin]]) { const plugin = new Plugin({ name }, controller, undefined, logger); await plugin.init(); - controller.plugins.set(name, plugin); } } @@ -65,7 +64,7 @@ t.test("class ControllerPlugin", t2 => { t2.test(".handleSeedRequest() creates the roles and reuses them by name", async t3 => { const { plugin, controller } = await startPlugin(t3); - const rolesPlugin = controller.plugins.get("exp_roles"); + const rolesPlugin = RolesPlugin.get(controller); await plugin.handleSeedRequest(); t3.strictSame(controller.roles.size, seedRoles.length, "every seed role exists"); @@ -80,7 +79,7 @@ t.test("class ControllerPlugin", t2 => { t2.test(".handleSeedRequest() creates the groups and resets them by name", async t3 => { const { plugin, controller } = await startPlugin(t3); - const groupsPlugin = controller.plugins.get("exp_groups"); + const groupsPlugin = GroupsPlugin.get(controller); await plugin.handleSeedRequest(); t3.strictSame(groupsPlugin.groups.size, seedGroups.length, "every seed group exists"); @@ -101,7 +100,7 @@ t.test("class ControllerPlugin", t2 => { t2.test(".handleSeedRequest() maps each role onto its group by rank", async t3 => { const { plugin, controller } = await startPlugin(t3); - const groupsPlugin = controller.plugins.get("exp_groups"); + const groupsPlugin = GroupsPlugin.get(controller); await plugin.handleSeedRequest(); const mapped = seedRoles.filter(role => role.group !== undefined); @@ -123,7 +122,7 @@ t.test("class ControllerPlugin", t2 => { t2.test(".handleSeedRequest() reuses mappings and keeps clear of other mappings", async t3 => { const { plugin, controller } = await startPlugin(t3); - const groupsPlugin = controller.plugins.get("exp_groups"); + const groupsPlugin = GroupsPlugin.get(controller); groupsPlugin.roleMappings.set(new RoleMappingRecord(99, new Set([0, 1]), 5, 2, true)); await plugin.handleSeedRequest();