From 2520a8ed6c6c57a1bcc5bef99297a243b386ca38 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Sat, 30 Jul 2022 00:04:52 +0200 Subject: [PATCH] feat(gui-v2): add proper role permissions type --- .../nc-gui-v2/composables/useUIPermission/index.ts | 12 ++++++++++-- .../composables/useUIPermission/rolePermissions.ts | 13 +++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/nc-gui-v2/composables/useUIPermission/index.ts b/packages/nc-gui-v2/composables/useUIPermission/index.ts index c2fc037893..f2bdd4d8a6 100644 --- a/packages/nc-gui-v2/composables/useUIPermission/index.ts +++ b/packages/nc-gui-v2/composables/useUIPermission/index.ts @@ -1,3 +1,4 @@ +import type { RolePermissions } from './rolePermissions' import rolePermissions from './rolePermissions' import { useState } from '#app' import { USER_PROJECT_ROLES } from '~/lib/constants' @@ -6,7 +7,7 @@ export function useUIPermission() { const { $state } = useNuxtApp() const projectRoles = useState>(USER_PROJECT_ROLES, () => ({})) - const isUIAllowed = (permission: string, _skipPreviewAs = false) => { + const isUIAllowed = (permission: RolePermissions, _skipPreviewAs = false) => { const user = $state.user let userRoles = user?.value?.roles || {} @@ -32,7 +33,14 @@ export function useUIPermission() { // } return Object.entries(roles).some(([role, hasRole]) => { - return hasRole && (rolePermissions[role] === '*' || (rolePermissions[role] as Record)?.[permission]) + const rolePermission = rolePermissions[role as keyof typeof rolePermissions] + + return ( + hasRole && + (rolePermission === '*' || typeof rolePermission === 'object' + ? rolePermission[permission as keyof typeof rolePermission] === true + : false) + ) }) } diff --git a/packages/nc-gui-v2/composables/useUIPermission/rolePermissions.ts b/packages/nc-gui-v2/composables/useUIPermission/rolePermissions.ts index 8fedbaddd0..9b29520f74 100644 --- a/packages/nc-gui-v2/composables/useUIPermission/rolePermissions.ts +++ b/packages/nc-gui-v2/composables/useUIPermission/rolePermissions.ts @@ -1,4 +1,4 @@ -const permissions: Record | '*'> = { +const rolePermissions = { creator: '*', owner: '*', guest: {}, @@ -36,4 +36,13 @@ const permissions: Record | '*'> = { }, } -export default permissions +export default rolePermissions + +type GetKeys = T extends Record ? keyof T : never + +export type RolePermissions = + | T[K] extends string + ? T[K] + : never & T[K] extends Record + ? GetKeys + : never