Skip to content

Commit

Permalink
feat(core): rename permissions to colon separated
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 1, 2024
1 parent 26b5920 commit 1811bdb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -388,8 +388,8 @@ export namespace Command {
}

export const Config: Schema<Config> = 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(),
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class Commander extends Map<string, Command> {
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()
Expand All @@ -133,8 +133,8 @@ export class Commander extends Map<string, Command> {

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', () => {
Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/command/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
})
},
})
Expand All @@ -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)) {
Expand Down
13 changes: 8 additions & 5 deletions packages/core/src/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export namespace Permissions {

export interface Config {
authority?: number
inherits?: string[]
depends?: string[]
permissions?: string[]
dependencies?: string[]
}
}

Expand All @@ -45,8 +45,11 @@ export class Permissions {
defineProperty(this, Context.current, ctx)
ctx.alias('permissions', ['perms'])

this.provide('authority.(value)', ({ value }, { user }: Partial<Session<'authority'>>) => {
return !user || user.authority >= +value
this.define('authority:(value)', {
check: ({ value }, { user }: Partial<Session<'authority'>>) => {
return !user || user.authority >= +value
},
list: () => Array(5).fill(0).map((_, i) => `authority:${i}`),
})

this.provide('(name)', ({ name }, session) => {
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions plugins/common/help/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 1811bdb

Please sign in to comment.