[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
This commit is contained in:
Todd Martin 2023-07-12 10:44:55 -04:00 committed by GitHub
parent a37b8db250
commit fbf67a819f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 14 deletions

View File

@ -286,19 +286,19 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
async setRegion(region: Region) { async setRegion(region: Region) {
this.selectedRegion = region; this.selectedRegion = region;
await this.stateService.setRegion(region); await this.stateService.setRegion(region);
switch (region) { if (region === Region.SelfHosted) {
case Region.EU: // 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); this.setUrlsInternal(this.euUrls);
break; } else if (region === Region.US) {
case Region.US:
this.setUrlsInternal(this.usUrls); 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;
} }
} }

View File

@ -179,7 +179,7 @@ export class StateService<
} }
async addAccount(account: TAccount) { async addAccount(account: TAccount) {
account = await this.setAccountEnvironmentUrls(account); account = await this.setAccountEnvironment(account);
await this.updateState(async (state) => { await this.updateState(async (state) => {
state.authenticatedAccounts.push(account.profile.userId); state.authenticatedAccounts.push(account.profile.userId);
await this.storageService.save(keys.authenticatedAccounts, state.authenticatedAccounts); await this.storageService.save(keys.authenticatedAccounts, state.authenticatedAccounts);
@ -2606,8 +2606,9 @@ export class StateService<
await this.defaultOnDiskLocalOptions() 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 environmentUrls = account.settings.environmentUrls;
const region = account.settings.region;
if (storedAccount?.settings != null) { if (storedAccount?.settings != null) {
account.settings = storedAccount.settings; account.settings = storedAccount.settings;
} else if (await this.storageService.has(keys.tempAccountSettings)) { } else if (await this.storageService.has(keys.tempAccountSettings)) {
@ -2615,6 +2616,8 @@ export class StateService<
await this.storageService.remove(keys.tempAccountSettings); await this.storageService.remove(keys.tempAccountSettings);
} }
account.settings.environmentUrls = environmentUrls; account.settings.environmentUrls = environmentUrls;
account.settings.region = region;
if ( if (
account.settings.vaultTimeoutAction === VaultTimeoutAction.LogOut && account.settings.vaultTimeoutAction === VaultTimeoutAction.LogOut &&
account.settings.vaultTimeout != null account.settings.vaultTimeout != null
@ -2642,6 +2645,7 @@ export class StateService<
); );
if (storedAccount?.settings != null) { if (storedAccount?.settings != null) {
storedAccount.settings.environmentUrls = account.settings.environmentUrls; storedAccount.settings.environmentUrls = account.settings.environmentUrls;
storedAccount.settings.region = account.settings.region;
account.settings = storedAccount.settings; account.settings = storedAccount.settings;
} }
await this.storageService.save( await this.storageService.save(
@ -2664,6 +2668,7 @@ export class StateService<
); );
if (storedAccount?.settings != null) { if (storedAccount?.settings != null) {
storedAccount.settings.environmentUrls = account.settings.environmentUrls; storedAccount.settings.environmentUrls = account.settings.environmentUrls;
storedAccount.settings.region = account.settings.region;
account.settings = storedAccount.settings; account.settings = storedAccount.settings;
} }
await this.storageService.save( await this.storageService.save(
@ -2812,7 +2817,9 @@ export class StateService<
return Object.assign(this.createAccount(), persistentAccountInformation); return Object.assign(this.createAccount(), persistentAccountInformation);
} }
protected async setAccountEnvironmentUrls(account: TAccount): Promise<TAccount> { // The environment urls and region are selected before login and are transferred here to an authenticated account
protected async setAccountEnvironment(account: TAccount): Promise<TAccount> {
account.settings.region = await this.getGlobalRegion();
account.settings.environmentUrls = await this.getGlobalEnvironmentUrls(); account.settings.environmentUrls = await this.getGlobalEnvironmentUrls();
return account; return account;
} }
@ -2822,6 +2829,11 @@ export class StateService<
return (await this.getGlobals(options)).environmentUrls ?? new EnvironmentUrls(); return (await this.getGlobals(options)).environmentUrls ?? new EnvironmentUrls();
} }
protected async getGlobalRegion(options?: StorageOptions): Promise<string> {
options = this.reconcileOptions(options, await this.defaultOnDiskOptions());
return (await this.getGlobals(options)).region ?? null;
}
protected async clearDecryptedDataForActiveUser(): Promise<void> { protected async clearDecryptedDataForActiveUser(): Promise<void> {
await this.updateState(async (state) => { await this.updateState(async (state) => {
const userId = state?.activeUserId; const userId = state?.activeUserId;