Skip to content

Commit

Permalink
feat(core): support perms.define() for disposable perms
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 7, 2023
1 parent 768f670 commit 6024676
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
45 changes: 33 additions & 12 deletions packages/core/src/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ declare module '@satorijs/core' {
class DAG {
store: Map<string, Map<string, Computed<boolean>[]>> = 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<boolean>) {
if (!this.store.has(source)) this.store.set(source, new Map())
const map = this.store.get(source)
Expand Down Expand Up @@ -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]
})
}

Expand All @@ -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<boolean> = 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<boolean> = 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<string>, session: Partial<Session> = {}) {
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6024676

Please sign in to comment.