Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/web-antd/src/router/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function setupAccessGuard(router: Router) {

// 生成菜单和路由
const { accessibleMenus, accessibleRoutes } = await generateAccess({
accessCodes: accessStore.accessCodes || [],
roles: userRoles,
router,
// 则会在菜单中显示,但是访问会被重定向到403
Expand Down
1 change: 1 addition & 0 deletions apps/web-ele/src/router/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function setupAccessGuard(router: Router) {

// 生成菜单和路由
const { accessibleMenus, accessibleRoutes } = await generateAccess({
accessCodes: accessStore.accessCodes || [],
roles: userRoles,
router,
// 则会在菜单中显示,但是访问会被重定向到403
Expand Down
1 change: 1 addition & 0 deletions apps/web-naive/src/router/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function setupAccessGuard(router: Router) {

// 生成菜单和路由
const { accessibleMenus, accessibleRoutes } = await generateAccess({
accessCodes: accessStore.accessCodes || [],
roles: userRoles,
router,
// 则会在菜单中显示,但是访问会被重定向到403
Expand Down
5 changes: 5 additions & 0 deletions packages/@core/base/typings/src/vue-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ interface RouteMeta {
* 用于路由->菜单排序
*/
order?: number;
/**
* 需要特定的权限码标识才可以访问
*/
perms?: string[];
/**
* 菜单所携带的参数
*/
Expand All @@ -121,6 +125,7 @@ type RouteRecordStringComponent<T = string> = {
type ComponentRecordType = Record<string, () => Promise<Component>>;

interface GenerateMenuAndRoutesOptions {
accessCodes?: string[];
fetchMenuListAsync?: () => Promise<RouteRecordStringComponent[]>;
forbiddenComponent?: RouteRecordRaw['component'];
layoutMap?: ComponentRecordType;
Expand Down
3 changes: 2 additions & 1 deletion packages/effects/access/src/accessible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function generateRoutes(
mode: AccessModeType,
options: GenerateMenuAndRoutesOptions,
) {
const { forbiddenComponent, roles, routes } = options;
const { forbiddenComponent, roles, routes, accessCodes } = options;

let resultRoutes: RouteRecordRaw[] = routes;
switch (mode) {
Expand All @@ -54,6 +54,7 @@ async function generateRoutes(
resultRoutes = await generateRoutesByFrontend(
routes,
roles || [],
accessCodes || [],
forbiddenComponent,
);
break;
Expand Down
17 changes: 17 additions & 0 deletions packages/utils/src/helpers/generate-routes-frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import { filterTree, mapTree } from '@vben-core/shared/utils';
async function generateRoutesByFrontend(
routes: RouteRecordRaw[],
roles: string[],
accessCodes: string[],
Copy link
Contributor

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 in generateRoutesByFrontend Invocations

  • packages/utils/src/helpers/__tests__/generate-routes-frontend.test.ts: Several test cases are missing the accessCodes parameter.
  • packages/effects/access/src/accessible.ts: Invocation of generateRoutesByFrontend does not include accessCodes.
🔗 Analysis chain

Ensure all invocations of generateRoutesByFrontend include the new accessCodes parameter

The addition of the accessCodes parameter to the generateRoutesByFrontend function requires updating all places where this function is called. Please verify that all invocations pass the appropriate accessCodes 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:

#!/bin/bash
# Description: Find all invocations of `generateRoutesByFrontend` and check for the `accessCodes` parameter.

# Search for the function calls to `generateRoutesByFrontend`
rg -A 2 -B 2 'generateRoutesByFrontend\(' --glob '!packages/utils/src/helpers/generate-routes-frontend.ts'

Length of output: 2104

forbiddenComponent?: RouteRecordRaw['component'],
): Promise<RouteRecordRaw[]> {
// 根据角色标识过滤路由表,判断当前用户是否拥有指定权限
const finalRoutes = filterTree(routes, (route) => {
if (!route.meta?.authority) {
return hasPerms(route, accessCodes);
Comment on lines +16 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider refactoring to reduce code duplication between hasAuthority and hasPerms

The functions hasAuthority and hasPerms share similar logic for access checks based on route metadata. To improve maintainability and reduce redundancy, consider refactoring these functions to share common code or merging them if appropriate.

}
return hasAuthority(route, roles);
});

Expand Down Expand Up @@ -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
Expand Down