Browse Source

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

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

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

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

Loading…
Cancel
Save