Skip to content

Commit

Permalink
feat(admin): use colon separated permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 1, 2024
1 parent 2c3bc05 commit 4176747
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
8 changes: 4 additions & 4 deletions plugins/admin/client/group.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

<el-select v-model="permission">
<el-option
v-for="id in store.permissions.filter(item => (active.type === 'track' ? item.startsWith('group.') : true) && !permissions.includes(item))"
v-for="id in [...Object.keys(store.admin.group).map(id => `group:${id}`), ...active.type === 'track' ? [] : store.permissions]"
:key="id"
:value="id">
<permission-name :id="id" />
Expand Down Expand Up @@ -232,11 +232,11 @@ async function removeUser() {
}
function getLink(name: string) {
if (name.startsWith('group.')) {
if (name.startsWith('group:')) {
return `/admin/group/${name.slice(6)}`
} else if (name.startsWith('track.')) {
} else if (name.startsWith('track:')) {
return `/admin/track/${name.slice(6)}`
} else if (name.startsWith('command.')) {
} else if (name.startsWith('command:')) {
return `/commands/${name.slice(8).replace(/\./g, '/')}`
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/admin/client/name.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<template>
<template v-if="id.startsWith('command.')">
<template v-if="id.startsWith('command:')">
指令:{{ id.slice(8) }}
</template>
<template v-else-if="id.startsWith('group.')">
<template v-else-if="id.startsWith('group:')">
用户组:{{ store.locales?.[`permission.${id}`] || store.admin.group[id.slice(6)].name || '未命名' }}
</template>
<template v-else>
Expand Down
17 changes: 8 additions & 9 deletions plugins/admin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class Admin extends Service {
this.tracks = await this.ctx.database.get('perm_track', {})
for (const item of this.groups) {
item.count = await this.ctx.database
.select('user', { permissions: { $el: 'group.' + item.id } })
.select('user', { permissions: { $el: 'group:' + item.id } })
.execute(row => $.count(row.id)) || 0
this.setupGroup(item)
}
Expand All @@ -72,8 +72,7 @@ export class Admin extends Service {

private setupGroup(item: PermGroup) {
item.dispose = this.ctx.permissions.define('(name)', {
inherits: ({ name }) => item.permissions.includes(name) && ['group.' + item.id],
list: () => ['group.' + item.id],
inherits: ({ name }) => item.permissions.includes(name) && ['group:' + item.id],
})
}

Expand Down Expand Up @@ -143,13 +142,13 @@ export class Admin extends Service {
if (index < 0) throw new Error('group not found')
const [item] = this.groups.splice(index, 1)
item.dispose!()
const users = await this.ctx.database.get('user', { permissions: { $el: 'group.' + id } }, ['id', 'permissions'])
const users = await this.ctx.database.get('user', { permissions: { $el: 'group:' + id } }, ['id', 'permissions'])
for (const user of users) {
remove(user.permissions, 'group.' + id)
remove(user.permissions, 'group:' + id)
}
await this.ctx.database.upsert('user', users)
const updates = this.groups.filter((group) => {
return remove(group.permissions, 'group.' + id)
return remove(group.permissions, 'group:' + id)
})
await this.ctx.database.upsert('group', updates)
await this.ctx.database.remove('group', id)
Expand All @@ -169,8 +168,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('group.' + item.id)) {
data.permissions.push('group.' + item.id)
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.get('console')?.refresh('admin')
Expand All @@ -182,7 +181,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, 'group.' + item.id)) {
if (remove(data.permissions, 'group:' + item.id)) {
item.count!--
await this.ctx.database.set('user', data.id, { permissions: data.permissions })
this.ctx.get('console')?.refresh('admin')
Expand Down

0 comments on commit 4176747

Please sign in to comment.