[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) {
this.selectedRegion = region;
await this.stateService.setRegion(region);
switch (region) {
case Region.EU:
this.setUrlsInternal(this.euUrls);
break;
case Region.US:
this.setUrlsInternal(this.usUrls);
break;
case Region.SelfHosted:
// if user saves with empty fields, default to US
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);
}
break;
} 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);
} else if (region === Region.US) {
this.setUrlsInternal(this.usUrls);
}
}
}

View File

@ -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<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();
return account;
}
@ -2822,6 +2829,11 @@ export class StateService<
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> {
await this.updateState(async (state) => {
const userId = state?.activeUserId;