|
|
|
@ -1,15 +1,10 @@
|
|
|
|
|
import { isString } from '@vueuse/core' |
|
|
|
|
import type { Permission } from './rolePermissions' |
|
|
|
|
import rolePermissions from './rolePermissions' |
|
|
|
|
import { useGlobal, useInjectionState, useRoles } from '#imports' |
|
|
|
|
import { useGlobal, useRoles } from '#imports' |
|
|
|
|
import type { ProjectRole, Role } from '~/lib' |
|
|
|
|
|
|
|
|
|
const [setup, use] = useInjectionState(() => { |
|
|
|
|
const { previewAs } = useGlobal() |
|
|
|
|
|
|
|
|
|
const { allRoles } = useRoles() |
|
|
|
|
|
|
|
|
|
const hasPermission = (role: Role | ProjectRole, hasRole: boolean, permission: Permission | string) => { |
|
|
|
|
const hasPermission = (role: Role | ProjectRole, hasRole: boolean, permission: Permission | string) => { |
|
|
|
|
const rolePermission = rolePermissions[role] |
|
|
|
|
|
|
|
|
|
if (!hasRole || !rolePermission) return false |
|
|
|
@ -17,7 +12,12 @@ const [setup, use] = useInjectionState(() => {
|
|
|
|
|
if (isString(rolePermission) && rolePermission === '*') return true |
|
|
|
|
|
|
|
|
|
return rolePermission[permission as keyof typeof rolePermission] |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function useUIPermission() { |
|
|
|
|
const { previewAs } = useGlobal() |
|
|
|
|
|
|
|
|
|
const { allRoles } = useRoles() |
|
|
|
|
|
|
|
|
|
const isUIAllowed = (permission: Permission | string, skipPreviewAs = false) => { |
|
|
|
|
if (previewAs.value && !skipPreviewAs) { |
|
|
|
@ -32,14 +32,4 @@ const [setup, use] = useInjectionState(() => {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return { isUIAllowed } |
|
|
|
|
}, 'useUIPermission') |
|
|
|
|
|
|
|
|
|
export function useUIPermission() { |
|
|
|
|
let usePermissions = use() |
|
|
|
|
|
|
|
|
|
if (!usePermissions) { |
|
|
|
|
usePermissions = setup() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return { isUIAllowed: usePermissions.isUIAllowed } |
|
|
|
|
} |
|
|
|
|