Repository navigation
Add a Commands tab to the staff panel #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6adb7b7
Store the server's command list for the Commands tab
ryanbarlow97 1ca6966
Add the Commands tab to the staff panel
ryanbarlow97 fdafbca
Accept command permissions up to 2048 characters
ryanbarlow97 3013a62
Mark the command list stale by age and qualify open commands
ryanbarlow97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| """The server's commands and which LuckPerms groups may use them, for the Commands tab. | ||
|
|
||
| TFMCWeb publishes the whole list; it replaces the stored one. Access is | ||
| decided on the server from each command's own permission, so plugins may | ||
| still refuse a command when it runs. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import time | ||
|
|
||
| from src.skins.db import connect | ||
|
|
||
| from . import mirror, nodes | ||
|
|
||
| GROUPS_MAX = 200 | ||
| PLUGINS_MAX = 500 | ||
| COMMANDS_MAX = 10_000 | ||
| ALIASES_MAX = 64 | ||
| NAME_MAX = 64 | ||
| TEXT_MAX = 300 | ||
| PERMISSION_MAX = 2048 | ||
| # TFMCWeb resends at least every ten minutes. | ||
| FRESH_SECONDS = 30 * 60 | ||
| KIND_ORDER = {"rank": 0, "staff": 1, "step": 2} | ||
|
|
||
|
|
||
| class CommandsError(Exception): | ||
| def __init__(self, code: str): | ||
| super().__init__(code) | ||
| self.code = code | ||
|
|
||
|
|
||
| def _text(raw, limit: int, *, required: bool = False) -> str | None: | ||
| if raw is None and not required: | ||
| return None | ||
| if not isinstance(raw, str) or not raw or len(raw) > limit: | ||
| raise CommandsError("bad_commands") | ||
| return raw | ||
|
|
||
|
|
||
| def _command(raw, plugins: set[str], groups: int) -> dict: | ||
| if not isinstance(raw, dict): | ||
| raise CommandsError("bad_commands") | ||
| aliases = raw.get("aliases") | ||
| if not isinstance(aliases, list) or len(aliases) > ALIASES_MAX: | ||
| raise CommandsError("bad_commands") | ||
| plugin = _text(raw.get("plugin"), NAME_MAX, required=True) | ||
| if plugin not in plugins or not isinstance(raw.get("shadowed"), bool): | ||
| raise CommandsError("bad_commands") | ||
| permission = _text(raw.get("permission"), PERMISSION_MAX) | ||
| access = raw.get("access") | ||
| if permission is None: | ||
| if access is not None: | ||
| raise CommandsError("bad_commands") | ||
| elif not isinstance(access, str) or len(access) != groups or access.strip("yn"): | ||
| raise CommandsError("bad_commands") | ||
| return { | ||
| "plugin": plugin, | ||
| "name": _text(raw.get("name"), NAME_MAX, required=True), | ||
| "aliases": [_text(a, NAME_MAX, required=True) for a in aliases], | ||
| "description": _text(raw.get("description"), TEXT_MAX), | ||
| "usage": _text(raw.get("usage"), TEXT_MAX), | ||
| "permission": permission, | ||
| "shadowed": raw["shadowed"], | ||
| "access": access, | ||
| } | ||
|
|
||
|
|
||
| def store(payload) -> dict: | ||
| if not isinstance(payload, dict): | ||
| raise CommandsError("bad_commands") | ||
| groups, plugins, commands = payload.get("groups"), payload.get("plugins"), payload.get("commands") | ||
| if (not isinstance(groups, list) or not isinstance(plugins, list) or not isinstance(commands, list) | ||
| or len(groups) > GROUPS_MAX or len(plugins) > PLUGINS_MAX or len(commands) > COMMANDS_MAX): | ||
| raise CommandsError("bad_commands") | ||
| group_names = [_text(g, NAME_MAX * 2, required=True).lower() for g in groups] | ||
| plugin_rows = [] | ||
| for raw in plugins: | ||
| if not isinstance(raw, dict): | ||
| raise CommandsError("bad_commands") | ||
| plugin_rows.append({"name": _text(raw.get("name"), NAME_MAX, required=True), | ||
| "version": _text(raw.get("version"), NAME_MAX)}) | ||
| names = {p["name"] for p in plugin_rows} | ||
| command_rows = [_command(raw, names, len(group_names)) for raw in commands] | ||
| generated_at = payload.get("generated_at") | ||
| if not isinstance(generated_at, int) or isinstance(generated_at, bool) or generated_at <= 0: | ||
| raise CommandsError("bad_commands") | ||
| server = _text(payload.get("server"), 32, required=True) | ||
| snapshot_hash = _text(payload.get("hash"), 128) | ||
| body = json.dumps({"groups": group_names, "plugins": plugin_rows, "commands": command_rows}, | ||
| separators=(",", ":")) | ||
| with connect() as conn: | ||
| conn.execute( | ||
| """ | ||
| INSERT INTO lp_commands (id, server, hash, generated_at, received_at, payload) | ||
| VALUES (1, ?, ?, ?, ?, ?) | ||
| ON CONFLICT(id) DO UPDATE SET server = excluded.server, hash = excluded.hash, | ||
| generated_at = excluded.generated_at, received_at = excluded.received_at, | ||
| payload = excluded.payload | ||
| """, | ||
| (server, snapshot_hash, generated_at, mirror._iso(), body), | ||
| ) | ||
| conn.commit() | ||
| return {"ok": True, "commands": len(command_rows)} | ||
|
|
||
|
|
||
| def _kinds(tracks: list[dict]) -> dict[str, tuple[str, str]]: | ||
| """A track's last group is a staff rank; the groups before it are its steps.""" | ||
| out: dict[str, tuple[str, str]] = {} | ||
| for track in tracks: | ||
| if not track["groups"]: | ||
| continue | ||
| *steps, last = track["groups"] | ||
| out[last] = ("staff", track["name"]) | ||
| for step in steps: | ||
| out.setdefault(step, ("step", track["name"])) | ||
| return out | ||
|
|
||
|
|
||
| def view(viewer: dict) -> dict: | ||
| now = int(time.time()) | ||
| with connect() as conn: | ||
| row = conn.execute( | ||
| "SELECT server, generated_at, received_at, payload FROM lp_commands WHERE id = 1").fetchone() | ||
| if row is None: | ||
| return {"status": None, "groups": [], "plugins": []} | ||
| groups = mirror.load_groups(conn) | ||
| counts = mirror.group_counts(conn) | ||
| tracks = mirror.load_tracks(conn) | ||
| data = json.loads(row["payload"]) | ||
| kinds = _kinds(tracks) | ||
| listed = [] | ||
| for index, name in enumerate(data["groups"]): | ||
| group = groups.get(name, {"name": name, "display_name": None, "weight": None, "nodes": []}) | ||
| kind, track = kinds.get(name, ("rank", None)) | ||
| listed.append((index, { | ||
| "name": name, | ||
| "display_name": group["display_name"], | ||
| "weight": group["weight"], | ||
| "members": counts.get(name, 0), | ||
| "kind": kind, | ||
| "track": track, | ||
| "parents": mirror.parents(group, now), | ||
| "nodes": [n for n in group["nodes"] if nodes.kind(n["key"]) == "permission"], | ||
| })) | ||
| listed.sort(key=lambda item: (KIND_ORDER[item[1]["kind"]], item[1]["weight"] or 0, item[1]["name"])) | ||
| order = [index for index, _ in listed] | ||
| plugins: dict[str, dict] = {p["name"]: {**p, "commands": []} for p in data["plugins"]} | ||
| for command in data["commands"]: | ||
| access = command["access"] | ||
| plugins[command["plugin"]]["commands"].append({ | ||
| **{k: v for k, v in command.items() if k != "plugin"}, | ||
| "access": None if access is None else "".join(access[i] for i in order), | ||
| }) | ||
| for plugin in plugins.values(): | ||
| plugin["commands"].sort(key=lambda c: (c["name"], c["shadowed"])) | ||
| received = mirror._age(row["received_at"], time.time()) | ||
| return { | ||
| "status": { | ||
| "server": row["server"], | ||
| "generated_at": row["generated_at"], | ||
| "received_at": row["received_at"], | ||
| "fresh": received is not None and received <= FRESH_SECONDS, | ||
| }, | ||
| "groups": [group for _, group in listed], | ||
| "plugins": sorted((p for p in plugins.values() if p["commands"]), key=lambda p: p["name"].lower()), | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import pytest | ||
|
|
||
| from src.luckperms import commands | ||
| from src.luckperms.test_luckperms import PLUGIN, SECONDARY, app, bridge, client, env, staff # noqa: F401 | ||
|
|
||
|
|
||
| def command(name, plugin="Essentials", permission=None, access=None, **extra): | ||
| return {"plugin": plugin, "name": name, "aliases": extra.get("aliases", []), | ||
| "description": extra.get("description"), "usage": extra.get("usage"), "permission": permission, | ||
| "shadowed": extra.get("shadowed", False), "access": access} | ||
|
|
||
|
|
||
| def catalogue(**overrides): | ||
| # Groups in the plugin's (alphabetical) order; access follows it. | ||
| payload = { | ||
| "server": "main", | ||
| "generated_at": 1792076432, | ||
| "hash": "c1", | ||
| "groups": ["commoner", "default", "helper", "helper_inactive", "helper_player", "noble", "staff"], | ||
| "plugins": [{"name": "Essentials", "version": "2.22.0"}, {"name": "LibsDisguises", "version": None}, | ||
| {"name": "Unused", "version": "1"}], | ||
| "commands": [ | ||
| command("me", permission="essentials.me", access="yyyyyyy", aliases=["eme"], | ||
| description="Describes an action."), | ||
| command("kick", permission="essentials.kick", access="nnynnny"), | ||
| command("disguise", plugin="LibsDisguises", permission="libsdisguises.seecmd.disguise", | ||
| access="yyyyyyy", usage="/disguise <type>"), | ||
| command("me", plugin="LibsDisguises", shadowed=True), | ||
| ], | ||
| } | ||
| payload.update(overrides) | ||
| return payload | ||
|
|
||
|
|
||
| def test_plugin_route_needs_the_primary_key(app): | ||
| plugin = client(app) | ||
| assert plugin.put("/luckperms/plugin/commands", json=catalogue()).status_code == 401 | ||
| assert plugin.put("/luckperms/plugin/commands", headers=SECONDARY, json=catalogue()).status_code == 403 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("change", [ | ||
| {"groups": "x"}, | ||
| {"groups": [""]}, | ||
| {"plugins": ["x"]}, | ||
| {"commands": [command("me", plugin="Nobody")]}, | ||
| {"commands": [command("me", permission="essentials.me", access="yy")]}, | ||
| {"commands": [command("me", permission="essentials.me", access="yyyyyyx")]}, | ||
| {"commands": [command("me", access="yyyyyyy")]}, | ||
| {"commands": [{**command("me"), "shadowed": "no"}]}, | ||
| {"commands": [{**command("me"), "aliases": "eme"}]}, | ||
| {"commands": [command("x" * 65)]}, | ||
| {"commands": ["me"]}, | ||
| {"generated_at": True}, | ||
| {"server": None}, | ||
| ]) | ||
| def test_malformed_lists_are_refused(app, change): | ||
| response = client(app).put("/luckperms/plugin/commands", headers=PLUGIN, json=catalogue(**change)) | ||
| assert response.status_code == 400 and response.json()["detail"] == "bad_commands" | ||
| assert client(app).put("/luckperms/plugin/commands", headers=PLUGIN, json=[1]).status_code == 400 | ||
|
|
||
|
|
||
| def test_staff_see_commands_with_groups_in_rank_order(app, bridge, staff): | ||
| mod = client(app, staff["mod"]) | ||
| assert mod.get("/admin/luckperms/commands").json() == {"status": None, "groups": [], "plugins": []} | ||
| assert bridge.put("/luckperms/plugin/commands", headers=PLUGIN, json=catalogue()).json() == { | ||
| "ok": True, "commands": 4} | ||
|
|
||
| data = mod.get("/admin/luckperms/commands").json() | ||
|
|
||
| assert data["status"]["server"] == "main" and data["status"]["generated_at"] == 1792076432 | ||
| assert data["status"]["fresh"] is True | ||
| assert [(g["name"], g["kind"], g["track"]) for g in data["groups"]] == [ | ||
| ("default", "rank", None), ("commoner", "rank", None), ("noble", "rank", None), | ||
| ("helper", "staff", "helper"), ("staff", "staff", "staff"), | ||
| ("helper_player", "step", "helper"), ("helper_inactive", "step", "helper"), | ||
| ] | ||
| helper = data["groups"][3] | ||
| assert helper["parents"] == ["helper_inactive"] and helper["members"] == 0 | ||
| assert [n["key"] for n in helper["nodes"]] == ["essentials.kick"] | ||
| staff_group = data["groups"][4] | ||
| assert staff_group["members"] == 2 and staff_group["weight"] == 200 | ||
| assert [p["name"] for p in data["plugins"]] == ["Essentials", "LibsDisguises"] | ||
| essentials = data["plugins"][0] | ||
| assert essentials["version"] == "2.22.0" | ||
| assert [c["name"] for c in essentials["commands"]] == ["kick", "me"] | ||
| # default, commoner, noble, helper, staff, helper_player, helper_inactive | ||
| assert essentials["commands"][0]["access"] == "nnnyynn" | ||
| assert essentials["commands"][1] == {"name": "me", "aliases": ["eme"], "description": "Describes an action.", | ||
| "usage": None, "permission": "essentials.me", "shadowed": False, | ||
| "access": "yyyyyyy"} | ||
| shadowed = data["plugins"][1]["commands"][1] | ||
| assert shadowed["shadowed"] is True and shadowed["access"] is None | ||
|
|
||
|
|
||
| def test_groups_missing_from_the_mirror_still_show(app, staff, env): | ||
| plugin = client(app) | ||
| payload = catalogue(groups=["ghost"], commands=[command("me", permission="essentials.me", access="y")]) | ||
| assert plugin.put("/luckperms/plugin/commands", headers=PLUGIN, json=payload).status_code == 200 | ||
| data = client(app, staff["mod"]).get("/admin/luckperms/commands").json() | ||
| assert data["groups"] == [{"name": "ghost", "display_name": None, "weight": None, "members": 0, "kind": "rank", | ||
| "track": None, "parents": [], "nodes": []}] | ||
|
|
||
|
|
||
| def test_old_lists_are_marked_stale(app, staff, env): | ||
| assert client(app).put("/luckperms/plugin/commands", headers=PLUGIN, json=catalogue()).status_code == 200 | ||
| with env.connect() as conn: | ||
| conn.execute("UPDATE lp_commands SET received_at = '2026-01-01T00:00:00Z'") | ||
| conn.commit() | ||
| assert client(app, staff["mod"]).get("/admin/luckperms/commands").json()["status"]["fresh"] is False | ||
|
|
||
|
|
||
| def test_players_cannot_read_commands(app, env): | ||
| from src.luckperms.test_luckperms import account | ||
| player = account(env, "pat") | ||
| assert client(app, player).get("/admin/luckperms/commands").status_code == 403 | ||
| assert client(app).get("/admin/luckperms/commands").status_code == 401 | ||
|
|
||
|
|
||
| def test_empty_tracks_are_ignored(): | ||
| assert commands._kinds([{"name": "empty", "groups": []}, {"name": "t", "groups": ["a", "b"]}]) == { | ||
| "b": ("staff", "t"), "a": ("step", "t")} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import AdminColumn from "../../components/admin/AdminColumn"; | ||
| import CommandsOverview from "../../components/admin/commands/CommandsOverview"; | ||
| import { parseCommandsState } from "@/lib/admin/commands"; | ||
|
|
||
| type SearchParams = Promise<Record<string, string | string[] | undefined>>; | ||
|
|
||
| export default async function AdminCommandsPage({ searchParams }: { searchParams: SearchParams }) { | ||
| const params = new URLSearchParams(); | ||
| for (const [key, value] of Object.entries(await searchParams)) { | ||
| const first = Array.isArray(value) ? value[0] : value; | ||
| if (first !== undefined) params.set(key, first); | ||
| } | ||
| return ( | ||
| <AdminColumn> | ||
| <CommandsOverview initialState={parseCommandsState(params)} /> | ||
| </AdminColumn> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
Reject duplicate group names in the published snapshot.
storelower-cases group names and does not check them for duplicates. Two names that differ only in case, or a repeated name, produce duplicate entries inlisted. The frontend usesgroup.nameas the React key and finds groups by name. The matrix columns and the role selection then become ambiguous. Commands with duplicate names in one plugin cause the same problem, because the row ids${plugin.name}/${command.name}collide. The only difference between such commands isshadowed.Reject duplicate group names in
store.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents