fix: 修复菜单权限问题

This commit is contained in:
CaptainB 2024-03-15 22:48:28 +08:00 committed by 刘瑞斌
parent fe1893db5e
commit fdf6833448
4 changed files with 41 additions and 21 deletions

View File

@ -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',

View File

@ -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'),
};
},
},

View File

@ -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[];
}

View File

@ -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);
}