mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-13 07:04:53 +09:00
Separate roles to their own file
This commit is contained in:
13 files changed
+125
-94
No files matched your search
@@ -5,20 +5,17 @@ const lib = require("@clusterio/lib");
|
||||
|
||||
describe("controller/src/ControllerUser", function() {
|
||||
describe("class ControllerUser", function() {
|
||||
class MockUserManager {
|
||||
roles = new Map([
|
||||
[1, new lib.Role(1, "a", "a role", new Set(["core.admin"]))],
|
||||
[2, new lib.Role(2, "b", "b role", new Set(["user-test"]))],
|
||||
]);
|
||||
}
|
||||
const userManager = new MockUserManager();
|
||||
const roles = new Map([
|
||||
[1, new lib.Role(1, "a", "a role", new Set(["core.admin"]))],
|
||||
[2, new lib.Role(2, "b", "b role", new Set(["user-test"]))],
|
||||
]);
|
||||
|
||||
it("should round trip serialize", function() {
|
||||
function test_roundtrip(serialized) {
|
||||
let user = ControllerUser.fromJSON(serialized, userManager);
|
||||
let user = ControllerUser.fromJSON(serialized, roles);
|
||||
let user_serialized = user.toJSON(true);
|
||||
assert.deepEqual(user_serialized, serialized);
|
||||
let user_deserialized = ControllerUser.fromJSON(user_serialized, userManager);
|
||||
let user_deserialized = ControllerUser.fromJSON(user_serialized, roles);
|
||||
assert.deepEqual(user_deserialized, user);
|
||||
}
|
||||
|
||||
@@ -31,9 +28,9 @@ describe("controller/src/ControllerUser", function() {
|
||||
describe(".checkPermission()", function() {
|
||||
it("should correctly resolve permissions", function() {
|
||||
lib.definePermission({ name: "user-test", title: "Test", description: "User Test" });
|
||||
let a = ControllerUser.fromJSON({ name: "admin", roles: [1] }, userManager);
|
||||
let b = ControllerUser.fromJSON({ name: "user", roles: [2] }, userManager);
|
||||
let c = ControllerUser.fromJSON({ name: "null", roles: [] }, userManager);
|
||||
let a = ControllerUser.fromJSON({ name: "admin", roles: [1] }, roles);
|
||||
let b = ControllerUser.fromJSON({ name: "user", roles: [2] }, roles);
|
||||
let c = ControllerUser.fromJSON({ name: "null", roles: [] }, roles);
|
||||
|
||||
a.checkPermission("core.control.connect");
|
||||
assert.throws(() => b.checkPermission("core.control.connect"), new Error("Permission denied"));
|
||||
|
||||
@@ -1404,6 +1404,7 @@ describe("Integration of Clusterio", function() {
|
||||
await execCtl(`role create temp ${args}`);
|
||||
let roles = await getControl().send(new lib.RoleListRequest());
|
||||
let tempRole = roles.find(role => role.name === "temp");
|
||||
if (tempRole) { tempRole.updatedAtMs = 0; }
|
||||
assert.deepEqual(
|
||||
tempRole,
|
||||
new lib.Role(5, "temp", "A temp role", new Set(["core.control.connect"]))
|
||||
@@ -1417,6 +1418,7 @@ describe("Integration of Clusterio", function() {
|
||||
await execCtl(`role edit temp ${args}`);
|
||||
let roles = await getControl().send(new lib.RoleListRequest());
|
||||
let newRole = roles.find(role => role.name === "new");
|
||||
if (newRole) { newRole.updatedAtMs = 0; }
|
||||
assert.deepEqual(newRole, new lib.Role(5, "new", "A new role", new Set()));
|
||||
});
|
||||
it("should add permissions with --add-perms", async function() {
|
||||
|
||||
@@ -507,10 +507,9 @@ describe("lib/datastore", function() {
|
||||
["save", new Map([...datastoreProvider.value.entries()].filter((v, k) => k !== "foo"))]
|
||||
);
|
||||
});
|
||||
it("does not break when the key does not exist", function() {
|
||||
it("does nothing when the key does not exist", function() {
|
||||
datastore.delete("test");
|
||||
assert.equal(eventsRaised.length, 1);
|
||||
assert.deepEqual(eventsRaised[0], [["test", undefined, true]]);
|
||||
assert.equal(eventsRaised.length, 0);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+9
-6
@@ -173,17 +173,20 @@ class MockController {
|
||||
},
|
||||
};
|
||||
|
||||
this.userManager = new UserManager(this.config);
|
||||
this.userManager.roles = new Map([
|
||||
const roleProvider = new lib.MemoryDatastoreProvider(new Map([
|
||||
[0, lib.Role.fromJSON({ id: 0, name: "Admin", description: "admin", permissions: ["core.admin"] })],
|
||||
[1, lib.Role.fromJSON({ id: 1, name: "Player", description: "player", permissions: [] })],
|
||||
]);
|
||||
this.userManager.roles.get(1).grantDefaultPermissions();
|
||||
]));
|
||||
|
||||
this.roles = new lib.SubscribableDatastore(roleProvider, roleProvider.value);
|
||||
this.roles.get(1).grantDefaultPermissions();
|
||||
|
||||
this.userManager = new UserManager(this.config, this.roles);
|
||||
this.userManager.users = new Map([
|
||||
["test", ControllerUser.fromJSON({ name: "test", roles: [0, 1] }, this.userManager)],
|
||||
["player", ControllerUser.fromJSON({ name: "player", roles: [1] }, this.userManager)],
|
||||
["test", ControllerUser.fromJSON({ name: "test", roles: [0, 1] }, this.roles)],
|
||||
["player", ControllerUser.fromJSON({ name: "player", roles: [1] }, this.roles)],
|
||||
]);
|
||||
|
||||
this.instances = new lib.KeyValueDatastore();
|
||||
this.hosts = new lib.KeyValueDatastore();
|
||||
this.handles = new Map();
|
||||
|
||||
Reference in new issue
Block a user