fix: problem that can't set roles array for route's authority; 🐛

修复:路由权限认证无法设置角色数组的问题;
master
chenghongxing 5 years ago
parent ec3e4b5571
commit 2021fb575d
  1. 47
      src/plugins/authority-plugin.js
  2. 24
      src/utils/routerUtil.js

@ -2,16 +2,19 @@
* 获取路由需要的权限 * 获取路由需要的权限
* @param permissions * @param permissions
* @param route * @param route
* @returns {*} * @returns {Permission}
*/ */
const getRoutePermission = (permissions, route) => permissions.find(item => item.id === route.meta.authority.permission) const getRoutePermission = (permissions, route) => permissions.find(item => item.id === route.meta.authority.permission)
/** /**
* 获取路由需要的角色 * 获取路由需要的角色
* @param roles * @param roles
* @param route * @param route
* @returns {*} * @returns {Array[Role]}
*/ */
const getRouteRole = (roles, route) => roles.find(item => item.id === route.meta.authority.role) const getRouteRole = (roles, route) => {
const requiredRoles = route.meta.authority.role
return roles.filter(item => requiredRoles.findIndex(required => required === item.id) !== -1)
}
/** /**
* 判断是否已为方法注入权限认证 * 判断是否已为方法注入权限认证
* @param method * @param method
@ -32,11 +35,40 @@ const auth = function(authConfig, permission, role, permissions, roles) {
const {check, type} = authConfig const {check, type} = authConfig
if (check && typeof check === 'function') { if (check && typeof check === 'function') {
return check.apply(this, [permission, role, permissions, roles]) return check.apply(this, [permission, role, permissions, roles])
} else { }
if (type === 'permission') { if (type === 'permission') {
return permission && permission.operation && permission.operation.indexOf(check) !== -1 return checkFromPermission(check, permission)
} else if (type === 'role') { } else if (type === 'role') {
return role && role.operation && role.operation.indexOf(check) !== -1 return checkFromRoles(check, role)
} else {
return checkFromPermission(check, permission) || checkFromRoles(check, role)
}
}
/**
* 检查权限是否有操作权限
* @param check 需要检查的操作权限
* @param permission 权限
* @returns {boolean}
*/
const checkFromPermission = function(check, permission) {
return permission && permission.operation && permission.operation.indexOf(check) !== -1
}
/**
* 检查 roles 是否有操作权限
* @param check 需要检查的操作权限
* @param roles 角色数组
* @returns {boolean}
*/
const checkFromRoles = function(check, roles) {
if (!roles) {
return false
}
for (let role of roles) {
const {operation} = role
if (operation && operation.indexOf(check) !== -1) {
return true
} }
} }
return false return false
@ -125,9 +157,6 @@ const AuthorityPlugin = {
const roles = this.$store.getters['account/roles'] const roles = this.$store.getters['account/roles']
const permission = getRoutePermission(permissions, this.$route) const permission = getRoutePermission(permissions, this.$route)
const role = getRouteRole(roles, this.$route) const role = getRouteRole(roles, this.$route)
if (!type) {
type = permission ? 'permission' : 'role'
}
return auth.apply(this, [{check, type}, permission, role, permissions, roles]) return auth.apply(this, [{check, type}, permission, role, permissions, roles])
} }
} }

@ -156,7 +156,25 @@ function hasRole(route, roles) {
if (typeof authority === 'object') { if (typeof authority === 'object') {
required = authority.role required = authority.role
} }
return authority === '*' || (required && roles && roles.findIndex(item => item === required || item.id === required) !== -1) return authority === '*' || hasAnyRole(required, roles)
}
/**
* 判断是否有需要的任意一个角色
* @param required {String | Array[String]} 需要的角色可以是单个角色或者一个角色数组
* @param roles 拥有的角色
* @returns {boolean}
*/
function hasAnyRole(required, roles) {
if (!required) {
return false
} else if(Array.isArray(required)) {
return roles.findIndex(role => {
return required.findIndex(item => item === role || item === role.id) !== -1
}) !== -1
} else {
return roles.findIndex(role => role === required || role.id === required) !== -1
}
} }
/** /**
@ -174,6 +192,10 @@ function formatAuthority(routes) {
authority.permission = meta.authority authority.permission = meta.authority
} else if (typeof meta.authority === 'object') { } else if (typeof meta.authority === 'object') {
authority = meta.authority authority = meta.authority
const {role} = authority
if (typeof role === 'string') {
authority.role = [role]
}
} else { } else {
console.log(typeof meta.authority) console.log(typeof meta.authority)
} }

Loading…
Cancel
Save