Staff roles with permissions, warnings, in-game staff commands

Moderators were one flag on the account. Now an account can hold a staff
role (lib/roles.js): helper, moderator or admin, each a list of named
permissions, with a higher role holding everything below it. An account
only marked `moderator` still counts as a moderator.

Every staff action checks the permission it needs, and staff never act
on staff of their own rank or above:

- warnings: kept on the account (who, when, the reason, a note for
  staff), and shown to the player with the game's own warning popup for
  the reason ("fw": inappropriate behavior, bullying, personal
  information, scamming), which this server's strings already word as the
  Burrow Rules.
- moderator panel: each action is checked against the role; new actions
  "warn" (with a reason and note), "summon" (bring a player to the room you
  are in) and "goto"; the player's page shows their warnings; GET
  /mod/api/me returns your role and permissions, and the page hides the
  buttons yours does not allow.
- in the game: !staff, !summon, !goto, !kick, !silence (HOURS, 0, or
  off), !warn PLAYER [reason] [note], !warnings PLAYER and !message, for
  staff whose role allows each, written to the moderators' log like the
  panel's actions, marked "(in game)". To anyone else they are plain chat.
  Usernames with spaces work.
- the login reply carries the player's own staffRole and
  staffPermissions (empty for everyone else), so a client can show staff
  tools only to staff.
- admin panel: a role picker on the account, "role" on POST
  /admin/api/account, and GET /admin/api/roles listing what each allows.
  The old moderator switch still works.

Also: the moderators' message box showed "--Message From Moderator--\n\n"
with the "\n" as written (strings 11134 and 11135 keep it as two
characters); they get real line breaks.

Staff powers need 2FA turned on, as the moderator panel already did. In
the game they come with the game session, which a trusted device opens
without a code; requiring a recent code there is left for later.

test/staff.js covers all of the above end to end.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
SnipzilandClaude Opus 5.5 committed 2026-10-16 22:42:46 -04:00
1 parent 63dc43ebb3
commit 2fba9f1612
10 files changed
+756 -21

No files matched your search

