mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-02 04:52:50 +08:00
feat(perm): resolvePermissionContext (admin = super-user)
This commit is contained in:
1 parent
f0c979ce71
commit
7a8666efbb
2 files changed
+37
-1
No files matched your search
@@ -4,7 +4,7 @@ import path from "node:path";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import { createDatabase, type BotDatabase } from "./database.js";
|
import { createDatabase, type BotDatabase } from "./database.js";
|
||||||
import { createPermissionStore } from "./permissions.js";
|
import { createPermissionStore } from "./permissions.js";
|
||||||
import { CAPABILITIES, BASIC_TIER_CAPABILITIES } from "./permissions.js";
|
import { CAPABILITIES, BASIC_TIER_CAPABILITIES, resolvePermissionContext } from "./permissions.js";
|
||||||
|
|
||||||
describe("PermissionStore", () => {
|
describe("PermissionStore", () => {
|
||||||
let dbFile: string;
|
let dbFile: string;
|
||||||
@@ -71,4 +71,20 @@ describe("PermissionStore", () => {
|
|||||||
store.pruneBot("botA");
|
store.pruneBot("botA");
|
||||||
expect(store.getBotAccess("u1")).toEqual(["botB"]);
|
expect(store.getBotAccess("u1")).toEqual(["botB"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("resolvePermissionContext", () => {
|
||||||
|
it("admin gets all capabilities and all bots regardless of stored rows", () => {
|
||||||
|
const store = createPermissionStore(db.db);
|
||||||
|
const ctx = resolvePermissionContext("admin", "u1", store);
|
||||||
|
expect([...ctx.capabilities].sort()).toEqual([...CAPABILITIES].sort());
|
||||||
|
expect(ctx.bots).toBe("all");
|
||||||
|
});
|
||||||
|
it("member reflects stored capabilities + bot access", () => {
|
||||||
|
const store = createPermissionStore(db.db);
|
||||||
|
store.setPermissions("u1", { capabilities: ["player.control"], bots: ["b1"] });
|
||||||
|
const ctx = resolvePermissionContext("member", "u1", store);
|
||||||
|
expect([...ctx.capabilities]).toEqual(["player.control"]);
|
||||||
|
expect(ctx.bots).toEqual(new Set(["b1"]));
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -67,3 +67,23 @@ export function createPermissionStore(db: Database.Database): PermissionStore {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface PermissionContext {
|
||||||
|
capabilities: Set<string>;
|
||||||
|
bots: "all" | Set<string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolvePermissionContext(
|
||||||
|
role: "admin" | "member",
|
||||||
|
userId: string,
|
||||||
|
store: PermissionStore
|
||||||
|
): PermissionContext {
|
||||||
|
if (role === "admin") {
|
||||||
|
return { capabilities: new Set(CAPABILITIES), bots: "all" };
|
||||||
|
}
|
||||||
|
const access = store.getBotAccess(userId);
|
||||||
|
return {
|
||||||
|
capabilities: new Set(store.getCapabilities(userId)),
|
||||||
|
bots: access === "all" ? "all" : new Set(access),
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in new issue
Block a user