Browse Source

fix: user role state

pull/6378/head
mertmit 1 year ago
parent
commit
e737e043f1
  1. 8
      packages/nc-gui/composables/useRoles/index.ts
  2. 6
      packages/nc-gui/middleware/auth.global.ts
  3. 13
      packages/nc-gui/store/users.ts

8
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,

6
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('/')

13
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,

Loading…
Cancel
Save