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.id
|
||||||
- cosmic.command.math
|
- cosmic.command.math
|
||||||
- cosmic.command.memory
|
- cosmic.command.memory
|
||||||
|
- cosmic.command.role
|
||||||
MODERATOR:
|
MODERATOR:
|
||||||
displayName: Moderator
|
displayName: Moderator
|
||||||
inherits: NONE
|
inherits: NONE
|
||||||
|
@ -19,7 +20,7 @@ ADMINISTRATOR:
|
||||||
displayName: Administrator
|
displayName: Administrator
|
||||||
inherits: MODERATOR
|
inherits: MODERATOR
|
||||||
permissions:
|
permissions:
|
||||||
- cosmic.commandGroup.
|
- cosmic.commandGroup.*
|
||||||
OWNER:
|
OWNER:
|
||||||
displayName: Owner
|
displayName: Owner
|
||||||
permissions:
|
permissions:
|
||||||
|
|
|
@ -5,6 +5,8 @@ import { Command } from "./Command";
|
||||||
import { CommandGroup } from "./CommandGroup";
|
import { CommandGroup } from "./CommandGroup";
|
||||||
import { Prefix } from "./Prefix";
|
import { Prefix } from "./Prefix";
|
||||||
import { createInventory, readInventory } from "../data/inventory";
|
import { createInventory, readInventory } from "../data/inventory";
|
||||||
|
import { hasPermission } from "../permissions";
|
||||||
|
import { Logger } from "../util/Logger";
|
||||||
|
|
||||||
export interface CommandMessage<T = unknown> {
|
export interface CommandMessage<T = unknown> {
|
||||||
m: "command";
|
m: "command";
|
||||||
|
@ -40,6 +42,8 @@ export class CommandHandler {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
public static logger = new Logger("Command Handler");
|
||||||
|
|
||||||
public static addCommandGroup(group: CommandGroup) {
|
public static addCommandGroup(group: CommandGroup) {
|
||||||
this.commandGroups.push(group);
|
this.commandGroups.push(group);
|
||||||
}
|
}
|
||||||
|
@ -54,15 +58,13 @@ export class CommandHandler {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
let role = Role.NONE;
|
let role = Role.NONE;
|
||||||
|
|
||||||
if (agent.platform == "console" && msg.p._id == "console") {
|
await createUser({
|
||||||
await createUser({
|
id: msg.p._id,
|
||||||
id: msg.p._id,
|
platform: agent.platform,
|
||||||
platform: agent.platform,
|
platformId: msg.p.platformId,
|
||||||
platformId: msg.p.platformId,
|
name: msg.p.name,
|
||||||
name: msg.p.name,
|
role: role
|
||||||
role: Role.NONE
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
user = await readUser(msg.p._id);
|
user = await readUser(msg.p._id);
|
||||||
if (!user)
|
if (!user)
|
||||||
|
@ -107,11 +109,13 @@ export class CommandHandler {
|
||||||
if (!usedAlias) return;
|
if (!usedAlias) return;
|
||||||
|
|
||||||
let usedCommand: Command | undefined;
|
let usedCommand: Command | undefined;
|
||||||
|
let usedCommandGroup: CommandGroup | undefined;
|
||||||
|
|
||||||
for (const group of this.commandGroups) {
|
for (const group of this.commandGroups) {
|
||||||
for (const command of group.commands) {
|
for (const command of group.commands) {
|
||||||
if (command.aliases.includes(usedAlias)) {
|
if (command.aliases.includes(usedAlias)) {
|
||||||
usedCommand = command;
|
usedCommand = command;
|
||||||
|
usedCommandGroup = group;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,6 +123,24 @@ export class CommandHandler {
|
||||||
|
|
||||||
if (!usedCommand) return;
|
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).user = user;
|
||||||
(msg as CommandMessage).inventory = inventory;
|
(msg as CommandMessage).inventory = inventory;
|
||||||
|
|
||||||
|
@ -126,7 +148,7 @@ export class CommandHandler {
|
||||||
const out = usedCommand.callback(msg as CommandMessage, agent);
|
const out = usedCommand.callback(msg as CommandMessage, agent);
|
||||||
if (out) return out;
|
if (out) return out;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
this.logger.error(err);
|
||||||
return "An error has occurred.";
|
return "An error has occurred.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,11 +10,9 @@ export const math = new Command(
|
||||||
(msg, agent) => {
|
(msg, agent) => {
|
||||||
try {
|
try {
|
||||||
const argcat = msg.argv.slice(1, msg.argv.length).join(" ");
|
const argcat = msg.argv.slice(1, msg.argv.length).join(" ");
|
||||||
console.log(argcat);
|
|
||||||
const answer = evaluate(argcat);
|
const answer = evaluate(argcat);
|
||||||
return `Answer: ${answer}`;
|
return `Answer: ${answer}`;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
|
||||||
return `Invalid expression: ${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 { cursor } from "./commands/utility/cursor";
|
||||||
import { inventory } from "./commands/economy/inventory";
|
import { inventory } from "./commands/economy/inventory";
|
||||||
import { color } from "./commands/utility/color";
|
import { color } from "./commands/utility/color";
|
||||||
|
import { role } from "./commands/utility/role";
|
||||||
|
|
||||||
export function loadCommands() {
|
export function loadCommands() {
|
||||||
// cringe
|
// cringe
|
||||||
|
@ -26,6 +27,6 @@ export function loadCommands() {
|
||||||
CommandHandler.addCommandGroup(fun);
|
CommandHandler.addCommandGroup(fun);
|
||||||
|
|
||||||
const utility = new CommandGroup("utility", "🔨 Utility");
|
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);
|
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 { Role } from "@prisma/client";
|
||||||
import { loadConfig } from "../util/config";
|
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
|
* Check two permission strings to see if they match
|
||||||
|
@ -12,16 +16,23 @@ import { loadConfig } from "../util/config";
|
||||||
* @param node2 Permission string
|
* @param node2 Permission string
|
||||||
*/
|
*/
|
||||||
export function handlePermission(node1: string, node2: string) {
|
export function handlePermission(node1: string, node2: string) {
|
||||||
|
// Split strings into arrays of nodes
|
||||||
|
// "thing.thing2" -> ["thing", "thing2"]
|
||||||
const hierarchy1 = node1.split(".");
|
const hierarchy1 = node1.split(".");
|
||||||
const hierarchy2 = node2.split(".");
|
const hierarchy2 = node2.split(".");
|
||||||
|
|
||||||
|
// Check nodes in order
|
||||||
for (let i = 0; i < hierarchy1.length; i++) {
|
for (let i = 0; i < hierarchy1.length; i++) {
|
||||||
if (i == hierarchy1.length - 1 || i == hierarchy2.length - 1) {
|
if (hierarchy1[i] == hierarchy2[i]) {
|
||||||
if (hierarchy1[i] == hierarchy2[i]) return true;
|
// Last node?
|
||||||
} else {
|
if (i == hierarchy1.length - 1 || i == hierarchy2.length - 1) {
|
||||||
if (hierarchy1[i] == hierarchy2[i]) continue;
|
return true;
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wildcard?
|
||||||
if (hierarchy1[i] == "*") return true;
|
if (hierarchy1[i] == "*") return true;
|
||||||
if (hierarchy2[i] == "*") return true;
|
if (hierarchy2[i] == "*") return true;
|
||||||
|
|
||||||
|
@ -31,72 +42,49 @@ export function handlePermission(node1: string, node2: string) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(handlePermission("*", "*"));
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a group has a permission
|
* Check if a group has a permission
|
||||||
* @param role Prisma role
|
* @param role Prisma role
|
||||||
* @param permission Permission to check
|
* @param permission Permission to check
|
||||||
*/
|
*/
|
||||||
export function hasPermission(role: Role, permission: string) {
|
export function hasPermission(role: Role, permission: string) {
|
||||||
// TODO hasPermission
|
const roleVal = roles.get(role);
|
||||||
return;
|
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 const roles = new Map<Role, TRole>();
|
||||||
|
|
||||||
export type TRole = {
|
export type TRole = {
|
||||||
displayName: string;
|
displayName: string;
|
||||||
|
inherits?: Role;
|
||||||
permissions: string[];
|
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>;
|
let config: Record<Role, TRole>;
|
||||||
|
|
||||||
export function loadRoleConfig() {
|
export function loadRoleConfig() {
|
||||||
config = loadConfig("config/roles.yml", defaultConfig);
|
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