+53 -10
View File
@@ -13,6 +13,7 @@ const manage = require("./manage.js");
const masterpieces = require("./masterpieces.js");
const modlog = require("./modlog.js");
const reports = require("./reports.js");
const roles = require("./roles.js");
const staff = require("./staff.js");
const stats = require("./stats.js");
const support = require("./support.js");
@@ -23,8 +24,9 @@ const world = require("./world.js");
// players, and no more. it is apart from the admin panel (lib/panel.js),
// which stays whoever runs the server's alone.
//
// A moderator is a game account the admin marked as one (`moderator` on the
// account). they sign in here with that account's username and password and
// A moderator is a game account the admin gave a staff role (lib/roles.js:
// helper, moderator or admin; `moderator` on an older account counts as a
// moderator), and what they may do here is what their role allows. they sign in here with that account's username and password and
// a code from their authenticator app, every time: an account without 2FA
// cannot sign in at all, and a trusted device is not taken for a code.
// being a moderator gives the account nothing in the game itself.
@@ -63,8 +65,7 @@ const cookieHeader = (token, maxAgeSeconds) =>
`${COOKIE}=${token}; Path=/mod; HttpOnly; SameSite=Strict; Max-Age=${maxAgeSeconds}${config.secureCookies ? "; Secure" : ""}`;
// may this account use the panel (still)?
const mayModerate = account => Boolean(account && account.moderator && totp.isOn(account) && !account.locked
&& world.moderation.banRemaining(account) === 0);
const mayModerate = account => Boolean(account && roles.isActiveStaff(account, world.moderation.banRemaining(account) !== 0));
const signedInAs = req => {
const token = cookieToken(req);
@@ -95,7 +96,9 @@ const playerRow = (account, playing) => {
banReason: ban ? ban.reason : "",
silenced: world.moderation.silenceRemaining(account),
nameRejected: Boolean(account.nameRejected),
moderator: Boolean(account.moderator),
moderator: Boolean(roles.roleOf(account)),
role: roles.roleOf(account),
warnings: (account.warnings || []).length,
playtime: account.playtime,
bio: account.bio,
discord: account.discord,
@@ -120,6 +123,7 @@ const playerDetail = account => ({
...playerRow(account, playing()),
history: bans.historyOf(account).map(({at, action, seconds, reason, by}) => ({at, action, seconds, reason, by})),
appeal: bans.appealOf(account),
warningList: (account.warnings || []).slice(-20).reverse(),
openReports: reports.list(false).filter(report => report.target.toLowerCase() === account.username.toLowerCase()).length,
});
@@ -134,6 +138,16 @@ const maskedAddress = ban => ({
const wholeNumber = (value, min, max) => (Number.isInteger(value) && value >= min && value <= max ? value : null);
// the permission (lib/roles.js) each action needs.
const ACTION_PERMISSIONS = {
ban: "player.ban", banReason: "player.ban", unban: "player.ban",
silence: "chat.silence", unsilence: "chat.silence",
kick: "player.kick", message: "player.warn", warn: "player.warn",
rejectName: "player.names", acceptName: "player.names", clearBio: "player.names",
banAddress: "address.ban",
summon: "player.summon", goto: "player.goto",
};
const act = (mod, target, body) => {
const {actions, PERMANENT} = world.moderation;
const name = target.username;
@@ -141,8 +155,12 @@ const act = (mod, target, body) => {
if (target === mod) {
return {error: "That is you."};
}
if (target.moderator) {
return {error: "Another moderator is the admin's to deal with."};
if (!roles.outranks(mod, target)) {
return {error: "Staff of your rank or above are for someone higher up to deal with."};
}
const permission = ACTION_PERMISSIONS[body.action];
if (permission && !roles.can(mod, permission)) {
return {error: "Your role cannot do that."};
}
switch (body.action) {
case "ban": {
@@ -183,6 +201,15 @@ const act = (mod, target, body) => {
}
return actions.message(target, text) ? {ok: `Shown to ${name}.`, detail: text} : {error: `${name} is not online.`};
}
case "warn": {
const reason = String(body.reason || "behavior");
const result = actions.warn(target, reason, by, body.note);
if (!result) {
return {error: `A warning's reason: ${world.moderation.WARNINGS.join(", ")}.`};
}
return {ok: `${name} warned (${reason}): warning ${result.count}.${result.online ? "" : " They are offline, so they will not see it."}`,
detail: `${reason}${body.note ? ` · ${String(body.note).slice(0, 200)}` : ""}`};
}
case "rejectName":
if (target.nameRejected) {
return {error: "Already rejected."};
@@ -213,6 +240,16 @@ const act = (mod, target, body) => {
}
return {ok: "Their address is banned.", detail: result.ban.reason};
}
// the moderator's own animal and the player's, in the game: both have
// to be online, the moderator in a room.
case "summon": {
const result = world.staffMove(target.username, mod.username);
return result.ok ? {ok: `${name} was brought to ${result.room}.`, detail: result.room} : {error: result.error};
}
case "goto": {
const result = world.staffMove(mod.username, target.username);
return result.ok ? {ok: `You went to ${name} (${result.room}).`, detail: result.room} : {error: result.error};
}
default:
return {error: "Unknown action."};
}
@@ -267,7 +304,7 @@ const handle = async (req, res, url, from, web) => {
const account = known && await accounts.authenticateAsync(known.username, body.password) === known ? known : null;
// one answer for a wrong password and for an account that is no
// moderator: who the moderators are is nobody's to find out here.
if (!account || !account.moderator) {
if (!account || !roles.roleOf(account)) {
failuresByAddress.record(from.key);
if (known && !account) {
accounts.recordLogin(known, "mod", from.address, false);
@@ -334,6 +371,12 @@ const handle = async (req, res, url, from, web) => {
};
switch (route) {
// who is signed in here, their role and what it allows: for a client to
// show only the buttons that will work.
case "GET /mod/api/me":
reply(200, {username: mod.username, role: roles.roleOf(mod), permissions: roles.permissionsOf(mod)});
return;
case "GET /mod/api/overview":
reply(200, {online: world.players().length, reports: reports.openCount(), requests: support.nameRequests(false).length + bans.openAppeals(),
paintings: masterpieces.list(masterpieces.PENDING).length});
@@ -468,8 +511,8 @@ const handle = async (req, res, url, from, web) => {
}
};
const moderators = () => accounts.all().filter(account => account.moderator)
.map(account => ({username: account.username, twoStep: totp.isOn(account), lastSeen: account.lastSeen || null}))
const moderators = () => accounts.all().filter(account => roles.roleOf(account))
.map(account => ({username: account.username, role: roles.roleOf(account), twoStep: totp.isOn(account), lastSeen: account.lastSeen || null}))
.sort((a, b) => a.username.localeCompare(b.username));
module.exports = {handle, forget, moderators};