From e737e043f1ee12c510ce8060dfdc0c47dc3090fe Mon Sep 17 00:00:00 2001 From: mertmit Date: Wed, 20 Sep 2023 19:11:43 +0000 Subject: [PATCH] fix: user role state --- packages/nc-gui/composables/useRoles/index.ts | 8 ++++++++ packages/nc-gui/middleware/auth.global.ts | 6 +++--- packages/nc-gui/store/users.ts | 13 ++----------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/packages/nc-gui/composables/useRoles/index.ts b/packages/nc-gui/composables/useRoles/index.ts index 4dd532919a..c80212a318 100644 --- a/packages/nc-gui/composables/useRoles/index.ts +++ b/packages/nc-gui/composables/useRoles/index.ts @@ -111,6 +111,14 @@ export const useRoles = createSharedComposable(() => { } else if (projectId) { const res = await api.auth.me({ project_id: projectId }) + user.value = { + ...user.value, + roles: res.roles, + project_roles: res.project_roles, + } as typeof User + } else { + const res = await api.auth.me({}) + user.value = { ...user.value, roles: res.roles, diff --git a/packages/nc-gui/middleware/auth.global.ts b/packages/nc-gui/middleware/auth.global.ts index 64702addf7..a52ae0e087 100644 --- a/packages/nc-gui/middleware/auth.global.ts +++ b/packages/nc-gui/middleware/auth.global.ts @@ -34,7 +34,7 @@ export default defineNuxtRouteMiddleware(async (to, from) => { const { api } = useApi({ useGlobalInstance: true }) - const { allRoles } = useRoles() + const { allRoles, loadRoles } = useRoles() /** If baseHostname defined block home page access under subdomains, and redirect to workspace page */ if ( @@ -92,9 +92,9 @@ export default defineNuxtRouteMiddleware(async (to, from) => { /** if users are accessing the projects without having enough permissions, redirect to My Projects page */ if (to.params.projectId && from.params.projectId !== to.params.projectId) { - const user = await api.auth.me({ project_id: to.params.projectId as string }) + await loadRoles() - if (user?.roles?.guest) { + if (state.user.value?.roles?.guest) { message.error("You don't have enough permission to access the project.") return navigateTo('/') diff --git a/packages/nc-gui/store/users.ts b/packages/nc-gui/store/users.ts index 208d9e6ccf..d209429a41 100644 --- a/packages/nc-gui/store/users.ts +++ b/packages/nc-gui/store/users.ts @@ -3,6 +3,7 @@ import { acceptHMRUpdate, defineStore } from 'pinia' export const useUsers = defineStore('userStore', () => { const { api } = useApi() const { user } = useGlobal() + const { loadRoles } = useRoles() const updateUserProfile = async ({ attrs, @@ -21,17 +22,7 @@ export const useUsers = defineStore('userStore', () => { } } - const loadCurrentUser = async () => { - const res = await api.auth.me() - - user.value = { - ...user.value, - ...res, - roles: res.roles, - project_roles: res.project_roles, - workspace_roles: res.workspace_roles, - } - } + const loadCurrentUser = loadRoles watch( () => user.value?.id,