Browse Source

refactor(nc-gui): stop duplicating allRoles & check preview permissions separately

pull/3703/head
braks 2 years ago
parent
commit
92110603ee
  1. 26
      packages/nc-gui/composables/useUIPermission/index.ts

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

@ -29,24 +29,26 @@ export function useUIPermission() {
}),
)
const isUIAllowed = (permission: Permission | string, skipPreviewAs = false) => {
let roles = { ...allRoles.value }
if (previewAs.value && !skipPreviewAs) {
roles = {
[previewAs.value]: true,
}
}
return Object.entries(roles).some(([role, hasRole]) => {
const rolePermission = rolePermissions[role as Role | ProjectRole]
const hasPermission = (role: Role | ProjectRole, hasRole: boolean, permission: Permission | string) => {
const rolePermission = rolePermissions[role]
return (
hasRole &&
rolePermission &&
((isString(rolePermission) && rolePermission === '*') || rolePermission[permission as keyof typeof rolePermission])
)
})
}
const isUIAllowed = (permission: Permission | string, skipPreviewAs = false) => {
let hasPreviewPermission = false
if (previewAs.value && !skipPreviewAs) {
hasPreviewPermission = hasPermission(previewAs.value, true, permission)
}
return (
hasPreviewPermission ||
Object.entries(allRoles.value).some(([role, hasRole]) => hasPermission(role as Role | ProjectRole, hasRole, permission))
)
}
return { isUIAllowed }

Loading…
Cancel
Save