Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[full-ci] Pass globalProperties to routes and navItems of apps #8437

Merged
merged 4 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions packages/web-app-admin-settings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ const appInfo = {
isFileEditor: false
}

// FIXME: a better way to access this is needed
const permissionManager = () => (window as any).__$permissionManager

const routes = [
const routes = ({ $permissionManager }) => [
{
path: '/',
redirect: () => {
if (permissionManager().hasSystemManagement()) {
if ($permissionManager.hasSystemManagement()) {
return { name: 'admin-settings-general' }
}
return { name: 'admin-settings-spaces' }
Expand Down Expand Up @@ -66,15 +63,15 @@ const routes = [
}
]

const navItems = [
const navItems = ({ $permissionManager }) => [
{
name: $gettext('General'),
icon: 'settings-4',
route: {
path: `/${appInfo.id}/general?`
},
enabled: () => {
return permissionManager().hasSystemManagement()
return $permissionManager.hasSystemManagement()
}
},
{
Expand All @@ -84,7 +81,7 @@ const navItems = [
path: `/${appInfo.id}/users?`
},
enabled: () => {
return permissionManager().hasUserManagement()
return $permissionManager.hasUserManagement()
}
},
{
Expand All @@ -94,7 +91,7 @@ const navItems = [
path: `/${appInfo.id}/groups?`
},
enabled: () => {
return permissionManager().hasUserManagement()
return $permissionManager.hasUserManagement()
}
},
{
Expand All @@ -104,7 +101,7 @@ const navItems = [
path: `/${appInfo.id}/spaces?`
},
enabled: () => {
return permissionManager().hasSpaceManagement()
return $permissionManager.hasSpaceManagement()
}
}
]
Expand Down
70 changes: 42 additions & 28 deletions packages/web-app-admin-settings/tests/unit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,68 @@ describe('admin settings index', () => {
describe('navItems', () => {
describe('general', () => {
it.each([true, false])('should be enabled according to the permissions', (enabled) => {
;(window as any).__$permissionManager = {
hasSystemManagement: () => enabled
}
expect(index.navItems.find((n) => n.name === 'General').enabled()).toBe(enabled)
const $permissionManager = { hasSystemManagement: () => enabled }
expect(
index
.navItems({ $permissionManager })
.find((n) => n.name === 'General')
.enabled()
).toBe(enabled)
})
})
describe('user management', () => {
it.each([true, false])('should be enabled according to the permissions', (enabled) => {
;(window as any).__$permissionManager = {
hasUserManagement: () => enabled
}
expect(index.navItems.find((n) => n.name === 'Users').enabled()).toBe(enabled)
const $permissionManager = { hasUserManagement: () => enabled }
expect(
index
.navItems({ $permissionManager })
.find((n) => n.name === 'Users')
.enabled()
).toBe(enabled)
})
})
describe('group management', () => {
it.each([true, false])('should be enabled according to the permissions', (enabled) => {
;(window as any).__$permissionManager = {
hasUserManagement: () => enabled
}
expect(index.navItems.find((n) => n.name === 'Groups').enabled()).toBe(enabled)
const $permissionManager = { hasUserManagement: () => enabled }
expect(
index
.navItems({ $permissionManager })
.find((n) => n.name === 'Groups')
.enabled()
).toBe(enabled)
})
})
describe('space management', () => {
it.each([true, false])('should be enabled according to the permissions', (enabled) => {
;(window as any).__$permissionManager = {
hasSpaceManagement: () => enabled
}
expect(index.navItems.find((n) => n.name === 'Spaces').enabled()).toBe(enabled)
const $permissionManager = { hasSpaceManagement: () => enabled }
expect(
index
.navItems({ $permissionManager })
.find((n) => n.name === 'Spaces')
.enabled()
).toBe(enabled)
})
})
})
describe('routes', () => {
describe('default-route "/"', () => {
it('should redirect to general if permission given', () => {
;(window as any).__$permissionManager = {
hasSystemManagement: () => true
}
expect(index.routes.find((n) => n.path === '/').redirect().name).toEqual(
'admin-settings-general'
)
const $permissionManager = { hasSystemManagement: () => true }
expect(
index
.routes({ $permissionManager })
.find((n) => n.path === '/')
.redirect().name
).toEqual('admin-settings-general')
})
it('should redirect to space management if no system management permission given', () => {
;(window as any).__$permissionManager = {
hasSystemManagement: () => false
}
expect(index.routes.find((n) => n.path === '/').redirect().name).toEqual(
'admin-settings-spaces'
)
const $permissionManager = { hasSystemManagement: () => false }
expect(
index
.routes({ $permissionManager })
.find((n) => n.path === '/')
.redirect().name
).toEqual('admin-settings-spaces')
})
})
})
Expand Down
16 changes: 12 additions & 4 deletions packages/web-runtime/src/container/application/classic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ import { RuntimeError } from '../error'
*/
class ClassicApplication extends NextApplication {
private readonly applicationScript: ClassicApplicationScript
private readonly app: App

constructor(runtimeApi: RuntimeApi, applicationScript: ClassicApplicationScript) {
constructor(runtimeApi: RuntimeApi, applicationScript: ClassicApplicationScript, app: App) {
super(runtimeApi)
this.applicationScript = applicationScript
this.app = app
}

initialize(): Promise<void> {
const { routes, navItems, translations, quickActions, store } = this.applicationScript
const { globalProperties } = this.app.config
const _routes = typeof routes === 'function' ? routes(globalProperties) : routes
const _navItems = typeof navItems === 'function' ? navItems(globalProperties) : navItems

routes && this.runtimeApi.announceRoutes(routes)
navItems && this.runtimeApi.announceNavigationItems(navItems)
routes && this.runtimeApi.announceRoutes(_routes)
navItems && this.runtimeApi.announceNavigationItems(_navItems)
translations && this.runtimeApi.announceTranslations(translations)
quickActions && this.runtimeApi.announceQuickActions(quickActions)
store && this.runtimeApi.announceStore(store)
Expand Down Expand Up @@ -61,19 +66,22 @@ class ClassicApplication extends NextApplication {

/**
*
* @param app
* @param applicationPath
* @param store
* @param router
* @param translations
* @param supportedLanguages
*/
export const convertClassicApplication = async ({
app,
applicationScript,
store,
router,
translations,
supportedLanguages
}: {
app: App
applicationScript: ClassicApplicationScript
store: Store<unknown>
router: Router
Expand Down Expand Up @@ -107,5 +115,5 @@ export const convertClassicApplication = async ({

await store.dispatch('registerApp', applicationScript.appInfo)

return new ClassicApplication(runtimeApi, applicationScript)
return new ClassicApplication(runtimeApi, applicationScript, app)
}
4 changes: 4 additions & 0 deletions packages/web-runtime/src/container/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as vueGettext from 'vue3-gettext' // eslint-disable-line

import { urlJoin } from 'web-client/src/utils'
import { ConfigurationManager } from 'web-pkg'
import { App } from 'vue'

export { NextApplication } from './next'

Expand Down Expand Up @@ -52,13 +53,15 @@ const loadScriptRequireJS = <T>(moduleUri: string) => {
* @param args
*/
export const buildApplication = async ({
app,
applicationPath,
store,
router,
translations,
supportedLanguages,
configurationManager
}: {
app: App
applicationPath: string
store: Store<unknown>
router: Router
Expand Down Expand Up @@ -111,6 +114,7 @@ export const buildApplication = async ({
throw new RuntimeError('next applications not implemented yet, stay tuned')
} else {
application = await convertClassicApplication({
app,
applicationScript,
store,
router,
Expand Down
4 changes: 4 additions & 0 deletions packages/web-runtime/src/container/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,23 @@ export const announceClient = async (runtimeConfiguration: RuntimeConfiguration)
* - bulk build all applications
* - bulk register all applications, no other application is guaranteed to be registered here, don't request one
*
* @param app
* @param runtimeConfiguration
* @param store
* @param router
* @param translations
* @param supportedLanguages
*/
export const initializeApplications = async ({
app,
runtimeConfiguration,
configurationManager,
store,
router,
translations,
supportedLanguages
}: {
app: App
runtimeConfiguration: RuntimeConfiguration
configurationManager: ConfigurationManager
store: Store<unknown>
Expand All @@ -129,6 +132,7 @@ export const initializeApplications = async ({
const applicationResults = await Promise.allSettled(
applicationPaths.map((applicationPath) =>
buildApplication({
app,
applicationPath,
store,
supportedLanguages,
Expand Down
4 changes: 2 additions & 2 deletions packages/web-runtime/src/container/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export interface ApplicationTranslations {
export interface ClassicApplicationScript {
appInfo?: ApplicationInformation
store?: Store<any>
routes?: RouteRecordRaw[]
navItems?: ApplicationNavigationItem[]
routes?: ((...args) => RouteRecordRaw[]) | RouteRecordRaw[]
navItems?: ((...args) => ApplicationNavigationItem[]) | ApplicationNavigationItem[]
quickActions?: ApplicationQuickActions
translations?: ApplicationTranslations
initialize?: () => void
Expand Down
1 change: 1 addition & 0 deletions packages/web-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const bootstrapApp = async (configurationPath: string): Promise<void> =>
announcePermissionManager({ app, store })

const applicationsPromise = await initializeApplications({
app,
runtimeConfiguration,
configurationManager,
store,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('initialize applications', () => {
jest.mocked(buildApplication).mockImplementation(buildApplicationMock)

const applications = await initializeApplications({
app: createApp(defineComponent({})),
configurationManager: mockDeep<ConfigurationManager>(),
runtimeConfiguration: {
apps: ['internalFishy', 'internalValid'],
Expand Down