Skip to content

Commit

Permalink
fix(admin): index user group by id, fix koishijs/koishi#1182
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 28, 2023
1 parent 6b586e3 commit bd4486a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion plugins/admin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@koishijs/plugin-admin",
"description": "Permission Manager for Koishi",
"version": "2.0.0-alpha.0",
"version": "2.0.0-alpha.1",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
28 changes: 17 additions & 11 deletions plugins/admin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ export class Admin extends Service {
this.data = await this.ctx.database.get('group', {})
for (const item of this.data) {
item.count = await this.ctx.database
.select('user', { permissions: { $el: item.name } })
.select('user', { permissions: { $el: 'group.' + item.id } })
.execute(row => $.count(row.id)) || 0
item.dispose = this.ctx.permissions.define(`group.` + item.id, item.permissions)
item.dispose = this.ctx.permissions.define('group.' + item.id, item.permissions)
}
}

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, [])
item.dispose = this.ctx.permissions.define('group.' + item.id, [])
this.data.push(item)
this.ctx.console?.groups?.refresh()
return item.id
Expand All @@ -63,6 +63,7 @@ export class Admin extends Service {
async renameGroup(id: number, name: string) {
const item = this.data.find(group => group.id === id)
if (!item) throw new Error('group not found')
if (item.name === name) return
item.name = name
await this.ctx.database.set('group', id, { name })
this.ctx.console?.groups?.refresh()
Expand All @@ -73,11 +74,16 @@ export class Admin extends Service {
if (index < 0) throw new Error('group not found')
const item = this.data[index]!
item.dispose!()
for (const user of await this.ctx.database.get('user', { permissions: { $el: item.name } }, ['id', 'permissions'])) {
remove(user.permissions, item.name)
await this.ctx.database.set('user', user.id, { permissions: user.permissions })
}
this.data.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)
}
await this.ctx.database.upsert('user', users)
const updates = this.data.filter((group) => {
return remove(group.permissions, 'group.' + id)
})
await this.ctx.database.upsert('group', updates)
await this.ctx.database.remove('group', id)
this.ctx.console?.groups?.refresh()
}
Expand All @@ -87,7 +93,7 @@ export class Admin extends Service {
if (!item) throw new Error('group not found')
item.permissions = permissions
item.dispose!()
item.dispose = this.ctx.permissions.define(`group.` + item.id, permissions)
item.dispose = this.ctx.permissions.define('group.' + item.id, permissions)
await this.ctx.database.set('group', id, { permissions })
this.ctx.console?.groups?.refresh()
}
Expand All @@ -97,8 +103,8 @@ export class Admin extends Service {
if (!item) throw new Error('group not found')
const data = await this.ctx.database.getUser(platform, aid, ['id', 'permissions'])
if (!data) throw new Error('user not found')
if (!data.permissions.includes(item.name)) {
data.permissions.push(item.name)
if (!data.permissions.includes('group.' + item.id)) {
data.permissions.push('group.' + item.id)
item.count!++
await this.ctx.database.set('user', data.id, { permissions: data.permissions })
this.ctx.console?.groups?.refresh()
Expand All @@ -110,7 +116,7 @@ export class Admin extends Service {
if (!item) throw new Error('group not found')
const data = await this.ctx.database.getUser(platform, aid, ['id', 'permissions'])
if (!data) throw new Error('user not found')
if (remove(data.permissions, item.name)) {
if (remove(data.permissions, 'group.' + item.id)) {
item.count!--
await this.ctx.database.set('user', data.id, { permissions: data.permissions })
this.ctx.console?.groups?.refresh()
Expand Down

0 comments on commit bd4486a

Please sign in to comment.