From d922976f8eebc89d3cf6befd3ceb282753447114 Mon Sep 17 00:00:00 2001 From: Will Martin Date: Thu, 29 Dec 2022 14:37:30 -0500 Subject: [PATCH] [EC-879] fix: close dialogs on vault lock (#4349) --- libs/components/src/dialog/dialog.service.ts | 63 ++++++++++++++++++-- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/libs/components/src/dialog/dialog.service.ts b/libs/components/src/dialog/dialog.service.ts index 1569df7224..f23b3fd797 100644 --- a/libs/components/src/dialog/dialog.service.ts +++ b/libs/components/src/dialog/dialog.service.ts @@ -1,9 +1,30 @@ -import { Dialog, DialogConfig, DialogRef } from "@angular/cdk/dialog"; -import { ComponentType } from "@angular/cdk/overlay"; -import { Injectable, TemplateRef } from "@angular/core"; +import { + DEFAULT_DIALOG_CONFIG, + Dialog, + DialogConfig, + DialogRef, + DIALOG_SCROLL_STRATEGY, +} from "@angular/cdk/dialog"; +import { ComponentType, Overlay, OverlayContainer } from "@angular/cdk/overlay"; +import { + Inject, + Injectable, + Injector, + OnDestroy, + Optional, + SkipSelf, + TemplateRef, +} from "@angular/core"; +import { NavigationEnd, Router } from "@angular/router"; +import { filter, Subject, switchMap, takeUntil } from "rxjs"; + +import { AuthService } from "@bitwarden/common/abstractions/auth.service"; +import { AuthenticationStatus } from "@bitwarden/common/enums/authenticationStatus"; @Injectable() -export class DialogService extends Dialog { +export class DialogService extends Dialog implements OnDestroy { + private _destroy$ = new Subject(); + override open( componentOrTemplateRef: ComponentType | TemplateRef, config?: DialogConfig> @@ -15,4 +36,38 @@ export class DialogService extends Dialog { return super.open(componentOrTemplateRef, config); } + + constructor( + /** Parent class constructor */ + _overlay: Overlay, + _injector: Injector, + @Optional() @Inject(DEFAULT_DIALOG_CONFIG) _defaultOptions: DialogConfig, + @Optional() @SkipSelf() _parentDialog: Dialog, + _overlayContainer: OverlayContainer, + @Inject(DIALOG_SCROLL_STRATEGY) scrollStrategy: any, + + /** Not in parent class */ + @Optional() router: Router, + @Optional() authService: AuthService + ) { + super(_overlay, _injector, _defaultOptions, _parentDialog, _overlayContainer, scrollStrategy); + + /** Close all open dialogs if the vault locks */ + if (router && authService) { + router.events + .pipe( + filter((event) => event instanceof NavigationEnd), + switchMap(() => authService.getAuthStatus()), + filter((v) => v !== AuthenticationStatus.Unlocked), + takeUntil(this._destroy$) + ) + .subscribe(() => this.closeAll()); + } + } + + override ngOnDestroy(): void { + this._destroy$.next(); + this._destroy$.complete(); + super.ngOnDestroy(); + } }