Refactor apps and widgets availability check

Signed-off-by: Anton Alexeyev <alexeyev.anton@gmail.com>
This commit is contained in:
Anton Alexeyev 2025-05-23 11:37:54 +07:00
parent 7fbc6b88a2
commit 682726b931
5 changed files with 18 additions and 13 deletions

View File

@ -17,7 +17,7 @@
import type { Application } from '@hcengineering/workbench'
import { createQuery } from '@hcengineering/presentation'
import workbench from '@hcengineering/workbench'
import { hideApplication, isAppAllowed, showApplication } from '../utils'
import { hideApplication, isAllowedToRole, showApplication } from '../utils'
import { Loading, IconCheck, Label, Icon } from '@hcengineering/ui'
// import Drag from './icons/Drag.svelte'
@ -69,7 +69,7 @@
const me = getCurrentAccount()
const filteredApps = apps.filter(
(it) => !hiddenAppsIds.includes(it._id) && isAppAllowed(it, me) && it.position !== 'top'
(it) => !hiddenAppsIds.includes(it._id) && isAllowedToRole(it.accessLevel, me) && it.position !== 'top'
)
</script>

View File

@ -22,7 +22,7 @@
import workbench from '@hcengineering/workbench'
import { inboxId } from '@hcengineering/inbox'
import { isAppAllowed } from '../utils'
import { isAllowedToRole } from '../utils'
import AppItem from './AppItem.svelte'
export let active: Ref<Application> | undefined
@ -56,7 +56,11 @@
$: topApps = apps.filter((it) => it.position === 'top')
$: midApps = apps.filter(
(it) => !hiddenAppsIds.includes(it._id) && isAppAllowed(it, me) && it.position !== 'top' && it.position !== 'bottom'
(it) =>
!hiddenAppsIds.includes(it._id) &&
isAllowedToRole(it.accessLevel, me) &&
it.position !== 'top' &&
it.position !== 'bottom'
)
$: bottomApps = apps.filter((it) => it.position === 'bottom')
</script>

View File

@ -101,7 +101,7 @@
import { getContext, onDestroy, onMount, tick } from 'svelte'
import { subscribeMobile } from '../mobile'
import workbench from '../plugin'
import { buildNavModel, isAppAllowed, logOut, workspacesStore } from '../utils'
import { buildNavModel, isAllowedToRole, logOut, workspacesStore } from '../utils'
import AccountPopup from './AccountPopup.svelte'
import AppItem from './AppItem.svelte'
import AppSwitcher from './AppSwitcher.svelte'
@ -158,7 +158,7 @@
const apps: Application[] = client
.getModel()
.findAllSync<Application>(workbench.class.Application, { hidden: false, _id: { $nin: excludedApps } })
.filter((it) => isAppAllowed(it, account))
.filter((it) => isAllowedToRole(it.accessLevel, account))
console.log(apps)

View File

@ -15,13 +15,13 @@
<script lang="ts">
import { createQuery, getClient } from '@hcengineering/presentation'
import { panelstore } from '@hcengineering/ui'
import { WidgetPreference } from '@hcengineering/workbench'
import { Widget, WidgetPreference } from '@hcengineering/workbench'
import workbench from '../../plugin'
import { sidebarStore, SidebarVariant } from '../../sidebar'
import SidebarExpanded from './SidebarExpanded.svelte'
import SidebarMini from './SidebarMini.svelte'
import { isAppAllowed } from '../../utils'
import { isAllowedToRole } from '../../utils'
import { getCurrentAccount } from '@hcengineering/core'
const account = getCurrentAccount()
@ -29,8 +29,8 @@
const widgets = client
.getModel()
.findAllSync(workbench.class.Widget, {})
.filter((it) => isAppAllowed(it, account))
.findAllSync<Widget>(workbench.class.Widget, {})
.filter((it) => isAllowedToRole(it.accessLevel, account))
const preferencesQuery = createQuery()
let preferences: WidgetPreference[] = []

View File

@ -17,6 +17,7 @@
import { getClient as getAccountClient } from '@hcengineering/account-client'
import type {
Account,
AccountRole,
Class,
Client,
Doc,
@ -141,9 +142,9 @@ export async function doNavigate (
}
}
export function isAppAllowed (app: Application, acc: Account): boolean {
if (app.accessLevel === undefined) return true
return hasAccountRole(acc, app.accessLevel)
export function isAllowedToRole (role: AccountRole | undefined, acc: Account): boolean {
if (role === undefined) return true
return hasAccountRole(acc, role)
}
export async function hideApplication (app: Application): Promise<void> {