Browse Source

feat(gui): update method to support custom role

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5885/head
Pranav C 1 year ago
parent
commit
481abfe87b
  1. 26
      packages/nc-gui/composables/useUIPermission/index.ts

26
packages/nc-gui/composables/useUIPermission/index.ts

@ -24,14 +24,32 @@ export const useUIPermission = createSharedComposable(() => {
const { previewAs } = useGlobal()
const { allRoles } = useRoles()
const isUIAllowed = (permission: Permission | string, skipPreviewAs = false) => {
const isUIAllowed = (
permission: Permission | string,
skipPreviewAs = false,
userRoles?: string | Record<string, boolean> | string[],
) => {
if (previewAs.value && !skipPreviewAs) {
return hasPermission(previewAs.value, true, permission)
}
return Object.entries(allRoles.value).some(([role, hasRole]) =>
hasPermission(role as Role | ProjectRole, hasRole, permission),
)
let roles: Record<string, boolean> = {}
if (!userRoles) {
roles = allRoles.value
} else if (Array.isArray(userRoles) || typeof userRoles === 'string') {
roles = (Array.isArray(userRoles) ? userRoles : userRoles.split(','))
// filter out any empty-string/null/undefined values
.filter(Boolean)
.reduce<Record<string, boolean>>((acc, role) => {
acc[role] = true
return acc
}, {})
} else if (typeof userRoles === 'object') {
roles = userRoles
}
return Object.entries(roles).some(([role, hasRole]) => hasPermission(role as Role | ProjectRole, hasRole, permission))
}
return { isUIAllowed }

Loading…
Cancel
Save