Browse Source

feat(gui-v2): add proper role permissions type

pull/2877/head
braks 2 years ago
parent
commit
2520a8ed6c
  1. 12
      packages/nc-gui-v2/composables/useUIPermission/index.ts
  2. 13
      packages/nc-gui-v2/composables/useUIPermission/rolePermissions.ts

12
packages/nc-gui-v2/composables/useUIPermission/index.ts

@ -1,3 +1,4 @@
import type { RolePermissions } from './rolePermissions'
import rolePermissions from './rolePermissions' import rolePermissions from './rolePermissions'
import { useState } from '#app' import { useState } from '#app'
import { USER_PROJECT_ROLES } from '~/lib/constants' import { USER_PROJECT_ROLES } from '~/lib/constants'
@ -6,7 +7,7 @@ export function useUIPermission() {
const { $state } = useNuxtApp() const { $state } = useNuxtApp()
const projectRoles = useState<Record<string, boolean>>(USER_PROJECT_ROLES, () => ({})) const projectRoles = useState<Record<string, boolean>>(USER_PROJECT_ROLES, () => ({}))
const isUIAllowed = (permission: string, _skipPreviewAs = false) => { const isUIAllowed = (permission: RolePermissions, _skipPreviewAs = false) => {
const user = $state.user const user = $state.user
let userRoles = user?.value?.roles || {} let userRoles = user?.value?.roles || {}
@ -32,7 +33,14 @@ export function useUIPermission() {
// } // }
return Object.entries(roles).some(([role, hasRole]) => { return Object.entries(roles).some(([role, hasRole]) => {
return hasRole && (rolePermissions[role] === '*' || (rolePermissions[role] as Record<string, boolean>)?.[permission]) const rolePermission = rolePermissions[role as keyof typeof rolePermissions]
return (
hasRole &&
(rolePermission === '*' || typeof rolePermission === 'object'
? rolePermission[permission as keyof typeof rolePermission] === true
: false)
)
}) })
} }

13
packages/nc-gui-v2/composables/useUIPermission/rolePermissions.ts

@ -1,4 +1,4 @@
const permissions: Record<string, Record<string, boolean> | '*'> = { const rolePermissions = {
creator: '*', creator: '*',
owner: '*', owner: '*',
guest: {}, guest: {},
@ -36,4 +36,13 @@ const permissions: Record<string, Record<string, boolean> | '*'> = {
}, },
} }
export default permissions export default rolePermissions
type GetKeys<T> = T extends Record<string, any> ? keyof T : never
export type RolePermissions<T extends typeof rolePermissions = typeof rolePermissions, K extends keyof T = keyof T> =
| T[K] extends string
? T[K]
: never & T[K] extends Record<string, any>
? GetKeys<T[K]>
: never

Loading…
Cancel
Save