From 0aef37fc305299d4c1558fbc80ed270b00928765 Mon Sep 17 00:00:00 2001 From: Alec Rippberger Date: Tue, 25 Mar 2025 16:32:53 -0500 Subject: [PATCH] Populate empty form with managed environment values --- ...self-hosted-env-config-dialog.component.ts | 66 +++++++++++-------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/libs/auth/src/angular/self-hosted-env-config-dialog/self-hosted-env-config-dialog.component.ts b/libs/auth/src/angular/self-hosted-env-config-dialog/self-hosted-env-config-dialog.component.ts index 8d80a38d7ad..753e63fcc86 100644 --- a/libs/auth/src/angular/self-hosted-env-config-dialog/self-hosted-env-config-dialog.component.ts +++ b/libs/auth/src/angular/self-hosted-env-config-dialog/self-hosted-env-config-dialog.component.ts @@ -1,5 +1,3 @@ -// FIXME: Update this file to be type safe and remove this and next line -// @ts-strict-ignore import { DialogRef } from "@angular/cdk/dialog"; import { CommonModule } from "@angular/common"; import { Component, OnDestroy, OnInit } from "@angular/core"; @@ -12,12 +10,13 @@ import { ValidationErrors, ValidatorFn, } from "@angular/forms"; -import { Subject, firstValueFrom, take, filter, takeUntil } from "rxjs"; +import { Subject, firstValueFrom, filter, takeUntil } from "rxjs"; import { JslibModule } from "@bitwarden/angular/jslib.module"; import { EnvironmentService, Region, + Urls, } from "@bitwarden/common/platform/abstractions/environment.service"; import { AsyncActionsModule, @@ -81,19 +80,17 @@ export class SelfHostedEnvConfigDialogComponent implements OnInit, OnDestroy { disableClose: false, }); - const dialogResult = await firstValueFrom(dialogRef.closed); - - return dialogResult; + return (await firstValueFrom(dialogRef.closed)) ?? false; } formGroup = this.formBuilder.group( { - baseUrl: [null], - webVaultUrl: [null], - apiUrl: [null], - identityUrl: [null], - iconsUrl: [null], - notificationsUrl: [null], + baseUrl: [""], + webVaultUrl: [""], + apiUrl: [""], + identityUrl: [""], + iconsUrl: [""], + notificationsUrl: [""], }, { validators: selfHostedEnvSettingsFormValidator() }, ); @@ -133,32 +130,43 @@ export class SelfHostedEnvConfigDialogComponent implements OnInit, OnDestroy { private environmentService: EnvironmentService, ) {} - ngOnInit() { - /** - * Populate the form with the current self-hosted environment settings. - */ + async ngOnInit() { this.environmentService.environment$ .pipe( - take(1), filter((env) => { const region = env.getRegion(); return region === Region.SelfHosted; }), takeUntil(this.destroy$), ) - .subscribe({ - next: (env) => { - const urls = env.getUrls(); - this.formGroup.patchValue({ - baseUrl: urls.base || "", - webVaultUrl: urls.webVault || "", - apiUrl: urls.api || "", - identityUrl: urls.identity || "", - iconsUrl: urls.icons || "", - notificationsUrl: urls.notifications || "", - }); - }, + .subscribe((env) => { + const urls = env.getUrls(); + this.formGroup.patchValue({ + baseUrl: urls.base || "", + webVaultUrl: urls.webVault || "", + apiUrl: urls.api || "", + identityUrl: urls.identity || "", + iconsUrl: urls.icons || "", + notificationsUrl: urls.notifications || "", + }); }); + + // Check if all form fields are empty + const allFieldsEmpty = + !this.baseUrl.value && + !this.webVaultUrl.value && + !this.apiUrl.value && + !this.identityUrl.value && + !this.iconsUrl.value && + !this.notificationsUrl.value; + + // If all fields are empty, set the environment to the managed environment as a fallback + if (allFieldsEmpty) { + const managedEnvironment = await this.environmentService.getManagedEnvironment(); + if (managedEnvironment) { + await this.environmentService.setEnvironment(Region.SelfHosted, managedEnvironment); + } + } } submit = async () => {