Skip to content

Commit

Permalink
feat(admin): refactor to new perm API
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 30, 2023
1 parent e23cd7f commit e25a179
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions plugins/admin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $, Context, remove, Schema, Service } from 'koishi'
import command from './command'
import service from './console'
import console from './console'
import zhCN from './locales/zh-CN.yml'

export * from './command'
Expand Down Expand Up @@ -41,7 +41,7 @@ export class Admin extends Service {

ctx.i18n.define('zh-CN', zhCN)
ctx.plugin(command)
ctx.plugin(service)
ctx.plugin(console)

ctx.model.extend('group', {
id: 'unsigned',
Expand All @@ -63,23 +63,32 @@ export class Admin extends Service {
item.count = await this.ctx.database
.select('user', { permissions: { $el: 'group.' + item.id } })
.execute(row => $.count(row.id)) || 0
item.dispose = this.ctx.permissions.define('group.' + item.id, item.permissions)
this.setupGroup(item)
}
for (const item of this.tracks) {
item.dispose = this.track(item.permissions)
this.setupTrack(item)
}
}

private track(permissions: string[]) {
const disposables = permissions.slice(1).map((_, index) => {
return this.ctx.permissions.inherit(permissions[index + 1], permissions[index])
private setupGroup(item: PermGroup) {
item.dispose = this.ctx.permissions.define('(name)', {
inherits: ({ name }) => item.permissions.includes(name) && ['group.' + item.id],
list: () => ['group.' + item.id],
})
}

private setupTrack(item: PermTrack) {
item.dispose = this.ctx.permissions.define('(name)', {
inherits: ({ name }) => {
const index = item.permissions.indexOf(name)
if (index > 0) return [item.permissions[index - 1]]
},
})
return () => disposables.forEach(fn => fn())
}

async createTrack(name: string) {
const item = await this.ctx.database.create('perm_track', { name })
item.dispose = this.track(item.permissions)
this.setupTrack(item)
this.tracks.push(item)
this.ctx.get('console')?.refresh('admin')
return item.id
Expand All @@ -97,7 +106,8 @@ export class Admin extends Service {
async deleteTrack(id: number) {
const index = this.tracks.findIndex(track => track.id === id)
if (index < 0) throw new Error('track not found')
this.tracks.splice(index, 1)
const [item] = this.tracks.splice(index, 1)
item.dispose!()
this.ctx.get('console')?.refresh('admin')
await this.ctx.database.remove('perm_track', id)
}
Expand All @@ -106,16 +116,14 @@ export class Admin extends Service {
const item = this.tracks.find(group => group.id === id)
if (!item) throw new Error('track not found')
item.permissions = permissions
item.dispose!()
item.dispose = this.track(permissions)
await this.ctx.database.set('perm_track', id, { permissions })
this.ctx.get('console')?.refresh('admin')
}

async createGroup(name: string) {
const item = await this.ctx.database.create('group', { name })
item.count = 0
item.dispose = this.ctx.permissions.define('group.' + item.id, [])
this.setupGroup(item)
this.groups.push(item)
this.ctx.get('console')?.refresh('admin')
return item.id
Expand All @@ -133,9 +141,8 @@ export class Admin extends Service {
async deleteGroup(id: number) {
const index = this.groups.findIndex(group => group.id === id)
if (index < 0) throw new Error('group not found')
const item = this.groups[index]!
const [item] = this.groups.splice(index, 1)
item.dispose!()
this.groups.splice(index, 1)
const users = await this.ctx.database.get('user', { permissions: { $el: 'group.' + id } }, ['id', 'permissions'])
for (const user of users) {
remove(user.permissions, 'group.' + id)
Expand All @@ -153,8 +160,6 @@ export class Admin extends Service {
const item = this.groups.find(group => group.id === id)
if (!item) throw new Error('group not found')
item.permissions = permissions
item.dispose!()
item.dispose = this.ctx.permissions.define('group.' + item.id, permissions)
await this.ctx.database.set('group', id, { permissions })
this.ctx.get('console')?.refresh('admin')
}
Expand Down

0 comments on commit e25a179

Please sign in to comment.