研发图纸文件管理系统-前端项目
chenghongxing
2020-08-26 2021fb575db9a19548a361f820e1256c6370999d
fix: problem that can't set roles array for route's authority; :bug:
修复:路由权限认证无法设置角色数组的问题;
2个文件已修改
71 ■■■■ 已修改文件
src/plugins/authority-plugin.js 47 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/utils/routerUtil.js 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/plugins/authority-plugin.js
@@ -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 {check, type} = authConfig
  if (check && typeof check === 'function') {
    return check.apply(this, [permission, role, permissions, roles])
  } else {
  }
    if (type === 'permission') {
      return permission && permission.operation && permission.operation.indexOf(check) !== -1
    return checkFromPermission(check, permission)
    } 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
@@ -125,9 +157,6 @@
          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])
        }
      }
src/utils/routerUtil.js
@@ -156,7 +156,25 @@
  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 @@
        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)
      }