-
Notifications
You must be signed in to change notification settings - Fork 7.2k
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
feat: 路由增加perms配置项 #4706
base: main
Are you sure you want to change the base?
feat: 路由增加perms配置项 #4706
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,14 @@ import { filterTree, mapTree } from '@vben-core/shared/utils'; | |
async function generateRoutesByFrontend( | ||
routes: RouteRecordRaw[], | ||
roles: string[], | ||
accessCodes: string[], | ||
forbiddenComponent?: RouteRecordRaw['component'], | ||
): Promise<RouteRecordRaw[]> { | ||
// 根据角色标识过滤路由表,判断当前用户是否拥有指定权限 | ||
const finalRoutes = filterTree(routes, (route) => { | ||
if (!route.meta?.authority) { | ||
return hasPerms(route, accessCodes); | ||
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider refactoring to reduce code duplication between The functions |
||
} | ||
return hasAuthority(route, roles); | ||
}); | ||
|
||
|
@@ -42,7 +46,20 @@ function hasAuthority(route: RouteRecordRaw, access: string[]) { | |
|
||
return canAccess || (!canAccess && menuHasVisibleWithForbidden(route)); | ||
} | ||
/** | ||
* 判断路由是否有权限访问 | ||
* @param route | ||
* @param access | ||
*/ | ||
function hasPerms(route: RouteRecordRaw, access: string[]) { | ||
const perms = route.meta?.perms; | ||
if (!perms) { | ||
return true; | ||
} | ||
const canAccess = access.some((value) => perms.includes(value)); | ||
|
||
return canAccess || (!canAccess && menuHasVisibleWithForbidden(route)); | ||
} | ||
/** | ||
* 判断路由是否在菜单中显示,但是访问会被重定向到403 | ||
* @param route | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Missing
accessCodes
Parameter ingenerateRoutesByFrontend
Invocationspackages/utils/src/helpers/__tests__/generate-routes-frontend.test.ts
: Several test cases are missing theaccessCodes
parameter.packages/effects/access/src/accessible.ts
: Invocation ofgenerateRoutesByFrontend
does not includeaccessCodes
.🔗 Analysis chain
Ensure all invocations of
generateRoutesByFrontend
include the newaccessCodes
parameterThe addition of the
accessCodes
parameter to thegenerateRoutesByFrontend
function requires updating all places where this function is called. Please verify that all invocations pass the appropriateaccessCodes
to prevent potential runtime errors.To assist with this verification, you can run the following script to locate all calls to
generateRoutesByFrontend
:🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 2104