From fdf6833448736cd23fae49d4453d81b09c459c8c Mon Sep 17 00:00:00 2001 From: CaptainB Date: Fri, 15 Mar 2024 22:48:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E6=9D=83=E9=99=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../routes/modules/projectManagement.ts | 1 + frontend/src/store/modules/user/index.ts | 18 ++++++++++-- frontend/src/store/modules/user/types.ts | 15 ++++++++++ frontend/src/utils/permission.ts | 28 +++++++------------ 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/frontend/src/router/routes/modules/projectManagement.ts b/frontend/src/router/routes/modules/projectManagement.ts index b92853ea6c..e5ee7cb6f7 100644 --- a/frontend/src/router/routes/modules/projectManagement.ts +++ b/frontend/src/router/routes/modules/projectManagement.ts @@ -14,6 +14,7 @@ const ProjectManagement: AppRouteRecordRaw = { order: 7, hideChildrenInMenu: true, roles: [ + 'PROJECT_BASE_INFO:READ', 'SYSTEM_PARAMETER_SETTING_BASE:READ', 'PROJECT_TEMPLATE:READ', 'PROJECT_FILE_MANAGEMENT:READ', diff --git a/frontend/src/store/modules/user/index.ts b/frontend/src/store/modules/user/index.ts index 64e2ce6e2c..a23feeef21 100644 --- a/frontend/src/store/modules/user/index.ts +++ b/frontend/src/store/modules/user/index.ts @@ -39,6 +39,8 @@ const useUserStore = defineStore('user', { certification: undefined, role: '', userRolePermissions: [], + userRoles: [], + userRoleRelations: [], loginType: [], }), @@ -56,10 +58,20 @@ const useUserStore = defineStore('user', { systemPermissions: string[]; } { const appStore = useAppStore(); + + state.userRoleRelations?.forEach(ug => { + state.userRolePermissions?.forEach(gp => { + if (gp.userRole.id === ug.roleId) { + ug.userRolePermissions = gp.userRolePermissions; + ug.userRole = gp.userRole; + } + }); + }); + return { - projectPermissions: composePermissions(state.userRolePermissions || [], 'PROJECT', appStore.currentProjectId), - orgPermissions: composePermissions(state.userRolePermissions || [], 'ORGANIZATION', appStore.currentOrgId), - systemPermissions: composePermissions(state.userRolePermissions || [], 'SYSTEM', 'global'), + projectPermissions: composePermissions(state.userRoleRelations || [], 'PROJECT', appStore.currentProjectId), + orgPermissions: composePermissions(state.userRoleRelations || [], 'ORGANIZATION', appStore.currentOrgId), + systemPermissions: composePermissions(state.userRoleRelations || [], 'SYSTEM', 'global'), }; }, }, diff --git a/frontend/src/store/modules/user/types.ts b/frontend/src/store/modules/user/types.ts index d03261762e..14de03cc09 100644 --- a/frontend/src/store/modules/user/types.ts +++ b/frontend/src/store/modules/user/types.ts @@ -11,6 +11,19 @@ export interface UserRole { scopeId: string; // 项目/组织/系统 id type: SystemScopeType; } + +export interface UserRoleRelation { + id: string; + userId: string; + roleId: string; + sourceId: string; + organizationId: string; + createTime: number; + createUser: string; + userRolePermissions: permissionsItem[]; + userRole: UserRole; +} + export interface permissionsItem { id: string; permissionId: string; @@ -40,5 +53,7 @@ export interface UserState { lastOrganizationId?: string; lastProjectId?: string; userRolePermissions?: UserRolePermissions[]; + userRoles?: UserRole[]; + userRoleRelations?: UserRoleRelation[]; loginType: string[]; } diff --git a/frontend/src/utils/permission.ts b/frontend/src/utils/permission.ts index fd331215aa..96f7359238 100644 --- a/frontend/src/utils/permission.ts +++ b/frontend/src/utils/permission.ts @@ -5,7 +5,7 @@ import { firstLevelMenu } from '@/config/permission'; import { INDEX_ROUTE } from '@/router/routes/base'; import appRoutes from '@/router/routes/index'; import { useAppStore, useUserStore } from '@/store'; -import { SystemScopeType, UserRole, UserRolePermissions } from '@/store/modules/user/types'; +import {SystemScopeType, UserRole, UserRoleRelation} from '@/store/modules/user/types'; export function hasPermission(permission: string, typeList: string[]) { const userStore = useUserStore(); @@ -52,31 +52,23 @@ export function hasAllPermission(permissions: string[], typeList = ['PROJECT', ' return permissions.every((permission) => hasPermission(permission, typeList)); } -function filterProject(role: UserRole, id: string) { - return role && role.type === 'PROJECT' && (role.scopeId === id || role.scopeId === 'global'); -} -function filterOrganization(role: UserRole, id: string) { - return role && role.type === 'ORGANIZATION' && (role.scopeId === id || role.scopeId === 'global'); -} -function filterSystem(role: UserRole, id: string) { - return role && role.type === 'SYSTEM' && role.scopeId === id; -} - -export function composePermissions(userRolePermissions: UserRolePermissions[], type: SystemScopeType, id: string) { - let func: (role: UserRole, val: string) => boolean; +export function composePermissions(userRoleRelations: UserRoleRelation[], type: SystemScopeType, id: string) { + let func: (role: UserRole) => boolean; switch (type) { case 'PROJECT': - func = filterProject; + func = role => role && role.type === 'PROJECT'; break; case 'ORGANIZATION': - func = filterOrganization; + func = role => role && role.type === 'ORGANIZATION'; break; default: - func = filterSystem; + func = role => role && role.type === 'SYSTEM'; break; } - return userRolePermissions - .filter((ur) => func(ur.userRole, id)) + + return userRoleRelations + .filter((ur) => func(ur.userRole)) + .filter((ur) => ur.sourceId === id) .flatMap((role) => role.userRolePermissions) .map((g) => g.permissionId); }