fix: fixed the issue that the router permission configuration does not support arrays

; 🐛
修复:修复路由权限配置不支持数组的问题;
This commit is contained in:
chenghongxing 2023-03-15 14:41:11 +08:00
parent d7a530db46
commit a4d4b28ce3
2 changed files with 13 additions and 13 deletions

View File

@ -8,10 +8,12 @@ function hasPermission(authority, permissions) {
let required = '*' let required = '*'
if (typeof authority === 'string') { if (typeof authority === 'string') {
required = authority required = authority
} else if (Array.isArray(authority)) {
required = authority
} else if (typeof authority === 'object') { } else if (typeof authority === 'object') {
required = authority.permission required = authority.permission
} }
return required === '*' || (permissions && permissions.findIndex(item => item === required || item.id === required) !== -1) return required === '*' || hasAnyItem(required, permissions, (r, t) => !!(r === t || r === t.id))
} }
/** /**
@ -24,25 +26,23 @@ function hasRole(authority, roles) {
if (typeof authority === 'object') { if (typeof authority === 'object') {
required = authority.role required = authority.role
} }
return authority === '*' || hasAnyRole(required, roles) return authority === '*' || hasAnyItem(required, roles, (r, t) => !!(r === t || r === t.id))
} }
/** /**
* 判断是否有需要的任意一个角色 * 判断目标数组是否有所需元素
* @param required {String | Array[String]} 需要的角色可以是单个角色或者一个角色数组 * @param {String | String[]}required 所需元素数组或单个元素
* @param roles 拥有的角色 * @param {String[]|Object[]} source 目标数组
* @param {Function} filter 匹配条件
* (r: String, s: String|Object) => boolean
* @returns {boolean} * @returns {boolean}
*/ */
function hasAnyRole(required, roles) { function hasAnyItem(required, source, filter) {
if (!required) { if (!required) {
return false 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
} }
let checkedList = Array.isArray(required) ? required : [required]
return !!source.find(s => checkedList.find(r => filter(r, s)))
} }
/** /**

View File

@ -212,7 +212,7 @@ function formatAuthority(routes, pAuthorities = []) {
let authority = {} let authority = {}
if (!meta.authority) { if (!meta.authority) {
authority = defaultAuthority authority = defaultAuthority
}else if (typeof meta.authority === 'string') { }else if (typeof meta.authority === 'string' || Array.isArray(meta.authority)) {
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