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