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

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

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

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

@ -1,13 +1,13 @@
import type { FilterType } from 'nocodb-sdk'
import type { I18n } from 'vue-i18n'
import type { Role } from './enums'
import type { ProjectRole, Role } from './enums'
export interface User {
id: string
email: string
firstname: string | null
lastname: string | null
roles: Roles
roles: Roles | string
invite_token?: string
project_id?: string
}
@ -31,7 +31,7 @@ export interface Field {
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 }

Loading…
Cancel
Save