mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +00:00
66 lines
1.8 KiB
JavaScript
66 lines
1.8 KiB
JavaScript
const alreadyWarned = new Set()
|
|
|
|
export default defineNuxtPlugin({
|
|
name: 'permissions',
|
|
dependsOn: ['i18n', 'store'],
|
|
async setup(nuxtApp) {
|
|
const { $registry, $store } = nuxtApp
|
|
|
|
let hasPermission
|
|
|
|
if (import.meta.env.VITEST) {
|
|
hasPermission = () => true
|
|
} else {
|
|
hasPermission = (operation, context, workspaceId = null) => {
|
|
let perms
|
|
|
|
if (workspaceId === null) {
|
|
perms = $store.getters['auth/getGlobalUserPermissions']
|
|
} else {
|
|
const workspace = $store.getters['workspace/get'](workspaceId)
|
|
if (workspace === undefined || !workspace._.permissionsLoaded) {
|
|
perms = $store.getters['auth/getGlobalUserPermissions']
|
|
} else {
|
|
perms = workspace._.permissions
|
|
}
|
|
}
|
|
|
|
for (const perm of perms) {
|
|
const { name, permissions } = perm
|
|
let manager
|
|
|
|
try {
|
|
manager = $registry.get('permissionManager', name)
|
|
} catch (e) {
|
|
// If a permission manager is missing we show a warning once.
|
|
if (!alreadyWarned.has(name)) {
|
|
alreadyWarned.add(name)
|
|
console.warn(`Permission manager '${name}' missing in registry`)
|
|
}
|
|
continue
|
|
}
|
|
|
|
try {
|
|
const result = manager.hasPermission(
|
|
permissions,
|
|
operation,
|
|
context,
|
|
workspaceId
|
|
)
|
|
|
|
if ([true, false].includes(result)) {
|
|
return result
|
|
}
|
|
} catch (e) {
|
|
console.warn('Error during permission check', e)
|
|
return false
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
nuxtApp.provide('hasPermission', hasPermission)
|
|
},
|
|
})
|