mirror of
https://github.com/chansee97/nova-admin.git
synced 2025-11-19 07:19:51 +08:00
feat: permission directive
This commit is contained in:
parent
7dd0301fb2
commit
1738bed2ca
@ -4,7 +4,7 @@ import { usePermission } from '@/hooks'
|
||||
export function install(app: App) {
|
||||
const { hasPermission } = usePermission()
|
||||
|
||||
function updatapermission(el: HTMLElement, permission: Entity.RoleType | Entity.RoleType[]) {
|
||||
function updatapermission(el: HTMLElement, permission: string | string[]) {
|
||||
if (!permission)
|
||||
throw new Error('v-permissson Directive with no explicit role attached')
|
||||
|
||||
@ -12,13 +12,10 @@ export function install(app: App) {
|
||||
el.parentElement?.removeChild(el)
|
||||
}
|
||||
|
||||
const permissionDirective: Directive<HTMLElement, Entity.RoleType | Entity.RoleType[]> = {
|
||||
const permissionDirective: Directive<HTMLElement, string | string[]> = {
|
||||
mounted(el, binding) {
|
||||
updatapermission(el, binding.value)
|
||||
},
|
||||
updated(el, binding) {
|
||||
updatapermission(el, binding.value)
|
||||
},
|
||||
}
|
||||
app.directive('permission', permissionDirective)
|
||||
}
|
||||
|
||||
@ -1,32 +1,25 @@
|
||||
import { useAuthStore } from '@/store'
|
||||
import { isArray, isString } from 'radash'
|
||||
|
||||
/** 权限判断 */
|
||||
export function usePermission() {
|
||||
const authStore = useAuthStore()
|
||||
|
||||
function hasPermission(
|
||||
permission?: Entity.RoleType | Entity.RoleType[],
|
||||
permissions?: string | string[],
|
||||
) {
|
||||
if (!permission)
|
||||
if (!permissions)
|
||||
return true
|
||||
|
||||
if (!authStore.userInfo)
|
||||
return false
|
||||
const { roles } = authStore.userInfo
|
||||
// 全部权限
|
||||
if (permissions === '*:*:*')
|
||||
return true
|
||||
|
||||
// 角色为super可直接通过
|
||||
let has = roles.includes('super')
|
||||
if (!has) {
|
||||
if (isArray(permission))
|
||||
// 角色为数组, 判断是否有交集
|
||||
has = permission.some(i => roles.includes(i))
|
||||
const { permissions: userPermissions } = authStore
|
||||
|
||||
if (isString(permission))
|
||||
// 角色为字符串, 判断是否包含
|
||||
has = roles.includes(permission)
|
||||
}
|
||||
return has
|
||||
// 确保 permissions 是数组
|
||||
const permissionArray = Array.isArray(permissions) ? permissions : [permissions]
|
||||
|
||||
return permissionArray.some(i => userPermissions.includes(i))
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@ -6,11 +6,13 @@ import { useTabStore } from './tab'
|
||||
|
||||
interface AuthStatus {
|
||||
userInfo: Entity.User | Record<string, any>
|
||||
permissions: string[]
|
||||
}
|
||||
export const useAuthStore = defineStore('auth-store', {
|
||||
state: (): AuthStatus => {
|
||||
return {
|
||||
userInfo: {},
|
||||
permissions: [],
|
||||
}
|
||||
},
|
||||
getters: {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user