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 type { Application } from '@hcengineering/workbench'
import { createQuery } from '@hcengineering/presentation' import { createQuery } from '@hcengineering/presentation'
import workbench from '@hcengineering/workbench' 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 { Loading, IconCheck, Label, Icon } from '@hcengineering/ui'
// import Drag from './icons/Drag.svelte' // import Drag from './icons/Drag.svelte'
@ -69,7 +69,7 @@
const me = getCurrentAccount() const me = getCurrentAccount()
const filteredApps = apps.filter( 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> </script>

View File

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

View File

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

View File

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

View File

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