Browse Source

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

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

30
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 }
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) {
roles = {
[previewAs.value]: true,
}
hasPreviewPermission = hasPermission(previewAs.value, true, permission)
}
return Object.entries(roles).some(([role, hasRole]) => {
const rolePermission = rolePermissions[role as Role | ProjectRole]
return (
hasRole &&
rolePermission &&
((isString(rolePermission) && rolePermission === '*') || rolePermission[permission as keyof typeof rolePermission])
)
})
return (
hasPreviewPermission ||
Object.entries(allRoles.value).some(([role, hasRole]) => hasPermission(role as Role | ProjectRole, hasRole, permission))
)
}
return { isUIAllowed }

Loading…
Cancel
Save