Skip to content

Commit

Permalink
refactor: announce password policy service before app init
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Dec 1, 2023
1 parent f2db986 commit 885a037
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ interface GeneratePasswordRules {
}

export class PasswordPolicyService {
private readonly capability: PasswordPolicyCapability
private readonly language: Language
private capability: PasswordPolicyCapability
private policy: PasswordPolicy
private generatePasswordRules: GeneratePasswordRules

constructor({ store, language }: { store: Store<unknown>; language: Language }) {
this.capability = get(store, 'getters.capabilities.password_policy', {})
constructor({ language }: { language: Language }) {
this.language = language
}

public initialize(store: Store<unknown>) {
this.capability = get(store, 'getters.capabilities.password_policy', {})
this.buildGeneratePasswordRules()
this.buildPolicy()
}
Expand Down
8 changes: 5 additions & 3 deletions packages/web-pkg/tests/unit/services/passwordPolicy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ describe('PasswordPolicyService', () => {
]
]
])('capability "%s"', (capability: PasswordPolicyCapability, expected: Array<string>) => {
const { passwordPolicyService } = getWrapper(capability)
const { passwordPolicyService, store } = getWrapper(capability)
passwordPolicyService.initialize(store)
expect(Object.keys((passwordPolicyService.getPolicy() as any).rules)).toEqual(expected)
})
})
Expand Down Expand Up @@ -100,7 +101,8 @@ describe('PasswordPolicyService', () => {
passwords: Array<string>,
expected: Array<boolean>
) => {
const { passwordPolicyService } = getWrapper(capability)
const { passwordPolicyService, store } = getWrapper(capability)
passwordPolicyService.initialize(store)
const policy = passwordPolicyService.getPolicy()
for (let i = 0; i < passwords.length; i++) {
expect((policy as any).check(passwords[i])).toEqual(expected[i])
Expand All @@ -119,8 +121,8 @@ const getWrapper = (capability: PasswordPolicyCapability) => {
})
const store = createStore(storeOptions)
return {
store,
passwordPolicyService: new PasswordPolicyService({
store,
language: { current: 'en' } as Language
})
}
Expand Down
10 changes: 2 additions & 8 deletions packages/web-runtime/src/container/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,9 @@ export const announceAuthService = ({
/**
* @param vue
*/
export const announcePasswordPolicyService = ({
app,
store
}: {
app: App
store: Store<any>
}): void => {
export const announcePasswordPolicyService = ({ app }: { app: App }): void => {
const language = app.config.globalProperties.$language
const passwordPolicyService = new PasswordPolicyService({ store, language })
const passwordPolicyService = new PasswordPolicyService({ language })
app.config.globalProperties.passwordPolicyService = passwordPolicyService
app.provide('$passwordPolicyService', passwordPolicyService)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/web-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const bootstrapApp = async (configurationPath: string): Promise<void> =>
app.provide('$archiverService', app.config.globalProperties.$archiverService)
announceLoadingService({ app })
announcePreviewService({ app, store, configurationManager })
announcePasswordPolicyService({ app })
await announceClient(runtimeConfiguration)

app.config.globalProperties.$wormhole = createWormhole()
Expand Down Expand Up @@ -165,9 +166,9 @@ export const bootstrapApp = async (configurationPath: string): Promise<void> =>
return
}

announcePasswordPolicyService({ app, store })

const clientService = app.config.globalProperties.$clientService
const passwordPolicyService = app.config.globalProperties.passwordPolicyService
passwordPolicyService.initialize(store)

// Register SSE event listeners
if (store.getters.capabilities?.core?.['support-sse']) {
Expand Down

0 comments on commit 885a037

Please sign in to comment.