Skip to content

Commit 99fd328

Browse files
authored
feat: UI permission instruction (#3010)
1 parent c79479d commit 99fd328

File tree

8 files changed

+96
-10
lines changed

8 files changed

+96
-10
lines changed

ui/src/directives/hasPermission.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { App } from 'vue'
2+
import { hasPermission } from '@/utils/permission'
3+
4+
const display = async (el: any, binding: any) => {
5+
const has = hasPermission(
6+
binding.value?.permission || binding.value,
7+
binding.value?.compare || 'OR',
8+
)
9+
if (!has) {
10+
el.style.display = 'none'
11+
} else {
12+
delete el.style.display
13+
}
14+
}
15+
16+
export default {
17+
install: (app: App) => {
18+
app.directive('hasPermission', {
19+
async created(el: any, binding: any) {
20+
display(el, binding)
21+
},
22+
async beforeUpdate(el: any, binding: any) {
23+
display(el, binding)
24+
},
25+
})
26+
},
27+
}

ui/src/directives/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { App } from 'vue'
2+
3+
const directives = import.meta.glob('./*.ts', { eager: true })
4+
const install = (app: App) => {
5+
Object.keys(directives)
6+
.filter((key: string) => {
7+
return !key.endsWith('index.ts')
8+
})
9+
.forEach((key: string) => {
10+
const directive: any = directives[key]
11+
app.use(directive.default)
12+
})
13+
}
14+
export default { install }

ui/src/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import App from './App.vue'
1010
import router from '@/router'
1111
import i18n from '@/locales'
1212
import Components from '@/components'
13+
import directives from '@/directives'
1314
const app = createApp(App)
1415
app.use(createPinia())
1516
for (const [key, component] of Object.entries(ElementPlusIcons)) {
@@ -18,11 +19,12 @@ for (const [key, component] of Object.entries(ElementPlusIcons)) {
1819
const locale_map: any = {
1920
'zh-CN': zhCn,
2021
'zh-Hant': zhTW,
21-
'en-US': enUs
22+
'en-US': enUs,
2223
}
2324
app.use(ElementPlus, {
24-
locale: locale_map[localStorage.getItem('MaxKB-locale') || navigator.language || 'en-US']
25+
locale: locale_map[localStorage.getItem('MaxKB-locale') || navigator.language || 'en-US'],
2526
})
27+
app.use(directives)
2628
app.use(router)
2729
app.use(i18n)
2830
app.use(Components)

ui/src/stores/modules/user.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ const useLoginStore = defineStore('user', {
3636
return !this.themeInfo?.theme || this.themeInfo?.theme === '#3370FF'
3737
},
3838
async profile() {
39-
return UserApi.getUserProfile().then((ok: { data: User }) => {
40-
this.userInfo = ok.data
39+
return UserApi.getUserProfile().then((ok) => {
40+
this.userInfo = ok
4141
useLocalStorage<string>(localeConfigKey, 'en-US').value =
4242
ok.data?.language || this.getLanguage()
4343
// return this.asyncGetProfile()
@@ -72,7 +72,7 @@ const useLoginStore = defineStore('user', {
7272
? [...this.userInfo?.permissions, 'x-pack']
7373
: this.userInfo?.permissions
7474
} else {
75-
return []
75+
return this.userInfo?.permissions
7676
}
7777
},
7878
getRole() {

ui/src/utils/permission/data.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Permission } from '@/utils/permission/type'
2+
const PermissionConst = {
3+
USER_READ: new Permission('USER:READ'),
4+
USER_CREATE: new Permission('USER:CREATE'),
5+
KNOWLEDGE_READ: new Permission('KNOWLEDGE:READ'),
6+
}
7+
export default PermissionConst

ui/src/utils/permission/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import useStore from '@/stores';
1+
import useStore from '@/stores'
22
import { Role, Permission, ComplexPermission } from '@/utils/permission/type'
33
/**
44
* 是否包含当前权限
55
* @param permission 当前权限
66
* @returns True 包含 false 不包含
77
*/
88
const hasPermissionChild = (permission: Role | string | Permission | ComplexPermission) => {
9-
const { user } = useStore();
9+
const { user } = useStore()
1010
const permissions = user.getPermissions()
1111
const role = user.getRole()
1212
if (!permission) {
@@ -43,7 +43,7 @@ export const hasPermission = (
4343
| string
4444
| Permission
4545
| ComplexPermission,
46-
compare: 'OR' | 'AND'
46+
compare: 'OR' | 'AND',
4747
): boolean => {
4848
if (permission instanceof Array) {
4949
return compare === 'OR'

ui/src/utils/permission/type.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ export class Permission {
1717
constructor(permission: string) {
1818
this.permission = permission
1919
}
20+
/**
21+
* 工作空间权限
22+
* @param workspace_id 工作空间id
23+
* @returns 工作空间权限
24+
*/
25+
getWorkspacePermission(workspace_id: string) {
26+
return `${this.permission}:/WORKSPACE/${workspace_id}`
27+
}
28+
/**
29+
* 工作空间资源权限
30+
* @param workspace_id 工作空间id
31+
* @param resource 资源
32+
* @param resource_id 资源id
33+
* @returns 工作空间资源权限
34+
*/
35+
getWorkspaceResourcePermission(workspace_id: string, resource: string, resource_id: string) {
36+
return `${this.permission}:/WORKSPACE/${workspace_id}/${resource}/${resource_id}`
37+
}
2038
}
2139
/**
2240
* 复杂权限对象

ui/src/views/HomeView.vue

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
<script setup lang="ts"></script>
1+
<script setup lang="ts">
2+
import PermissionConst from '@/utils/permission/data'
3+
</script>
24

3-
<template>首页</template>
5+
<template>
6+
首页
7+
<div v-hasPermission="PermissionConst.USER_READ.getWorkspacePermission('default')">
8+
default工作空间用户只读
9+
</div>
10+
<div v-hasPermission="PermissionConst.USER_READ.getWorkspacePermission('default1')">
11+
default1工作空间用户只读
12+
</div>
13+
<div v-hasPermission="PermissionConst.USER_READ">用户只读</div>
14+
<div
15+
v-hasPermission="
16+
PermissionConst.KNOWLEDGE_READ.getWorkspaceResourcePermission('default', 'KNOWLEDGE', 'xxx')
17+
"
18+
>
19+
default工作空间的知识库xxx权限
20+
</div>
21+
</template>

0 commit comments

Comments
 (0)