From 32ae020f9588b341304b6f9c4ec4bdf56536ad7f Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Fri, 1 Dec 2023 22:57:37 -0500 Subject: [PATCH] Finish roles --- config/permissions.yml | 5 -- config/roles.yml | 3 +- src/commands/CommandHandler.ts | 42 +++++++++--- src/commands/commands/utility/math.ts | 2 - src/commands/commands/utility/role.ts | 16 +++++ src/commands/index.ts | 3 +- src/permissions/default.ts | 39 +++++++++++ src/permissions/index.ts | 96 ++++++++++++--------------- src/util/permission.ts | 0 9 files changed, 133 insertions(+), 73 deletions(-) delete mode 100644 config/permissions.yml create mode 100644 src/commands/commands/utility/role.ts create mode 100644 src/permissions/default.ts delete mode 100644 src/util/permission.ts diff --git a/config/permissions.yml b/config/permissions.yml deleted file mode 100644 index 8957f56..0000000 --- a/config/permissions.yml +++ /dev/null @@ -1,5 +0,0 @@ -user: - -moderator: - -admin: diff --git a/config/roles.yml b/config/roles.yml index d0597dc..4292148 100644 --- a/config/roles.yml +++ b/config/roles.yml @@ -8,6 +8,7 @@ NONE: - cosmic.command.id - cosmic.command.math - cosmic.command.memory + - cosmic.command.role MODERATOR: displayName: Moderator inherits: NONE @@ -19,7 +20,7 @@ ADMINISTRATOR: displayName: Administrator inherits: MODERATOR permissions: - - cosmic.commandGroup. + - cosmic.commandGroup.* OWNER: displayName: Owner permissions: diff --git a/src/commands/CommandHandler.ts b/src/commands/CommandHandler.ts index 1852468..ad46f19 100644 --- a/src/commands/CommandHandler.ts +++ b/src/commands/CommandHandler.ts @@ -5,6 +5,8 @@ import { Command } from "./Command"; import { CommandGroup } from "./CommandGroup"; import { Prefix } from "./Prefix"; import { createInventory, readInventory } from "../data/inventory"; +import { hasPermission } from "../permissions"; +import { Logger } from "../util/Logger"; export interface CommandMessage { m: "command"; @@ -40,6 +42,8 @@ export class CommandHandler { } ); + public static logger = new Logger("Command Handler"); + public static addCommandGroup(group: CommandGroup) { this.commandGroups.push(group); } @@ -54,15 +58,13 @@ export class CommandHandler { if (!user) { let role = Role.NONE; - if (agent.platform == "console" && msg.p._id == "console") { - await createUser({ - id: msg.p._id, - platform: agent.platform, - platformId: msg.p.platformId, - name: msg.p.name, - role: Role.NONE - }); - } + await createUser({ + id: msg.p._id, + platform: agent.platform, + platformId: msg.p.platformId, + name: msg.p.name, + role: role + }); user = await readUser(msg.p._id); if (!user) @@ -107,11 +109,13 @@ export class CommandHandler { if (!usedAlias) return; let usedCommand: Command | undefined; + let usedCommandGroup: CommandGroup | undefined; for (const group of this.commandGroups) { for (const command of group.commands) { if (command.aliases.includes(usedAlias)) { usedCommand = command; + usedCommandGroup = group; break; } } @@ -119,6 +123,24 @@ export class CommandHandler { if (!usedCommand) return; + let permissionNode = `cosmic.command.${usedCommand.id}`; + // this.logger.debug("Role:", user.role); + // this.logger.debug("Node:", permissionNode); + let groupNode; + + if (usedCommandGroup) + groupNode = `cosmic.commandGroup.${usedCommandGroup.id}`; + + if (groupNode) { + if (!hasPermission(user.role, groupNode)) { + if (!hasPermission(user.role, permissionNode)) + return "No permission"; + } + } else { + if (!hasPermission(user.role, permissionNode)) + return "No permission"; + } + (msg as CommandMessage).user = user; (msg as CommandMessage).inventory = inventory; @@ -126,7 +148,7 @@ export class CommandHandler { const out = usedCommand.callback(msg as CommandMessage, agent); if (out) return out; } catch (err) { - console.error(err); + this.logger.error(err); return "An error has occurred."; } } diff --git a/src/commands/commands/utility/math.ts b/src/commands/commands/utility/math.ts index f0d7ff5..d675555 100644 --- a/src/commands/commands/utility/math.ts +++ b/src/commands/commands/utility/math.ts @@ -10,11 +10,9 @@ export const math = new Command( (msg, agent) => { try { const argcat = msg.argv.slice(1, msg.argv.length).join(" "); - console.log(argcat); const answer = evaluate(argcat); return `Answer: ${answer}`; } catch (err) { - console.error(err); return `Invalid expression: ${err}`; } } diff --git a/src/commands/commands/utility/role.ts b/src/commands/commands/utility/role.ts new file mode 100644 index 0000000..d02cb42 --- /dev/null +++ b/src/commands/commands/utility/role.ts @@ -0,0 +1,16 @@ +import { getRole } from "../../../permissions"; +import { MPPAgent } from "../../../services/mpp"; +import { Command } from "../../Command"; + +export const role = new Command( + "role", + ["role"], + "get your role bozo", + "role", + (msg, agent) => { + const role = getRole(msg.user.role); + if (!role) return `Your role: ${msg.user.role} (this role is broken)`; + return `Your role: ${role.displayName} [${msg.user.role}]`; + }, + false +); diff --git a/src/commands/index.ts b/src/commands/index.ts index 8585161..788487e 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -10,6 +10,7 @@ import { magic8ball } from "./commands/fun/magic8ball"; import { cursor } from "./commands/utility/cursor"; import { inventory } from "./commands/economy/inventory"; import { color } from "./commands/utility/color"; +import { role } from "./commands/utility/role"; export function loadCommands() { // cringe @@ -26,6 +27,6 @@ export function loadCommands() { CommandHandler.addCommandGroup(fun); const utility = new CommandGroup("utility", "🔨 Utility"); - utility.addCommands([math, memory, id, msg, cursor, color]); + utility.addCommands([math, memory, id, msg, cursor, color, role]); CommandHandler.addCommandGroup(utility); } diff --git a/src/permissions/default.ts b/src/permissions/default.ts new file mode 100644 index 0000000..1904f93 --- /dev/null +++ b/src/permissions/default.ts @@ -0,0 +1,39 @@ +import { Role } from "@prisma/client"; +import { TRole } from "."; + +export const defaultConfig = { + NONE: { + displayName: "None", + permissions: [ + "cosmic.commandGroup.general", + + "cosmic.command.inventory", + + "cosmic.command.magic8ball", + + "cosmic.command.color", + "cosmic.command.id", + "cosmic.command.math", + "cosmic.command.memory", + "cosmic.command.role" + ] + }, + MODERATOR: { + displayName: "Moderator", + inherits: "NONE", + permissions: [ + "cosmic.commandGroup.economy", + "cosmic.command.msg", + "cosmic.command.memory" + ] + }, + ADMINISTRATOR: { + displayName: "Administrator", + inherits: "MODERATOR", + permissions: ["cosmic.commandGroup.*"] + }, + OWNER: { + displayName: "Owner", + permissions: ["*"] + } +} as Record; diff --git a/src/permissions/index.ts b/src/permissions/index.ts index b8d71d2..3c86b0b 100644 --- a/src/permissions/index.ts +++ b/src/permissions/index.ts @@ -1,5 +1,9 @@ import { Role } from "@prisma/client"; import { loadConfig } from "../util/config"; +import { Logger } from "../util/Logger"; +import { defaultConfig } from "./default"; + +const logger = new Logger("Permission Handler"); /** * Check two permission strings to see if they match @@ -12,16 +16,23 @@ import { loadConfig } from "../util/config"; * @param node2 Permission string */ export function handlePermission(node1: string, node2: string) { + // Split strings into arrays of nodes + // "thing.thing2" -> ["thing", "thing2"] const hierarchy1 = node1.split("."); const hierarchy2 = node2.split("."); + // Check nodes in order for (let i = 0; i < hierarchy1.length; i++) { - if (i == hierarchy1.length - 1 || i == hierarchy2.length - 1) { - if (hierarchy1[i] == hierarchy2[i]) return true; - } else { - if (hierarchy1[i] == hierarchy2[i]) continue; + if (hierarchy1[i] == hierarchy2[i]) { + // Last node? + if (i == hierarchy1.length - 1 || i == hierarchy2.length - 1) { + return true; + } else { + continue; + } } + // Wildcard? if (hierarchy1[i] == "*") return true; if (hierarchy2[i] == "*") return true; @@ -31,72 +42,49 @@ export function handlePermission(node1: string, node2: string) { return false; } -console.log(handlePermission("*", "*")); - /** * Check if a group has a permission * @param role Prisma role * @param permission Permission to check */ export function hasPermission(role: Role, permission: string) { - // TODO hasPermission - return; + const roleVal = roles.get(role); + if (!roleVal) return false; + + if (roleVal.inherits) { + // logger.debug( + // "Detected role inheritence from " + role + " to " + roleVal.inherits + // ); + if (hasPermission(roleVal.inherits, permission)) return true; + } + + for (const perm of roleVal.permissions) { + // logger.debug("Checking " + permission + " and " + perm); + if (handlePermission(permission, perm)) return true; + } + + return false; } export const roles = new Map(); export type TRole = { displayName: string; + inherits?: Role; permissions: string[]; -} & ( - | { - permissions: string[]; - } - | { - inherits: Role; - permissions?: string[]; - } -); - -let defaultConfig = { - NONE: { - displayName: "None", - permissions: [ - "cosmic.commandGroup.general", - - "cosmic.command.inventory", - - "cosmic.command.magic8ball", - - "cosmic.command.color", - "cosmic.command.id", - "cosmic.command.math", - "cosmic.command.memory" - ] - }, - MODERATOR: { - displayName: "Moderator", - inherits: "NONE", - permissions: [ - "cosmic.commandGroup.economy", - "cosmic.command.msg", - "cosmic.command.memory" - ] - }, - ADMINISTRATOR: { - displayName: "Administrator", - inherits: "MODERATOR", - permissions: ["cosmic.commandGroup."] - }, - OWNER: { - displayName: "Owner", - permissions: ["*"] - } -} as Record; +}; let config: Record; export function loadRoleConfig() { config = loadConfig("config/roles.yml", defaultConfig); - console.log(config); + + roles.set("NONE", config.NONE); + roles.set("MODERATOR", config.MODERATOR); + roles.set("ADMINISTRATOR", config.ADMINISTRATOR); + roles.set("OWNER", config.OWNER); +} + +export function getRole(role: Role) { + return roles.get(role); } diff --git a/src/util/permission.ts b/src/util/permission.ts deleted file mode 100644 index e69de29..0000000