From d16f0a924bd748b1d3a680fad238dac9053e81f2 Mon Sep 17 00:00:00 2001 From: wangtian <2710165659@qq.com> Date: Mon, 30 Nov 2026 16:56:33 +0800 Subject: [PATCH] perf(ui): cache permission lookups and fix batch selection --- ui/src/stores/modules/user.ts | 47 +++++++-- ui/src/utils/permission/index.ts | 18 ++-- ui/src/views/application/index.vue | 59 +++++++---- .../component/KnowledgeListContainer.vue | 91 +++++++++-------- .../tool/component/ToolListContainer.vue | 98 +++++++++++-------- 5 files changed, 191 insertions(+), 122 deletions(-) diff --git a/ui/src/stores/modules/user.ts b/ui/src/stores/modules/user.ts index a622e8efd97..237f5c8fc78 100644 --- a/ui/src/stores/modules/user.ts +++ b/ui/src/stores/modules/user.ts @@ -20,6 +20,16 @@ export interface userStateTypes { rsaKey: string } +interface PermissionIndex { + userInfo: User | null + edition: string + list: string[] + set: Set + roles: Set +} + +let permissionIndex: PermissionIndex | null = null + const useUserStore = defineStore('user', { state: (): userStateTypes => ({ userInfo: null, @@ -43,17 +53,34 @@ const useUserStore = defineStore('user', { return this.workspace_id }, - getPermissions() { - if (this.userInfo) { - if (this.isEE()) { - return [...this.userInfo?.permissions, 'X-PACK-EE'] - } else if (this.isPE()) { - return [...this.userInfo?.permissions, 'X-PACK-PE'] - } - return this.userInfo?.permissions - } else { - return [] + getPermissionIndex(): PermissionIndex { + const edition = this.getEdition() + if ( + permissionIndex && + permissionIndex.userInfo === this.userInfo && + permissionIndex.edition === edition + ) { + return permissionIndex + } + const base: string[] = this.userInfo?.permissions ?? [] + const list = edition === 'X-PACK-CE' ? base : [...base, edition] + permissionIndex = { + userInfo: this.userInfo, + edition, + list, + set: new Set(list), + roles: new Set(this.userInfo?.role ?? []), } + return permissionIndex + }, + getPermissions() { + return this.getPermissionIndex().list + }, + hasPermissionKey(permission: string) { + return this.getPermissionIndex().set.has(permission) + }, + hasRoleKey(role: string) { + return this.getPermissionIndex().roles.has(role) }, getEdition() { if (this.userInfo) { diff --git a/ui/src/utils/permission/index.ts b/ui/src/utils/permission/index.ts index ccdf3d29afa..27837fe1868 100644 --- a/ui/src/utils/permission/index.ts +++ b/ui/src/utils/permission/index.ts @@ -19,9 +19,6 @@ const hasPermissionChild = ( permission: Role | string | Permission | ComplexPermission | Edition | PF, ) => { const { user } = useStore() - const permissions = user.getPermissions() - const role: Array = user.getRole() - const edition = user.getEdition() if (!permission) { return true } @@ -29,25 +26,26 @@ const hasPermissionChild = ( permission = (permission as PF)() } if (permission instanceof Role) { - return role.includes(permission.role) + return user.hasRoleKey(permission.role) } if (permission instanceof Permission) { - return permissions.includes(permission.permission) + return user.hasPermissionKey(permission.permission) } if (permission instanceof Edition) { - return permission.edition === edition + return permission.edition === user.getEdition() } if (permission instanceof ComplexPermission) { const permissionOk = permission.permissionList.some((p) => - permissions.includes(isFunction(p) ? (p as CPF)().toString() : p.toString()), + user.hasPermissionKey(isFunction(p) ? (p as CPF)().toString() : p.toString()), ) const roleList = permission.roleList const roleOk = roleList.some((r) => - role.includes(isFunction(r) ? (r as CRF)().toString() : r.toString()), + user.hasRoleKey(isFunction(r) ? (r as CRF)().toString() : r.toString()), ) const editionList = permission.editionList + const edition = user.getEdition() const editionOK = - permission.editionList.length > 0 + editionList.length > 0 ? editionList.some((e) => edition.toString() == e.toString()) : true @@ -56,7 +54,7 @@ const hasPermissionChild = ( : (permissionOk || roleOk) && editionOK } if (typeof permission === 'string') { - return permissions.includes(permission) + return user.hasPermissionKey(permission) } return false diff --git a/ui/src/views/application/index.vue b/ui/src/views/application/index.vue index a0f5356a5e8..76bee242be4 100644 --- a/ui/src/views/application/index.vue +++ b/ui/src/views/application/index.vue @@ -186,7 +186,7 @@ > -