Browse Source

Merge pull request #2796 from nocodb/feat/get-project-roles

feat(gui-v2): get project roles
pull/2803/head
navi 2 years ago committed by GitHub
parent
commit
42f08eb3c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      packages/nc-gui-v2/composables/useProject.ts
  2. 20
      packages/nc-gui-v2/composables/useUIPermission/index.ts
  3. 1
      packages/nc-gui-v2/lib/constants.ts

16
packages/nc-gui-v2/composables/useProject.ts

@ -1,28 +1,38 @@
import { SqlUiFactory } from 'nocodb-sdk' import { SqlUiFactory } from 'nocodb-sdk'
import type { ProjectType, TableType } from 'nocodb-sdk' import type { ProjectType, TableType } from 'nocodb-sdk'
import { useNuxtApp } from '#app' import { useNuxtApp, useState } from '#app'
import { USER_PROJECT_ROLES } from '~/lib/constants'
export default () => { export default () => {
const projectRoles = useState<Record<string, boolean>>(USER_PROJECT_ROLES, () => ({}))
const { $api } = useNuxtApp() const { $api } = useNuxtApp()
const project = useState<ProjectType>('project') const project = useState<ProjectType>('project')
const tables = useState<TableType[]>('tables') const tables = useState<TableType[]>('tables')
const loadProjectRoles = async () => {
projectRoles.value = {}
if (project.value.id) {
const user = await $api.auth.me({ project_id: project.value.id })
projectRoles.value = user.roles
}
}
const loadTables = async () => { const loadTables = async () => {
if (project.value.id) { if (project.value.id) {
const tablesResponse = await $api.dbTable.list(project.value.id) const tablesResponse = await $api.dbTable.list(project.value.id)
if (tablesResponse.list) tables.value = tablesResponse.list if (tablesResponse.list) tables.value = tablesResponse.list
} }
} }
const loadProject = async (projectId: string) => { const loadProject = async (projectId: string) => {
project.value = await $api.project.read(projectId) project.value = await $api.project.read(projectId)
await loadProjectRoles()
} }
const isMysql = computed(() => ['mysql', 'mysql2'].includes(project.value?.bases?.[0]?.type || '')) const isMysql = computed(() => ['mysql', 'mysql2'].includes(project.value?.bases?.[0]?.type || ''))
const isPg = computed(() => project.value?.bases?.[0]?.type === 'pg') const isPg = computed(() => project.value?.bases?.[0]?.type === 'pg')
const sqlUi = computed(() => SqlUiFactory.create({ client: project.value?.bases?.[0]?.type || '' })) const sqlUi = computed(() => SqlUiFactory.create({ client: project.value?.bases?.[0]?.type || '' }))
return { project, tables, loadProject, loadTables, isMysql, isPg, sqlUi } return { project, tables, loadProject, loadTables, isMysql, isPg, sqlUi }

20
packages/nc-gui-v2/composables/useUIPermission/index.ts

@ -1,14 +1,27 @@
import rolePermissions from './rolePermissions' import rolePermissions from './rolePermissions'
import { useState } from '#app'
import { USER_PROJECT_ROLES } from '~/lib/constants'
export default () => { export default () => {
const { $state } = useNuxtApp() const { $state } = useNuxtApp()
const projectRoles = useState<Record<string, boolean>>(USER_PROJECT_ROLES, () => ({}))
const isUIAllowed = (permission: keyof typeof rolePermissions[keyof typeof rolePermissions], _skipPreviewAs = false) => { const isUIAllowed = (permission: keyof typeof rolePermissions[keyof typeof rolePermissions], _skipPreviewAs = false) => {
const user = $state.user const user = $state.user
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) => {
acc[role] = true
return acc
}, {})
}
// merge user role and project specific user roles
const roles = { const roles = {
...(user?.value?.roles || {}), ...userRoles,
// todo: load project specific roles ...(projectRoles?.value || {}),
// ...(state.projectRole || {}),
} }
// todo: handle preview as // todo: handle preview as
@ -17,6 +30,7 @@ export default () => {
// [state.previewAs]: true // [state.previewAs]: true
// }; // };
// } // }
return Object.entries(roles).some(([role, hasRole]) => { return Object.entries(roles).some(([role, hasRole]) => {
return ( return (
hasRole && hasRole &&

1
packages/nc-gui-v2/lib/constants.ts

@ -1 +1,2 @@
export const NOCO = 'noco' export const NOCO = 'noco'
export const USER_PROJECT_ROLES = 'user_project_roles'

Loading…
Cancel
Save