Browse Source

refactor(gui-v2): isUIAllowed

pull/3234/head
Wing-Kam Wong 2 years ago
parent
commit
fe6f1282e4
  1. 16
      packages/nc-gui-v2/composables/useUIPermission/index.ts

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

@ -7,7 +7,7 @@ export function useUIPermission() {
const projectRoles = useState<Record<string, boolean>>(USER_PROJECT_ROLES, () => ({})) const projectRoles = useState<Record<string, boolean>>(USER_PROJECT_ROLES, () => ({}))
const getRoles = (skipPreviewAs = false) => { const baseRoles = computed(() => {
let userRoles = user.value?.roles || {} let userRoles = user.value?.roles || {}
// if string populate key-value paired object // if string populate key-value paired object
@ -19,24 +19,24 @@ export function useUIPermission() {
} }
// merge user role and project specific user roles // merge user role and project specific user roles
let roles = { const roles = {
...userRoles, ...userRoles,
...projectRoles.value, ...projectRoles.value,
} }
return roles
})
const isUIAllowed = (permission: Permission | string, skipPreviewAs = false) => {
let roles = baseRoles.value
if (previewAs.value && !skipPreviewAs) { if (previewAs.value && !skipPreviewAs) {
roles = { roles = {
[previewAs.value]: true, [previewAs.value]: true,
} }
} }
return roles return Object.entries<boolean>(roles).some(([role, hasRole]) => {
}
const isUIAllowed = (permission: Permission | string, skipPreviewAs = false) => {
return Object.entries<boolean>(getRoles(skipPreviewAs)).some(([role, hasRole]) => {
const rolePermission = rolePermissions[role as keyof typeof rolePermissions] as '*' | Record<Permission, true> const rolePermission = rolePermissions[role as keyof typeof rolePermissions] as '*' | Record<Permission, true>
return hasRole && (rolePermission === '*' || rolePermission?.[permission as Permission]) return hasRole && (rolePermission === '*' || rolePermission?.[permission as Permission])
}) })
} }

Loading…
Cancel
Save