From 2021fb575db9a19548a361f820e1256c6370999d Mon Sep 17 00:00:00 2001 From: chenghongxing <1126263215@qq.com> Date: Wed, 26 Aug 2020 21:53:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20problem=20that=20can't=20set=20roles=20a?= =?UTF-8?q?rray=20for=20route's=20authority;=20:bug:=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=EF=BC=9A=E8=B7=AF=E7=94=B1=E6=9D=83=E9=99=90=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=AE=BE=E7=BD=AE=E8=A7=92=E8=89=B2=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/authority-plugin.js | 49 ++++++++++++++++++++++++++------- src/utils/routerUtil.js | 24 +++++++++++++++- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/plugins/authority-plugin.js b/src/plugins/authority-plugin.js index 65cba84..36996d4 100644 --- a/src/plugins/authority-plugin.js +++ b/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 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]) } } diff --git a/src/utils/routerUtil.js b/src/utils/routerUtil.js index d6a5172..39355a1 100644 --- a/src/utils/routerUtil.js +++ b/src/utils/routerUtil.js @@ -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) }