Refactor User and Role into data classes (#531)

Use the JSON data class pattern for User, Role, and PlayerStats and move
them into lib/src/data.  This means that User is purely data now and
can't reference role objects directly.  To make it possible easily check
permissions attached to roles and store data only visible on the
controller on user objects a ControllerUser type is introduced which is
linked with the UserManager and can therefore reference role objects
directly.

Resolves #525.
This commit is contained in:
Hornwitser authored and GitHub committed 2023-12-02 01:41:43 +01:00
1 parent f802e6ba67
commit e453a80155
37 files changed
+629 -613

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
"use strict";
const assert = require("assert").strict;
const lib = require("@clusterio/lib");
describe("lib/data/Role", function() {
describe("class Role", function() {
it("should round trip serialize", function() {
let orig = lib.Role.fromJSON({ id: 11, name: "Role", description: "My Role", permissions: ["test"] });
let copy = lib.Role.fromJSON(orig.toJSON());
assert.deepEqual(copy, orig);
});
describe(".grantDefaultRoles()", function() {
it("should only grant permissions with grantByDefault", function() {
let role = lib.Role.fromJSON({ id: 11, name: "Role", description: "My Role" });
role.grantDefaultPermissions();
assert(role.permissions.size > 0, "No permissions were granted");
for (let permission of role.permissions) {
assert(
lib.permissions.get(permission).grantByDefault === true,
"Non-default permission granted"
);
}
});
});
});
});
+67
View File
@@ -0,0 +1,67 @@
"use strict";
const assert = require("assert").strict;
const lib = require("@clusterio/lib");
describe("lib/data/User", function() {
describe("class User", function() {
it("should round trip serialize", function() {
function test_roundtrip(serialized) {
let user = lib.User.fromJSON(serialized);
let user_serialized = user.toJSON();
assert.deepEqual(user_serialized, serialized);
let user_deserialized = lib.User.fromJSON(user_serialized);
assert.deepEqual(user_deserialized, user);
}
test_roundtrip({ name: "admin", roles: [1] });
test_roundtrip({ name: "user", is_admin: true, is_whitelisted: true });
test_roundtrip({ name: "user", is_banned: true, ban_reason: "Bad user" });
test_roundtrip({ name: "user", instance_stats: [[1, { join_count: 1 }]]});
});
it("should keep invalid roles", function() {
let user = lib.User.fromJSON({ name: "test", roles: [1, 4, 55] });
assert.equal(user.roleIds.size, 3, "Unexpected count of roles");
});
it("should calculate playerStats", function() {
let user = lib.User.fromJSON({ name: "test", roles: [1], instance_stats: [
[1, {
join_count: 1,
online_time_ms: 60e3,
last_join_at: new Date("2020-05T12:00Z").getTime(),
last_leave_at: new Date("2020-05T12:01Z").getTime(),
last_leave_reason: "quit",
}],
[2, {
join_count: 1,
online_time_ms: 0,
last_join_at: new Date("2020-05T12:02Z").getTime(),
}],
]});
assert.equal(user.playerStats.onlineTimeMs, 60e3);
assert.equal(user.playerStats.joinCount, 2);
assert.deepEqual(user.playerStats.lastJoinAt, new Date("2020-05T12:02Z"));
assert.deepEqual(user.playerStats.lastLeaveAt, new Date("2020-05T12:01Z"));
assert.equal(user.playerStats.lastLeaveReason, "quit");
user.instanceStats.set(
3,
lib.PlayerStats.fromJSON({
join_count: 2,
online_time_ms: 120e3,
last_join_at: new Date("2020-05T13:00Z").getTime(),
last_leave_at: new Date("2020-05T13:02Z").getTime(),
last_leave_reason: "afk",
}),
);
user.recalculatePlayerStats();
assert.equal(user.playerStats.onlineTimeMs, 180e3);
assert.equal(user.playerStats.joinCount, 4);
assert.deepEqual(user.playerStats.lastJoinAt, new Date("2020-05T13:00Z"));
assert.deepEqual(user.playerStats.lastLeaveAt, new Date("2020-05T13:02Z"));
assert.equal(user.playerStats.lastLeaveReason, "afk");
});
});
});