Browse Source

refactor(nc-gui): remove `useInjectionState` from `useUIPermission`

pull/3703/head
braks 2 years ago
parent
commit
58210be87a
  1. 30
      packages/nc-gui/composables/useUIPermission/index.ts

30
packages/nc-gui/composables/useUIPermission/index.ts

@ -1,23 +1,23 @@
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 hasPermission = (role: Role | ProjectRole, hasRole: boolean, permission: Permission | string) => {
const rolePermission = rolePermissions[role]
const { allRoles } = useRoles()
if (!hasRole || !rolePermission) return false
const hasPermission = (role: Role | ProjectRole, hasRole: boolean, permission: Permission | string) => {
const rolePermission = rolePermissions[role]
if (isString(rolePermission) && rolePermission === '*') return true
if (!hasRole || !rolePermission) return false
return rolePermission[permission as keyof typeof rolePermission]
}
if (isString(rolePermission) && rolePermission === '*') return true
export function useUIPermission() {
const { previewAs } = useGlobal()
return rolePermission[permission as keyof typeof rolePermission]
}
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 }
}

Loading…
Cancel
Save