mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.3 KiB
45 lines
1.3 KiB
2 years ago
|
import type { Permission } from './rolePermissions'
|
||
2 years ago
|
import rolePermissions from './rolePermissions'
|
||
2 years ago
|
import { USER_PROJECT_ROLES, useGlobal, useState } from '#imports'
|
||
2 years ago
|
|
||
2 years ago
|
export function useUIPermission() {
|
||
2 years ago
|
const { user, previewAs } = useGlobal()
|
||
|
|
||
2 years ago
|
const projectRoles = useState<Record<string, boolean>>(USER_PROJECT_ROLES, () => ({}))
|
||
2 years ago
|
|
||
2 years ago
|
const baseRoles = computed(() => {
|
||
2 years ago
|
let userRoles = user.value?.roles || {}
|
||
|
|
||
2 years ago
|
// 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
|
||
2 years ago
|
const roles = {
|
||
2 years ago
|
...userRoles,
|
||
2 years ago
|
...projectRoles.value,
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
return roles
|
||
|
})
|
||
|
|
||
|
const isUIAllowed = (permission: Permission | string, skipPreviewAs = false) => {
|
||
|
let roles = baseRoles.value
|
||
2 years ago
|
if (previewAs.value && !skipPreviewAs) {
|
||
2 years ago
|
roles = {
|
||
2 years ago
|
[previewAs.value]: true,
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return Object.entries<boolean>(roles).some(([role, hasRole]) => {
|
||
2 years ago
|
const rolePermission = rolePermissions[role as keyof typeof rolePermissions] as '*' | Record<Permission, true>
|
||
2 years ago
|
return hasRole && (rolePermission === '*' || rolePermission?.[permission as Permission])
|
||
2 years ago
|
})
|
||
|
}
|
||
|
|
||
|
return { isUIAllowed }
|
||
|
}
|