Finish roles
This commit is contained in:
parent
8580049cae
commit
32ae020f95
|
@ -1,5 +0,0 @@
|
|||
user:
|
||||
|
||||
moderator:
|
||||
|
||||
admin:
|
|
@ -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:
|
||||
|
|
|
@ -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<T = unknown> {
|
||||
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.";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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}`;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<Role, TRole>;
|
|
@ -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<Role, TRole>();
|
||||
|
||||
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<Role, TRole>;
|
||||
};
|
||||
|
||||
let config: Record<Role, TRole>;
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue