From 1811bdbfe6b0a9885cc04352c90eb85435c7ca1b Mon Sep 17 00:00:00 2001 From: Shigma Date: Mon, 1 Jan 2024 16:15:21 +0800 Subject: [PATCH] feat(core): rename permissions to colon separated --- packages/core/src/command/command.ts | 10 +++++----- packages/core/src/command/index.ts | 6 +++--- packages/core/src/command/validate.ts | 22 +++++++++++----------- packages/core/src/permission.ts | 13 ++++++++----- plugins/common/help/src/index.ts | 6 +++--- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/packages/core/src/command/command.ts b/packages/core/src/command/command.ts index e49b1a472..9f7065b92 100644 --- a/packages/core/src/command/command.ts +++ b/packages/core/src/command/command.ts @@ -79,7 +79,7 @@ export class Command< slash: true, ...config, }) - this.config.inherits ??= [`authority.${config?.authority ?? 1}`] + this.config.permissions ??= [`authority:${config?.authority ?? 1}`] this._registerAlias(name) ctx.$commander._commandList.push(this) } @@ -237,9 +237,9 @@ export class Command< desc = args.shift() as string } const config = { ...args[0] as Argv.OptionConfig } - config.inherits ??= [`authority.${config.authority ?? 0}`] + config.permissions ??= [`authority:${config.authority ?? 0}`] this._createOption(name, desc, config) - this.caller.collect('command.option', () => this.removeOption(name)) + this.caller.collect('option', () => this.removeOption(name)) return this } @@ -388,8 +388,8 @@ export namespace Command { } export const Config: Schema = Schema.object({ - inherits: Schema.array(String).role('table').default(['authority.1']).description('权限继承。'), - depends: Schema.array(String).role('table').description('权限依赖。'), + permissions: Schema.array(String).role('perms').default(['authority:1']).description('权限继承。'), + dependencies: Schema.array(String).role('perms').description('权限依赖。'), slash: Schema.boolean().description('启用斜线指令功能。').default(true), checkUnknown: Schema.boolean().description('是否检查未知选项。').default(false).hidden(), checkArgCount: Schema.boolean().description('是否检查参数数量。').default(false).hidden(), diff --git a/packages/core/src/command/index.ts b/packages/core/src/command/index.ts index 76c6e27b7..4cfd602b6 100644 --- a/packages/core/src/command/index.ts +++ b/packages/core/src/command/index.ts @@ -122,7 +122,7 @@ export class Commander extends Map { suffix: session.text('internal.suggest-command'), filter: (name) => { name = this.resolve(name)!.name - return ctx.permissions.test(`command.${name}`, session, cache) + return ctx.permissions.test(`command:${name}`, session, cache) }, }) if (!name) return next() @@ -133,8 +133,8 @@ export class Commander extends Map { ctx.schema.extend('command', Command.Config, 1000) ctx.schema.extend('command-option', Schema.object({ - inherits: Schema.array(String).role('table').default(['authority.0']).description('权限继承。'), - depends: Schema.array(String).role('table').description('权限依赖。'), + permissions: Schema.array(String).role('perms').default(['authority:0']).description('权限继承。'), + dependencies: Schema.array(String).role('perms').description('权限依赖。'), }), 1000) ctx.on('ready', () => { diff --git a/packages/core/src/command/validate.ts b/packages/core/src/command/validate.ts index 6b55b7fbb..5316f8a1e 100644 --- a/packages/core/src/command/validate.ts +++ b/packages/core/src/command/validate.ts @@ -3,32 +3,32 @@ import { Context } from '../context' import { Argv } from './parser' export default function validate(ctx: Context) { - ctx.permissions.define('command.(name)', { + ctx.permissions.define('command:(name)', { depends: ({ name }) => { const command = ctx.$commander.get(name) if (!command) return - const depends = [...command.config.depends ?? []] - if (command.parent) depends.push(`command.${command.parent.name}`) + const depends = [...command.config.dependencies ?? []] + if (command.parent) depends.push(`command:${command.parent.name}`) return depends }, inherits: ({ name }) => { - return ctx.$commander.get(name)?.config.inherits + return ctx.$commander.get(name)?.config.permissions }, list: () => { - return ctx.$commander._commandList.map(command => `command.${command.name}`) + return ctx.$commander._commandList.map(command => `command:${command.name}`) }, }) - ctx.permissions.define('command.(name).option.(name2)', { + ctx.permissions.define('command:(name):option:(name2)', { depends: ({ name, name2 }) => { - return ctx.$commander.get(name)?._options[name2]?.depends + return ctx.$commander.get(name)?._options[name2]?.dependencies }, inherits: ({ name, name2 }) => { - return ctx.$commander.get(name)?._options[name2]?.inherits + return ctx.$commander.get(name)?._options[name2]?.permissions }, list: () => { return ctx.$commander._commandList.flatMap(command => { - return Object.keys(command._options).map(name => `command.${command.name}.option.${name}`) + return Object.keys(command._options).map(name => `command:${command.name}:option:${name}`) }) }, }) @@ -43,10 +43,10 @@ export default function validate(ctx: Context) { } // check permissions - const permissions = [`command.${command.name}`] + const permissions = [`command:${command.name}`] for (const option of Object.values(command._options)) { if (option.name in options) { - permissions.push(`command.${command.name}.option.${option.name}`) + permissions.push(`command:${command.name}:option:${option.name}`) } } if (!await ctx.permissions.test(permissions, session)) { diff --git a/packages/core/src/permission.ts b/packages/core/src/permission.ts index 53ddf2129..3b9881da6 100644 --- a/packages/core/src/permission.ts +++ b/packages/core/src/permission.ts @@ -33,8 +33,8 @@ export namespace Permissions { export interface Config { authority?: number - inherits?: string[] - depends?: string[] + permissions?: string[] + dependencies?: string[] } } @@ -45,8 +45,11 @@ export class Permissions { defineProperty(this, Context.current, ctx) ctx.alias('permissions', ['perms']) - this.provide('authority.(value)', ({ value }, { user }: Partial>) => { - return !user || user.authority >= +value + this.define('authority:(value)', { + check: ({ value }, { user }: Partial>) => { + return !user || user.authority >= +value + }, + list: () => Array(5).fill(0).map((_, i) => `authority:${i}`), }) this.provide('(name)', ({ name }, session) => { @@ -69,7 +72,7 @@ export class Permissions { ...options, match: createMatch(pattern), } - if (!pattern.includes('(')) entry.list = () => [pattern] + if (!pattern.includes('(')) entry.list ||= () => [pattern] return this.caller.effect(() => { this.store.push(entry) return () => remove(this.store, entry) diff --git a/plugins/common/help/src/index.ts b/plugins/common/help/src/index.ts index 6c96f16e4..f20ee3065 100644 --- a/plugins/common/help/src/index.ts +++ b/plugins/common/help/src/index.ts @@ -138,7 +138,7 @@ export function apply(ctx: Context, config: Config) { suffix: session.text('internal.suggest-command'), filter: (name) => { name = $.resolve(name)!.name - return ctx.permissions.test(`command.${name}`, session, cache) + return ctx.permissions.test(`command:${name}`, session, cache) }, }) return $.resolve(name) @@ -161,7 +161,7 @@ export function apply(ctx: Context, config: Config) { const command = await inferCommand(target, session) if (!command) return - if (!await ctx.permissions.test(`command.${command.name}`, session)) { + if (!await ctx.permissions.test(`command:${command.name}`, session)) { return session.text('internal.low-authority') } return showHelp(command, session, options) @@ -187,7 +187,7 @@ async function formatCommands(path: string, session: Session<'authority'>, child children = Array.from(getCommands(session, children, options.showHidden)) // Step 2: filter commands by permission children = (await Promise.all(children.map(async (command) => { - return [command, await session.app.permissions.test(`command.${command.name}`, session, cache)] as const + return [command, await session.app.permissions.test(`command:${command.name}`, session, cache)] as const }))).filter(([, result]) => result).map(([command]) => command) // Step 3: sort commands by name children.sort((a, b) => a.displayName > b.displayName ? 1 : -1)