Browse Source

Merge pull request #2940 from nocodb/fix/permission-type

pull/2945/head
Braks 2 years ago committed by GitHub
parent
commit
fe791baff3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      packages/nc-gui-v2/composables/useUIPermission/index.ts
  2. 7
      packages/nc-gui-v2/composables/useUIPermission/rolePermissions.ts

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

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

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

@ -40,9 +40,10 @@ 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
export type Permission<T extends typeof rolePermissions = typeof rolePermissions, K extends keyof T = keyof T> = K extends
| 'creator'
| 'owner'
? T[K]
: never & T[K] extends Record<string, any>
: never | T[K] extends Record<string, any>
? GetKeys<T[K]>
: never

Loading…
Cancel
Save