Skip to content

feat: permission route #3381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions ui/src/router/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
createRouter,
createWebHistory,
type NavigationGuardNext,
type RouteLocationNormalized,
type RouteRecordRaw,
type RouteRecordName,
} from 'vue-router'
import { hasPermission, set_next_route } from '@/utils/permission/index'
export const getChildRouteList: (
routeList: Array<RouteRecordRaw>,
path: string,
name?: RouteRecordName | null | undefined,
) => Array<RouteRecordRaw> = (routeList, path, name) => {
for (let index = 0; index < routeList.length; index++) {
const route = routeList[index]
if (name === route.name && path === route.path) {
return route.children || []
}
if (route.children && route.children.length > 0) {
const result = getChildRouteList(route.children, path, name)
if (result && result?.length > 0) {
return result
}
}
}
return []
}
/**
* 获取同级路由
* @param routeList
* @param name
* @returns
*/
export const getSameRouteList: (
routeList: Array<RouteRecordRaw>,
name?: RouteRecordName | null | undefined,
) => Array<RouteRecordRaw> = (routeList, name) => {
for (let index = 0; index < routeList.length; index++) {
const route = routeList[index]
if (name === route.name) {
return routeList
}
if (route.children && route.children.length > 0) {
const result = getSameRouteList(route.children, name)
if (result && result?.length > 0) {
return result
}
}
}
return []
}

/**
* 获取有权限的路由
* @param routes
* @param to
* @returns
*/
export const getPermissionRoute = (routes: Array<RouteRecordRaw>, to: RouteLocationNormalized) => {
const routeName: string = to.meta
? to.meta.permissionRoute
? (to.meta.permissionRoute as string)
: (to.name as string)
: (to.name as string)
const routeList = getSameRouteList(routes, routeName)
const route = routeList.find((route: any) => {
return (
(to.meta.group ? to.meta.group == route.meta.group : true) &&
(route.meta.permission ? hasPermission(route.meta.permission as any, 'OR') : true)
)
})

if (route?.name) {
return { name: route?.name, params: to.params }
}
return { name: 'noPermission' }
}
34 changes: 4 additions & 30 deletions ui/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { hasPermission, set_next_route } from '@/utils/permission/index'
import { getChildRouteList } from '@/router/common'
import NProgress from 'nprogress'
import { getPermissionRoute } from '@/router/common'
import {
createRouter,
createWebHistory,
Expand Down Expand Up @@ -47,44 +49,16 @@ router.beforeEach(
if (to.meta.permission ? hasPermission(to.meta.permission as any, 'OR') : true) {
next()
} else {
console.log('s')
if (to.meta.get_permission_route) {
const t = to.meta.get_permission_route()
console.log(t)
next(t)
return
}
// 如果没有权限则直接取404页面
next({ path: '/no-permission' })
const n = getPermissionRoute(routes, to)
next(n)
}
},
)
router.afterEach(() => {
NProgress.done()
})

export const getChildRouteListByPathAndName = (path: any, name?: RouteRecordName | any) => {
return getChildRouteList(routes, path, name)
}

export const getChildRouteList: (
routeList: Array<RouteRecordRaw>,
path: string,
name?: RouteRecordName | null | undefined,
) => Array<RouteRecordRaw> = (routeList, path, name) => {
for (let index = 0; index < routeList.length; index++) {
const route = routeList[index]
if (name === route.name && path === route.path) {
return route.children || []
}
if (route.children && route.children.length > 0) {
const result = getChildRouteList(route.children, path, name)
if (result && result?.length > 0) {
return result
}
}
}
return []
}

