Skip to content

Commit 7f91797

Browse files
committed
feat: ui hasPermission
1 parent f773792 commit 7f91797

File tree

9 files changed

+56
-254
lines changed

9 files changed

+56
-254
lines changed

ui/src/directives/hasPermission.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { App } from 'vue'
22
import { hasPermission } from '@/utils/permission'
3-
43
const display = async (el: any, binding: any) => {
54
const has = hasPermission(
65
binding.value?.permission || binding.value,

ui/src/router/modules/application-detail.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { ComplexPermission } from '@/utils/permission/type'
2+
import { PermissionConst, RoleConst } from '@/utils/permission/data'
3+
14
const ApplicationDetailRouter = {
25
path: '/application/:id/:type',
36
name: 'ApplicationDetail',
@@ -15,6 +18,10 @@ const ApplicationDetailRouter = {
1518
active: 'overview',
1619
parentPath: '/application/:id/:type',
1720
parentName: 'ApplicationDetail',
21+
permission: [
22+
PermissionConst.APPLICATION_OVERVIEW_READ.getWorkspacePermission,
23+
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
24+
],
1825
},
1926
component: () => import('@/views/application-overview/index.vue'),
2027
},
@@ -66,10 +73,10 @@ const ApplicationDetailRouter = {
6673
title: 'views.chatLog.title',
6774
active: 'chat-log',
6875
parentPath: '/application/:id/:type',
69-
parentName: 'ApplicationDetail'
76+
parentName: 'ApplicationDetail',
7077
},
71-
component: () => import('@/views/chat-log/index.vue')
72-
}
78+
component: () => import('@/views/chat-log/index.vue'),
79+
},
7380
],
7481
}
7582

ui/src/stores/modules-shared-system/theme.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

ui/src/stores/modules-shared-system/user.ts

Lines changed: 0 additions & 191 deletions
This file was deleted.

ui/src/stores/modules/user.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ const useUserStore = defineStore('user', {
7373
UserApi.getProfile()
7474
.then(async (ok) => {
7575
// this.version = ok.data?.version || '-'
76-
console.log(ok)
7776
this.license_is_valid = ok.data.license_is_valid
7877
this.edition = ok.data.edition
7978

@@ -91,21 +90,23 @@ const useUserStore = defineStore('user', {
9190
})
9291
})
9392
},
94-
9593
getPermissions() {
9694
if (this.userInfo) {
97-
return this.isXPack && this.XPACK_LICENSE_IS_VALID
98-
? [...this.userInfo?.permissions, 'x-pack']
99-
: this.userInfo?.permissions
100-
} else {
95+
if (this.isEE()) {
96+
return [...this.userInfo?.permissions, 'x-pack-ee']
97+
} else if (this.isPE()) {
98+
return [...this.userInfo?.permissions, 'x-pack-pe']
99+
}
101100
return this.userInfo?.permissions
101+
} else {
102+
return []
102103
}
103104
},
104105
getRole() {
105106
if (this.userInfo) {
106107
return this.userInfo?.role
107108
} else {
108-
return ''
109+
return []
109110
}
110111
},
111112
async theme(loading?: Ref<boolean>) {

ui/src/utils/common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,7 @@ export const defaultIcon = '/ui/favicon.ico'
6666
export function isAppIcon(url: string | undefined) {
6767
return url === defaultIcon ? '' : url
6868
}
69+
70+
export function isFunction(fn: any) {
71+
return typeof fn === 'function'
72+
}

ui/src/utils/permission/data.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { Permission } from '@/utils/permission/type'
1+
import { Permission, Role } from '@/utils/permission/type'
22
const PermissionConst = {
33
USER_READ: new Permission('USER:READ'),
44
USER_CREATE: new Permission('USER:CREATE'),
55
KNOWLEDGE_READ: new Permission('KNOWLEDGE:READ'),
6+
APPLICATION_OVERVIEW_READ: new Permission('APPLICATION_OVERVIEW_READ'),
67
}
7-
export default PermissionConst
8+
const RoleConst = {
9+
ADMIN: new Role('ADMIN'),
10+
WORKSPACE_MANAGE: new Role('WORKSPACE_MANAGE'),
11+
USER: new Role('USER'),
12+
}
13+
export { PermissionConst, RoleConst }

ui/src/utils/permission/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
import useStore from '@/stores'
22
import { Role, Permission, ComplexPermission } from '@/utils/permission/type'
3+
import { isFunction } from '@/utils/common'
4+
5+
type PF = () => Role | string | Permission | ComplexPermission
36
/**
47
* 是否包含当前权限
58
* @param permission 当前权限
69
* @returns True 包含 false 不包含
710
*/
8-
const hasPermissionChild = (permission: Role | string | Permission | ComplexPermission) => {
11+
const hasPermissionChild = (permission: Role | string | Permission | ComplexPermission | PF) => {
912
const { user } = useStore()
1013
const permissions = user.getPermissions()
11-
const role = user.getRole()
14+
const role: Array<string> = user.getRole()
1215
if (!permission) {
1316
return true
1417
}
18+
19+
if (isFunction(permission)) {
20+
permission = (permission as PF)()
21+
}
1522
if (permission instanceof Role) {
16-
return role === permission.role
23+
return role.includes(permission.role)
1724
}
1825
if (permission instanceof Permission) {
1926
return permissions.includes(permission.permission)
2027
}
2128
if (permission instanceof ComplexPermission) {
2229
const permissionOk = permission.permissionList.some((p) => permissions.includes(p))
23-
const roleOk = permission.roleList.includes(role)
30+
const roleOk = role.some((r) => permission.roleList.includes(r))
2431
return permission.compare === 'AND' ? permissionOk && roleOk : permissionOk || roleOk
2532
}
2633
if (typeof permission === 'string') {
@@ -38,11 +45,12 @@ const hasPermissionChild = (permission: Role | string | Permission | ComplexPerm
3845
*/
3946
export const hasPermission = (
4047
permission:
41-
| Array<Role | string | Permission | ComplexPermission>
48+
| Array<Role | string | Permission | ComplexPermission | PF>
4249
| Role
4350
| string
4451
| Permission
45-
| ComplexPermission,
52+
| ComplexPermission
53+
| PF,
4654
compare: 'OR' | 'AND',
4755
): boolean => {
4856
if (permission instanceof Array) {

0 commit comments

Comments
 (0)