fix: problem that child route can't inherit authority configuration of it's parent; 🐛

修复:子路由无法继承其父路由的权限配置的问题;
This commit is contained in:
iczer 2020-09-02 18:55:28 +08:00
parent 5ec6f73d6f
commit 4db7c2deff
3 changed files with 36 additions and 17 deletions

View File

@ -1,4 +1,4 @@
import {hasPermission, hasRole} from '@/utils/authority-utils' import {hasAuthority} from '@/utils/authority-utils'
import {loginIgnore} from '@/router/index' import {loginIgnore} from '@/router/index'
import {checkAuthorization} from '@/utils/request' import {checkAuthorization} from '@/utils/request'
@ -30,7 +30,7 @@ const authorityGuard = (to, from, next, options) => {
const {store, message} = options const {store, message} = options
const permissions = store.getters['account/permissions'] const permissions = store.getters['account/permissions']
const roles = store.getters['account/roles'] const roles = store.getters['account/roles']
if (!hasPermission(to, permissions) && !hasRole(to, roles)) { if (!hasAuthority(to, permissions, roles)) {
message.warning(`对不起,您无权访问页面: ${to.fullPath},请联系管理员`) message.warning(`对不起,您无权访问页面: ${to.fullPath},请联系管理员`)
next({path: '/403'}) next({path: '/403'})
} else { } else {

View File

@ -1,11 +1,10 @@
/** /**
* 判断是否有路由的权限 * 判断是否有路由的权限
* @param route 路由 * @param authority 路由权限配置
* @param permissions 用户权限集合 * @param permissions 用户权限集合
* @returns {boolean|*} * @returns {boolean|*}
*/ */
function hasPermission(route, permissions) { function hasPermission(authority, permissions) {
const authority = route.meta.authority || '*'
let required = '*' let required = '*'
if (typeof authority === 'string') { if (typeof authority === 'string') {
required = authority required = authority
@ -17,11 +16,10 @@ function hasPermission(route, permissions) {
/** /**
* 判断是否有路由需要的角色 * 判断是否有路由需要的角色
* @param route 路由 * @param authority 路由权限配置
* @param roles 用户角色集合 * @param roles 用户角色集合
*/ */
function hasRole(route, roles) { function hasRole(authority, roles) {
const authority = route.meta.authority || '*'
let required = undefined let required = undefined
if (typeof authority === 'object') { if (typeof authority === 'object') {
required = authority.role required = authority.role
@ -47,6 +45,23 @@ function hasAnyRole(required, roles) {
} }
} }
/**
* 路由权限校验
* @param route 路由
* @param permissions 用户权限集合
* @param roles 用户角色集合
* @returns {boolean}
*/
function hasAuthority(route, permissions, roles) {
const authorities = [...route.meta.pAuthorities, route.meta.authority]
for (let authority of authorities) {
if (!hasPermission(authority, permissions) && !hasRole(authority, roles)) {
return false
}
}
return true
}
/** /**
* 根据权限配置过滤菜单数据 * 根据权限配置过滤菜单数据
* @param menuData * @param menuData
@ -56,7 +71,7 @@ function hasAnyRole(required, roles) {
function filterMenu(menuData, permissions, roles) { function filterMenu(menuData, permissions, roles) {
menuData.forEach(menu => { menuData.forEach(menu => {
if (menu.meta && menu.meta.invisible === undefined) { if (menu.meta && menu.meta.invisible === undefined) {
menu.meta.invisible = !hasPermission(menu, permissions) && !hasRole(menu, roles) menu.meta.invisible = !hasAuthority(menu, permissions, roles)
if (menu.children && menu.children.length > 0) { if (menu.children && menu.children.length > 0) {
filterMenu(menu.children, permissions, roles) filterMenu(menu.children, permissions, roles)
} }
@ -64,4 +79,4 @@ function filterMenu(menuData, permissions, roles) {
}) })
} }
export {hasPermission, hasRole, filterMenu} export {filterMenu, hasAuthority}

View File

@ -97,15 +97,16 @@ function mergeRoutes(target, source) {
/** /**
* 格式化路由的权限配置 * 格式化路由的权限配置
* @param routes * @param routes 路由
* @param pAuthorities 父级路由权限配置集合
*/ */
function formatAuthority(routes) { function formatAuthority(routes, pAuthorities = []) {
routes.forEach(route => { routes.forEach(route => {
const meta = route.meta const meta = route.meta
if (meta) { if (meta) {
let authority = {} let authority = {}
if (!meta.authority) { if (!meta.authority) {
authority.permission = '*' authority = pAuthorities.length > 0 ? pAuthorities[pAuthorities.length - 1] : {permission: '*'}
}else if (typeof meta.authority === 'string') { }else if (typeof meta.authority === 'string') {
authority.permission = meta.authority authority.permission = meta.authority
} else if (typeof meta.authority === 'object') { } else if (typeof meta.authority === 'object') {
@ -114,17 +115,20 @@ function formatAuthority(routes) {
if (typeof role === 'string') { if (typeof role === 'string') {
authority.role = [role] authority.role = [role]
} }
if (!authority.permission && !authority.role) {
authority = pAuthorities.length > 0 ? pAuthorities[pAuthorities.length - 1] : {permission: '*'}
}
} else { } else {
console.log(typeof meta.authority) console.log(typeof meta.authority)
} }
meta.authority = authority meta.authority = authority
} else { } else {
route.meta = { const authority = pAuthorities.length > 0 ? pAuthorities[pAuthorities.length - 1] : {permission: '*'}
authority: {permission: '*'} route.meta = {authority}
}
} }
route.meta.pAuthorities = pAuthorities
if (route.children) { if (route.children) {
formatAuthority(route.children) formatAuthority(route.children, [...pAuthorities, route.meta.authority])
} }
}) })
} }