From fbf67a819f2ef678409a6136bbef2ea2ad922fa7 Mon Sep 17 00:00:00 2001 From: Todd Martin <106564991+trmartin4@users.noreply.github.com> Date: Wed, 12 Jul 2023 10:44:55 -0400 Subject: [PATCH] [PM-2846][PM-2860] Properly pass region from global to account state (#5764) * Properly pass region from global to account state * Fixed comment. * Updated logic to not set environment if region with predefined URLs is selected. * Added logic to clear environment URLs in EnvironmentService. * Fixed comment --- .../platform/services/environment.service.ts | 22 +++++++++---------- .../src/platform/services/state.service.ts | 18 ++++++++++++--- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/libs/common/src/platform/services/environment.service.ts b/libs/common/src/platform/services/environment.service.ts index 188c633a03..7b48c9d6eb 100644 --- a/libs/common/src/platform/services/environment.service.ts +++ b/libs/common/src/platform/services/environment.service.ts @@ -286,19 +286,19 @@ export class EnvironmentService implements EnvironmentServiceAbstraction { async setRegion(region: Region) { this.selectedRegion = region; await this.stateService.setRegion(region); - switch (region) { - case Region.EU: + if (region === Region.SelfHosted) { + // If user saves a self-hosted region with empty fields, default to US + if (this.isEmpty()) { + await this.setRegion(Region.US); + } + } else { + // If we are setting the region to EU or US, clear the self-hosted URLs + this.stateService.setEnvironmentUrls(new EnvironmentUrls()); + if (region === Region.EU) { this.setUrlsInternal(this.euUrls); - break; - case Region.US: + } else if (region === Region.US) { this.setUrlsInternal(this.usUrls); - break; - case Region.SelfHosted: - // if user saves with empty fields, default to US - if (this.isEmpty()) { - await this.setRegion(Region.US); - } - break; + } } } diff --git a/libs/common/src/platform/services/state.service.ts b/libs/common/src/platform/services/state.service.ts index dce5d76440..57b2a47da8 100644 --- a/libs/common/src/platform/services/state.service.ts +++ b/libs/common/src/platform/services/state.service.ts @@ -179,7 +179,7 @@ export class StateService< } async addAccount(account: TAccount) { - account = await this.setAccountEnvironmentUrls(account); + account = await this.setAccountEnvironment(account); await this.updateState(async (state) => { state.authenticatedAccounts.push(account.profile.userId); await this.storageService.save(keys.authenticatedAccounts, state.authenticatedAccounts); @@ -2606,8 +2606,9 @@ export class StateService< await this.defaultOnDiskLocalOptions() ) ); - // EnvironmentUrls are set before authenticating and should override whatever is stored from any previous session + // EnvironmentUrls and region are set before authenticating and should override whatever is stored from any previous session const environmentUrls = account.settings.environmentUrls; + const region = account.settings.region; if (storedAccount?.settings != null) { account.settings = storedAccount.settings; } else if (await this.storageService.has(keys.tempAccountSettings)) { @@ -2615,6 +2616,8 @@ export class StateService< await this.storageService.remove(keys.tempAccountSettings); } account.settings.environmentUrls = environmentUrls; + account.settings.region = region; + if ( account.settings.vaultTimeoutAction === VaultTimeoutAction.LogOut && account.settings.vaultTimeout != null @@ -2642,6 +2645,7 @@ export class StateService< ); if (storedAccount?.settings != null) { storedAccount.settings.environmentUrls = account.settings.environmentUrls; + storedAccount.settings.region = account.settings.region; account.settings = storedAccount.settings; } await this.storageService.save( @@ -2664,6 +2668,7 @@ export class StateService< ); if (storedAccount?.settings != null) { storedAccount.settings.environmentUrls = account.settings.environmentUrls; + storedAccount.settings.region = account.settings.region; account.settings = storedAccount.settings; } await this.storageService.save( @@ -2812,7 +2817,9 @@ export class StateService< return Object.assign(this.createAccount(), persistentAccountInformation); } - protected async setAccountEnvironmentUrls(account: TAccount): Promise { + // The environment urls and region are selected before login and are transferred here to an authenticated account + protected async setAccountEnvironment(account: TAccount): Promise { + account.settings.region = await this.getGlobalRegion(); account.settings.environmentUrls = await this.getGlobalEnvironmentUrls(); return account; } @@ -2822,6 +2829,11 @@ export class StateService< return (await this.getGlobals(options)).environmentUrls ?? new EnvironmentUrls(); } + protected async getGlobalRegion(options?: StorageOptions): Promise { + options = this.reconcileOptions(options, await this.defaultOnDiskOptions()); + return (await this.getGlobals(options)).region ?? null; + } + protected async clearDecryptedDataForActiveUser(): Promise { await this.updateState(async (state) => { const userId = state?.activeUserId;