feat: 优化鉴权

This commit is contained in:
ibuler
2020-06-04 15:36:53 +08:00
parent ff39a2a19a
commit 154d118949
5 changed files with 28 additions and 17 deletions

View File

@@ -73,6 +73,18 @@ export function getAdminOrUserPageRole(allRoles, page) {
return allRoles[1]
}
export function hasPerm(source, target) {
if (typeof source !== 'object') {
source = [source]
}
if (typeof target !== 'object') {
target = [target]
}
const totalSource = sumPerms(source)
const totalTarget = sumPerms(target)
return (totalTarget & totalSource) === totalTarget
}
export function sumPerms(perms) {
let sum = 0
for (const perm of perms) {
@@ -86,5 +98,5 @@ export default {
SUPER_ADMIN, SUPER_AUDITOR, ORG_ADMIN, ORG_AUDITOR,
USER, ANON,
getRolesDisplay, getPermsToRolesDisplay, getAdminOrUserPageRole,
parseUserRoles, sumPerms
parseUserRoles, sumPerms, hasPerm
}

View File

@@ -106,15 +106,18 @@ function cleanCurrentRole(allRoles) {
let currentRole = store.getters.currentRole
// 没有的话就应该选择一个
console.log('Curernt ROle', currentRole)
// console.log('Curernt Role', currentRole)
if (!currentRole && typeof currentRole !== 'number') {
currentRole = rolec.getAdminOrUserPageRole(allRoles)
console.log('1')
} else {
console.log('2')
if ((currentRole & rolec.sumPerms(allRoles)) !== currentRole) {
currentRole = rolec.getAdminOrUserPageRole(allRoles)
}
}
const hasAudit = rolec.hasPerm(currentRole, rolec.PERM_AUDIT)
const hasAdmin = rolec.hasPerm(allRoles, rolec.PERM_ADMIN)
// 这代表上次用户登录了auditor这次切换了
if (hasAudit && hasAdmin) {
currentRole = rolec.getAdminOrUserPageRole(allRoles)
}
if (!rolec.hasPerm(allRoles, currentRole)) {
currentRole = rolec.getAdminOrUserPageRole(allRoles)
}
store.dispatch('users/setCurrentRole', currentRole)
return [currentRole]