2022-03-04 15:03:48 +01:00
|
|
|
import { Component, EventEmitter, OnInit, Output } from "@angular/core";
|
2018-07-16 18:42:49 +02:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { CipherService } from "@bitwarden/common/abstractions/cipher.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
|
|
import { OrganizationService } from "@bitwarden/common/abstractions/organization.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
|
|
import { UserVerificationService } from "@bitwarden/common/abstractions/userVerification.service";
|
|
|
|
import { CipherType } from "@bitwarden/common/enums/cipherType";
|
|
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
|
|
|
import { CipherView } from "@bitwarden/common/models/view/cipherView";
|
|
|
|
import { Verification } from "@bitwarden/common/types/verification";
|
2018-07-16 18:42:49 +02:00
|
|
|
|
2022-03-04 15:03:48 +01:00
|
|
|
class CountBasedLocalizationKey {
|
|
|
|
singular: string;
|
|
|
|
plural: string;
|
|
|
|
|
|
|
|
getKey(count: number) {
|
|
|
|
return count == 1 ? this.singular : this.plural;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(singular: string, plural: string) {
|
|
|
|
this.singular = singular;
|
|
|
|
this.plural = plural;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class OrganizationContentSummaryItem {
|
|
|
|
count: number;
|
|
|
|
get localizationKey(): string {
|
|
|
|
return this.localizationKeyOptions.getKey(this.count);
|
|
|
|
}
|
|
|
|
private localizationKeyOptions: CountBasedLocalizationKey;
|
|
|
|
constructor(count: number, localizationKeyOptions: CountBasedLocalizationKey) {
|
|
|
|
this.count = count;
|
|
|
|
this.localizationKeyOptions = localizationKeyOptions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class OrganizationContentSummary {
|
|
|
|
totalItemCount = 0;
|
|
|
|
itemCountByType: OrganizationContentSummaryItem[] = [];
|
|
|
|
}
|
|
|
|
|
2018-07-16 18:42:49 +02:00
|
|
|
@Component({
|
|
|
|
selector: "app-delete-organization",
|
|
|
|
templateUrl: "delete-organization.component.html",
|
|
|
|
})
|
2022-03-04 15:03:48 +01:00
|
|
|
export class DeleteOrganizationComponent implements OnInit {
|
2018-07-16 18:42:49 +02:00
|
|
|
organizationId: string;
|
2022-03-04 15:03:48 +01:00
|
|
|
loaded: boolean;
|
|
|
|
deleteOrganizationRequestType: "InvalidFamiliesForEnterprise" | "RegularDelete" = "RegularDelete";
|
|
|
|
organizationName: string;
|
|
|
|
organizationContentSummary: OrganizationContentSummary = new OrganizationContentSummary();
|
2021-11-22 14:41:40 +01:00
|
|
|
@Output() onSuccess: EventEmitter<any> = new EventEmitter();
|
2018-07-16 18:42:49 +02:00
|
|
|
|
2021-11-09 19:24:26 +01:00
|
|
|
masterPassword: Verification;
|
2018-07-16 18:42:49 +02:00
|
|
|
formPromise: Promise<any>;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private apiService: ApiService,
|
|
|
|
private i18nService: I18nService,
|
2021-12-07 20:41:45 +01:00
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private userVerificationService: UserVerificationService,
|
2022-03-04 15:03:48 +01:00
|
|
|
private logService: LogService,
|
|
|
|
private cipherService: CipherService,
|
|
|
|
private organizationService: OrganizationService
|
2021-11-22 14:41:40 +01:00
|
|
|
) {}
|
2018-07-16 18:42:49 +02:00
|
|
|
|
2022-03-04 15:03:48 +01:00
|
|
|
async ngOnInit(): Promise<void> {
|
|
|
|
await this.load();
|
|
|
|
}
|
|
|
|
|
2018-07-16 18:42:49 +02:00
|
|
|
async submit() {
|
|
|
|
try {
|
2021-11-17 00:37:36 +01:00
|
|
|
this.formPromise = this.userVerificationService
|
|
|
|
.buildRequest(this.masterPassword)
|
|
|
|
.then((request) => this.apiService.deleteOrganization(this.organizationId, request));
|
2018-07-16 18:42:49 +02:00
|
|
|
await this.formPromise;
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"success",
|
|
|
|
this.i18nService.t("organizationDeleted"),
|
2018-07-16 18:42:49 +02:00
|
|
|
this.i18nService.t("organizationDeletedDesc")
|
|
|
|
);
|
2021-11-22 14:41:40 +01:00
|
|
|
this.onSuccess.emit();
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2018-07-16 18:42:49 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2022-03-04 15:03:48 +01:00
|
|
|
|
|
|
|
private async load() {
|
|
|
|
this.organizationName = (await this.organizationService.get(this.organizationId)).name;
|
|
|
|
this.organizationContentSummary = await this.buildOrganizationContentSummary();
|
|
|
|
this.loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async buildOrganizationContentSummary(): Promise<OrganizationContentSummary> {
|
|
|
|
const organizationContentSummary = new OrganizationContentSummary();
|
|
|
|
const organizationItems = (
|
|
|
|
await this.cipherService.getAllFromApiForOrganization(this.organizationId)
|
|
|
|
).filter((item) => item.deletedDate == null);
|
|
|
|
|
|
|
|
if (organizationItems.length < 1) {
|
|
|
|
return organizationContentSummary;
|
|
|
|
}
|
|
|
|
|
|
|
|
organizationContentSummary.totalItemCount = organizationItems.length;
|
|
|
|
for (const cipherType of Utils.iterateEnum(CipherType)) {
|
|
|
|
const count = this.getOrganizationItemCountByType(organizationItems, cipherType);
|
|
|
|
if (count > 0) {
|
|
|
|
organizationContentSummary.itemCountByType.push(
|
|
|
|
new OrganizationContentSummaryItem(
|
|
|
|
count,
|
|
|
|
this.getOrganizationItemLocalizationKeysByType(CipherType[cipherType])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return organizationContentSummary;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getOrganizationItemCountByType(items: CipherView[], type: CipherType) {
|
|
|
|
return items.filter((item) => item.type == type).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getOrganizationItemLocalizationKeysByType(type: string): CountBasedLocalizationKey {
|
|
|
|
return new CountBasedLocalizationKey(`type${type}`, `type${type}Plural`);
|
|
|
|
}
|
2018-07-16 18:42:49 +02:00
|
|
|
}
|