mirror of
https://github.com/iczer/vue-antd-admin
synced 2025-04-06 04:00:06 +08:00
fix: problem that can't set roles array for route's authority; 🐛
修复:路由权限认证无法设置角色数组的问题;
This commit is contained in:
parent
ec3e4b5571
commit
2021fb575d
src
@ -2,16 +2,19 @@
|
||||
* 获取路由需要的权限
|
||||
* @param permissions
|
||||
* @param route
|
||||
* @returns {*}
|
||||
* @returns {Permission}
|
||||
*/
|
||||
const getRoutePermission = (permissions, route) => permissions.find(item => item.id === route.meta.authority.permission)
|
||||
/**
|
||||
* 获取路由需要的角色
|
||||
* @param roles
|
||||
* @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
|
||||
@ -32,11 +35,40 @@ const auth = function(authConfig, permission, role, permissions, roles) {
|
||||
const {check, type} = authConfig
|
||||
if (check && typeof check === 'function') {
|
||||
return check.apply(this, [permission, role, permissions, roles])
|
||||
}
|
||||
if (type === 'permission') {
|
||||
return checkFromPermission(check, permission)
|
||||
} else if (type === 'role') {
|
||||
return checkFromRoles(check, role)
|
||||
} else {
|
||||
if (type === 'permission') {
|
||||
return permission && permission.operation && permission.operation.indexOf(check) !== -1
|
||||
} else if (type === 'role') {
|
||||
return role && role.operation && role.operation.indexOf(check) !== -1
|
||||
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
|
||||
@ -125,9 +157,6 @@ const AuthorityPlugin = {
|
||||
const roles = this.$store.getters['account/roles']
|
||||
const permission = getRoutePermission(permissions, this.$route)
|
||||
const role = getRouteRole(roles, this.$route)
|
||||
if (!type) {
|
||||
type = permission ? 'permission' : 'role'
|
||||
}
|
||||
return auth.apply(this, [{check, type}, permission, role, permissions, roles])
|
||||
}
|
||||
}
|
||||
|
@ -156,7 +156,25 @@ function hasRole(route, roles) {
|
||||
if (typeof authority === 'object') {
|
||||
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
|
||||
} else if (typeof meta.authority === 'object') {
|
||||
authority = meta.authority
|
||||
const {role} = authority
|
||||
if (typeof role === 'string') {
|
||||
authority.role = [role]
|
||||
}
|
||||
} else {
|
||||
console.log(typeof meta.authority)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user