export default router
4 changes: 2 additions & 2 deletions ui/src/router/modules/application.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PermissionConst, EditionConst, RoleConst } from '@/utils/permission/data'
import { get_next_route } from '@/utils/permission'
const applicationRouter = {
path: '/application',
name: 'application',
Expand All @@ -12,6 +11,7 @@ const applicationRouter = {
PermissionConst.APPLICATION_READ.getWorkspacePermissionWorkspaceManageRole,
PermissionConst.APPLICATION_READ.getWorkspacePermission,
],
group: 'workspace',
order: 1,
},
redirect: '/application',
Expand All @@ -20,7 +20,7 @@ const applicationRouter = {
{
path: '/application',
name: 'application-index',
meta: { title: '应用主页', activeMenu: '/application' },
meta: { title: '应用主页', activeMenu: '/application', sameRoute: 'application' },
component: () => import('@/views/application/index.vue'),
hidden: true,
},
Expand Down
5 changes: 5 additions & 0 deletions ui/src/router/modules/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const DocumentRouter = {
active: 'document',
parentPath: '/knowledge/:id/:folderId',
parentName: 'KnowledgeDetail',
group: 'KnowledgeDetail',
permission: [
RoleConst.ADMIN,
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
Expand All @@ -43,6 +44,7 @@ const DocumentRouter = {
active: 'problem',
parentPath: '/knowledge/:id/:folderId',
parentName: 'KnowledgeDetail',
group: 'KnowledgeDetail',
permission: [
RoleConst.ADMIN,
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
Expand All @@ -66,6 +68,7 @@ const DocumentRouter = {
active: 'hit-test',
parentPath: '/knowledge/:id/:folderId',
parentName: 'KnowledgeDetail',
group: 'KnowledgeDetail',
},
component: () => import('@/views/hit-test/index.vue'),
},
Expand All @@ -80,6 +83,7 @@ const DocumentRouter = {
parentPath: '/knowledge/:id/:folderId',
parentName: 'KnowledgeDetail',
resourceType: ChatUserResourceEnum.KNOWLEDGE,
group: 'KnowledgeDetail',
permission: [
RoleConst.ADMIN,
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
Expand All @@ -104,6 +108,7 @@ const DocumentRouter = {
active: 'setting',
parentPath: '/knowledge/:id/:folderId',
parentName: 'KnowledgeDetail',
group: 'KnowledgeDetail',
permission: [
RoleConst.ADMIN,
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
Expand Down
3 changes: 2 additions & 1 deletion ui/src/router/modules/knowledge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ModelRouter = {
PermissionConst.KNOWLEDGE_READ.getWorkspacePermission,
PermissionConst.KNOWLEDGE_READ.getWorkspacePermissionWorkspaceManageRole,
],
group: 'workspace',
order: 2,
},
redirect: '/knowledge',
Expand All @@ -19,7 +20,7 @@ const ModelRouter = {
{
path: '/knowledge',
name: 'knowledge-index',
meta: { title: '知识库主页', activeMenu: '/knowledge' },
meta: { title: '知识库主页', activeMenu: '/knowledge', sameRoute: 'knowledge' },
component: () => import('@/views/knowledge/index.vue'),
},

Expand Down
2 changes: 2 additions & 0 deletions ui/src/router/modules/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ModelRouter = {
PermissionConst.MODEL_READ.getWorkspacePermission,
PermissionConst.MODEL_READ.getWorkspacePermissionWorkspaceManageRole,
],
group: 'workspace',
order: 4,
},
redirect: '/model',
Expand All @@ -22,6 +23,7 @@ const ModelRouter = {
meta: {
title: '模型主页',
activeMenu: '/model',
sameRoute: 'model',
},
component: () => import('@/views/model/index.vue'),
},
Expand Down
2 changes: 2 additions & 0 deletions ui/src/router/modules/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ModelRouter = {
PermissionConst.TOOL_READ.getWorkspacePermission,
PermissionConst.TOOL_READ.getWorkspacePermissionWorkspaceManageRole,
],
group: 'workspace',
order: 3,
},
redirect: '/tool',
Expand All @@ -20,6 +21,7 @@ const ModelRouter = {
path: '/tool',
name: 'tool-index',
meta: { title: '工具主页', activeMenu: '/tool' },
sameRoute: 'tool',
component: () => import('@/views/tool/index.vue'),
},
],
Expand Down
20 changes: 2 additions & 18 deletions ui/src/router/routes.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
import type { RouteRecordRaw } from 'vue-router'
const modules: any = import.meta.glob('./modules/*.ts', { eager: true })
import { hasPermission, set_next_route } from '@/utils/permission/index'

const rolesRoutes: RouteRecordRaw[] = [...Object.keys(modules).map((key) => modules[key].default)]
const get_workspace_permission_route = () => {
const route = rolesRoutes.find((route: any) => {
return (
route.meta?.menu &&
(route.meta.permission ? hasPermission(route.meta.permission as any, 'OR') : true)
)
})
if (route?.name) {
return { name: route?.name }
}
return { name: 'noPermission' }
}

export const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'home',
redirect: '/application',
children: [
...rolesRoutes.map((r) => {
if (r.meta) {
r.meta.get_permission_route = get_workspace_permission_route
}
return r
}),
...rolesRoutes,
{
path: '/no-permission',
name: 'noPermission',
Expand Down
Loading