diff --git a/packages/core/package.json b/packages/core/package.json index dbe3622860..dc39a97d80 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -33,8 +33,8 @@ "@koishijs/i18n-utils": "^1.0.0", "@koishijs/utils": "^7.0.4", "@minatojs/core": "^2.3.2", - "@satorijs/core": "^2.6.0", - "cordis": "^2.8.6", + "@satorijs/core": "^2.6.1", + "cordis": "^2.8.7", "cosmokit": "^1.4.4", "fastest-levenshtein": "^1.0.16" } diff --git a/packages/core/src/permission.ts b/packages/core/src/permission.ts index 379933b1b4..130235f529 100644 --- a/packages/core/src/permission.ts +++ b/packages/core/src/permission.ts @@ -17,6 +17,18 @@ declare module '@satorijs/core' { class DAG { store: Map[]>> = new Map() + define(name: string) { + this.delete(name) + this.store.set(name, new Map()) + } + + delete(name: string) { + this.store.delete(name) + for (const map of this.store.values()) { + map.delete(name) + } + } + link(source: string, target: string, condition: Computed) { if (!this.store.has(source)) this.store.set(source, new Map()) const map = this.store.get(source) @@ -66,10 +78,14 @@ export class Permissions { }) } + private get caller(): Context { + return this[Context.current] + } + provide(name: string, callback: Permissions.ProvideCallback) { this.#providers[name] = callback - this[Context.current]?.collect('permission-provide', () => { - delete this.#providers[name] + return this.caller?.collect('permission-provide', () => { + return delete this.#providers[name] }) } @@ -94,29 +110,34 @@ export class Permissions { this.inherit(`authority.${value}`, name) } + define(name: string, inherits: string[]) { + this.#inherits.define(name) + this.ctx.emit('internal/permission') + for (const permission of inherits) { + this.inherit(name, permission) + } + return this.caller?.collect('permission-define', () => { + this.#inherits.delete(name) + this.ctx.emit('internal/permission') + }) + } + inherit(child: string, parent: string, condition: Computed = true) { this.#inherits.link(parent, child, condition) - this.ctx.emit('internal/permission') - this[Context.current]?.collect('permission-inherit', () => { + return this.caller?.collect('permission-inherit', () => { this.#inherits.unlink(parent, child, condition) - this.ctx.emit('internal/permission') }) } depend(dependent: string, dependency: string, condition: Computed = true) { this.#depends.link(dependent, dependency, condition) - this.ctx.emit('internal/permission') - this[Context.current]?.collect('permission-depend', () => { + return this.caller?.collect('permission-depend', () => { this.#depends.unlink(dependent, dependency, condition) - this.ctx.emit('internal/permission') }) } list() { - return [...new Set([ - ...this.#inherits.store.keys(), - ...this.#depends.store.keys(), - ])] + return [...this.#inherits.store.keys()] } async test(x: string[], y: Iterable, session: Partial = {}) { diff --git a/packages/koishi/package.json b/packages/koishi/package.json index 927215c8fd..7c86fa6e64 100644 --- a/packages/koishi/package.json +++ b/packages/koishi/package.json @@ -46,7 +46,7 @@ "@koishijs/core": "4.14.0", "@koishijs/loader": "4.1.0", "@koishijs/utils": "^7.0.4", - "@satorijs/satori": "^2.6.0", + "@satorijs/satori": "^2.6.1", "cac": "^6.7.14", "kleur": "^4.1.5", "ns-require": "^1.1.4"