Skip to content
This repository was archived by the owner on May 12, 2020. It is now read-only.

Commit 3b3e926

Browse files
committed
feat(permission): add real-time routes filter
Real-time routes filter is used to filter common routes which needs to filter user access
1 parent fedc43d commit 3b3e926

5 files changed

Lines changed: 24 additions & 18 deletions

File tree

File renamed without changes.

src/permission/__roles_filter__/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import NProgress from 'nprogress'
55
import 'nprogress/nprogress.css'
66
import { tokenFromStorage, userInfoFromStorage } from 'UTILS/storage'
77
import { MessageBox } from 'element-ui'
8-
import createDynamicRoutes from '../create-routes'
8+
import createDynamicRoutes from './create-routes'
99
import constantRoutes from 'ROUTER/routes/constant'
1010

1111
NProgress.configure({ showSpinner: false })

src/permission/filter-routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function filterRoutes (routes, accessMap) {
4747
* @param {Array} accessMap 一个对应当前用户各个模块权限的 hashmap
4848
*/
4949
// ! 本质问题: 如何确定两个数组是否存在交集
50-
function validateAccess (route, accessMap) {
50+
export function validateAccess (route, accessMap) {
5151
if (route.meta && route.meta.access) {
5252
return Object.keys(route.meta.access).every(accessName => {
5353
// 在后端回传的权限表中不存在对应 route 所需的最低权限中的一项,

src/permission/index.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'nprogress/nprogress.css'
66
import { tokenFromStorage, userInfoFromStorage } from 'UTILS/storage'
77
import { MessageBox } from 'element-ui'
88
// import createDynamicRoutes from './create-routes'
9-
import createDynamicRoutes from './filter-routes'
9+
import createDynamicRoutes, { validateAccess } from './filter-routes'
1010
import constantRoutes from 'ROUTER/routes/constant'
1111

1212
NProgress.configure({ showSpinner: false })
@@ -123,18 +123,21 @@ router.beforeEach((to, from, next) => {
123123
return createRoutesMap(to, next)
124124
}
125125

126-
next()
127-
128-
// step: real-time routes filter
129-
// ! 如有动态权限验证的需求
130-
// if (!to.meta.access || validateAccess(route, to.meta.access)) {
131-
// next()
132-
// } else {
133-
// next({
134-
// path: `/403?redirect=${to.path}`,
135-
// replace: true
136-
// })
137-
// }
126+
// Optional step: real-time routes filter
127+
// ! 动态权限验证,如在公有路由中的未参与 private routes 过滤的路由需要权限验证时
128+
if (
129+
// 当前路由不存在权限验证时
130+
!to.meta.access ||
131+
// 当前路由存在权限验证时
132+
validateAccess(to, store.getters['login/accessMap'])
133+
) {
134+
next()
135+
} else {
136+
next({
137+
path: `/403?redirect=${to.path}`,
138+
replace: true
139+
})
140+
}
138141
} else {
139142
// ! State: user logout
140143
next({

src/router/routes/constant.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { constantComponents, plainExport } from 'ROUTER/components'
2-
import permission from 'PERMISSION/roles-map'
32

43
/**
54
* meta: {
@@ -43,7 +42,9 @@ export default [
4342
component: constantComponents.pagesAdminDashboard,
4443
meta: {
4544
title: 'Dashboard',
46-
roles: permission.admin.dashboard
45+
access: {
46+
device: ['read', 'write', 'import']
47+
}
4748
}
4849
},
4950
{
@@ -52,7 +53,9 @@ export default [
5253
meta: {
5354
title: 'Table',
5455
icon: 'el-icon-tickets',
55-
roles: permission.admin.table
56+
access: {
57+
device: ['read', 'write', 'import']
58+
}
5659
}
5760
}
5861
]

0 commit comments

Comments
 (0)