Browse Source

refactor(nc-gui): clean up permission types

pull/3703/head
braks 2 years ago
parent
commit
27791f5054
  1. 25
      packages/nc-gui/composables/useUIPermission/index.ts
  2. 14
      packages/nc-gui/composables/useUIPermission/rolePermissions.ts
  3. 1
      packages/nc-gui/lib/enums.ts
  4. 6
      packages/nc-gui/lib/types.ts

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

@ -2,19 +2,19 @@ import { isString } from '@vueuse/core'
import type { Permission } from './rolePermissions' import type { Permission } from './rolePermissions'
import rolePermissions from './rolePermissions' import rolePermissions from './rolePermissions'
import { USER_PROJECT_ROLES, computed, useGlobal, useState } from '#imports' import { USER_PROJECT_ROLES, computed, useGlobal, useState } from '#imports'
import type { Role, Roles } from '~/lib' import type { Roles } from '~/lib'
export function useUIPermission() { export function useUIPermission() {
const { user, previewAs } = useGlobal() const { user, previewAs } = useGlobal()
const projectRoles = useState<Record<string, boolean>>(USER_PROJECT_ROLES, () => ({})) const projectRoles = useState<Roles>(USER_PROJECT_ROLES, () => ({}))
const baseRoles = computed(() => { const baseRoles = computed<Roles>(() => {
let userRoles = isString(user.value?.roles) ? user.value?.roles : ({ ...(user.value?.roles || {}) } as Roles) let userRoles = user.value?.roles
// if string populate key-value paired object // if string populate key-value paired object
if (typeof userRoles === 'string') { if (isString(userRoles)) {
userRoles = userRoles.split(',').reduce<Record<string, boolean>>((acc, role) => { userRoles = userRoles.split(',').reduce<Roles>((acc, role) => {
acc[role] = true acc[role] = true
return acc return acc
}, {}) }, {})
@ -27,18 +27,19 @@ export function useUIPermission() {
} }
}) })
const isUIAllowed = (permission: Permission | string, skipPreviewAs = false) => { const isUIAllowed = (permission: Permission, skipPreviewAs = false) => {
let roles = baseRoles.value as Record<string, any> let roles = baseRoles.value
if (previewAs.value && !skipPreviewAs) { if (previewAs.value && !skipPreviewAs) {
roles = { roles = {
[previewAs.value as Role]: true, [previewAs.value]: true,
} }
} }
return Object.entries<boolean>(roles).some(([role, hasRole]) => { return Object.entries(roles).some(([role, hasRole]) => {
const rolePermission = rolePermissions[role as keyof typeof rolePermissions] as '*' | Record<Permission, true> const rolePermission = rolePermissions[role as keyof typeof rolePermissions]
return hasRole && (rolePermission === '*' || rolePermission?.[permission as Permission])
return hasRole && (rolePermission === '*' || rolePermission?.[permission as keyof typeof rolePermission])
}) })
} }

14
packages/nc-gui/composables/useUIPermission/rolePermissions.ts

@ -43,14 +43,10 @@ const rolePermissions = {
}, },
} as const } as const
export default rolePermissions type GetKeys<T> = T extends Record<string, boolean> ? keyof T : never
type GetKeys<T> = T extends Record<string, any> ? keyof T : never 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, boolean> ? GetKeys<T[K]> : never)
| string
export type Permission<T extends typeof rolePermissions = typeof rolePermissions, K extends keyof T = keyof T> = K extends export default rolePermissions
| 'creator'
| 'owner'
? T[K]
: never | T[K] extends Record<string, any>
? GetKeys<T[K]>
: never

1
packages/nc-gui/lib/enums.ts

@ -2,6 +2,7 @@ export enum Role {
Super = 'super', Super = 'super',
Admin = 'admin', Admin = 'admin',
User = 'user', User = 'user',
Guest = 'guest',
} }
export enum ProjectRole { export enum ProjectRole {

6
packages/nc-gui/lib/types.ts

@ -1,13 +1,13 @@
import type { FilterType } from 'nocodb-sdk' import type { FilterType } from 'nocodb-sdk'
import type { I18n } from 'vue-i18n' import type { I18n } from 'vue-i18n'
import type { Role } from './enums' import type { ProjectRole, Role } from './enums'
export interface User { export interface User {
id: string id: string
email: string email: string
firstname: string | null firstname: string | null
lastname: string | null lastname: string | null
roles: Roles roles: Roles | string
invite_token?: string invite_token?: string
project_id?: string project_id?: string
} }
@ -31,7 +31,7 @@ export interface Field {
system?: boolean system?: boolean
} }
export type Roles = Record<Role | string, boolean> | string export type Roles = Record<Role | ProjectRole | string, boolean>
export type Filter = FilterType & { status?: 'update' | 'delete' | 'create'; parentId?: string; readOnly?: boolean } export type Filter = FilterType & { status?: 'update' | 'delete' | 'create'; parentId?: string; readOnly?: boolean }

Loading…
Cancel
Save