1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-29 06:33:40 +00:00
Files
browser/libs/key-management-ui/src/key-connector/remove-password.component.ts
Maciej Zieniuk 239556b55f [PM-18017] Show key connector domain in remove password page (#14695)
* Passed in userId on RemovePasswordComponent.

* Added userId on other references to KeyConnectorService methods

* remove password component refactor, test coverage, enabled strict

* explicit user id provided to key connector service

* redirect to / instead when user not logged in or not managing organization

* key connector service explicit user id

* key connector service no longer requires account service

* key connector service missing null type

* cli convert to key connector unit tests

* remove unnecessary SyncService

* error toast not showing on ErrorResponse

* bad import due to merge conflict

* bad import due to merge conflict

* missing loading in remove password component for browser extension

* error handling in remove password component

* organization observable race condition in key-connector

* usesKeyConnector always returns boolean

* unit test coverage

* key connector reactive

* reactive key connector service

* introducing convertAccountRequired$

* cli build fix

* moving message sending side effect to sync

* key connector service unit tests

* fix unit tests

* move key connector components to KM team ownership

* new unit tests in wrong place

* key connector domain shown in remove password component

* type safety improvements

* convert to key connector command localization

* key connector domain in convert to key connector command

* convert to key connector command unit tests with prompt assert

* organization name placement change in the remove password component

* unit test update

* key connector url required to be provided when migrating user

* unit tests in wrong place after KM code ownership move

* infinite page reload

* failing unit tests

* failing unit tests

---------

Co-authored-by: Todd Martin <tmartin@bitwarden.com>
2025-05-19 08:58:51 -04:00

128 lines
3.8 KiB
TypeScript

import { Directive, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { firstValueFrom } from "rxjs";
import { OrganizationApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/organization/organization-api.service.abstraction";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { KeyConnectorService } from "@bitwarden/common/key-management/key-connector/abstractions/key-connector.service";
import { ErrorResponse } from "@bitwarden/common/models/response/error.response";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { UserId } from "@bitwarden/common/types/guid";
import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.service.abstraction";
import { DialogService, ToastService } from "@bitwarden/components";
@Directive()
export class RemovePasswordComponent implements OnInit {
continuing = false;
leaving = false;
loading = true;
organization!: Organization;
private activeUserId!: UserId;
constructor(
private logService: LogService,
private router: Router,
private accountService: AccountService,
private syncService: SyncService,
private i18nService: I18nService,
private keyConnectorService: KeyConnectorService,
private organizationApiService: OrganizationApiServiceAbstraction,
private dialogService: DialogService,
private toastService: ToastService,
) {}
async ngOnInit() {
const activeAccount = await firstValueFrom(this.accountService.activeAccount$);
if (activeAccount == null) {
this.logService.info(
"[Key Connector remove password] No active account found, redirecting to login.",
);
await this.router.navigate(["/"]);
return;
}
this.activeUserId = activeAccount.id;
await this.syncService.fullSync(false);
this.organization = await this.keyConnectorService.getManagingOrganization(this.activeUserId);
if (this.organization == null) {
this.logService.info(
"[Key Connector remove password] No organization found, redirecting to login.",
);
await this.router.navigate(["/"]);
return;
}
this.loading = false;
}
get action() {
return this.continuing || this.leaving;
}
convert = async () => {
this.continuing = true;
try {
await this.keyConnectorService.migrateUser(
this.organization.keyConnectorUrl,
this.activeUserId,
);
this.toastService.showToast({
variant: "success",
message: this.i18nService.t("removedMasterPassword"),
});
await this.router.navigate(["/"]);
} catch (e) {
this.continuing = false;
this.handleActionError(e);
}
};
leave = async () => {
const confirmed = await this.dialogService.openSimpleDialog({
title: this.organization.name,
content: { key: "leaveOrganizationConfirmation" },
type: "warning",
});
if (!confirmed) {
return false;
}
this.leaving = true;
try {
await this.organizationApiService.leave(this.organization.id);
this.toastService.showToast({
variant: "success",
message: this.i18nService.t("leftOrganization"),
});
await this.router.navigate(["/"]);
} catch (e) {
this.leaving = false;
this.handleActionError(e);
}
};
handleActionError(e: unknown) {
let message = "";
if (e instanceof ErrorResponse || e instanceof Error) {
message = e.message;
}
this.toastService.showToast({
variant: "error",
title: this.i18nService.t("errorOccurred"),
message: message,
});
}
}