2021-07-21 11:32:27 +02:00
|
|
|
import { Component, OnInit, ViewChild, ViewContainerRef } from "@angular/core";
|
|
|
|
import { ActivatedRoute } from "@angular/router";
|
2021-10-15 00:59:43 +02:00
|
|
|
import { first } from "rxjs/operators";
|
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ModalService } from "@bitwarden/angular/services/modal.service";
|
|
|
|
import { ValidationService } from "@bitwarden/angular/services/validation.service";
|
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.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";
|
2022-08-24 18:33:05 +02:00
|
|
|
import { OrganizationApiServiceAbstraction } from "@bitwarden/common/abstractions/organization/organization-api.service.abstraction";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
|
|
import { ProviderService } from "@bitwarden/common/abstractions/provider.service";
|
|
|
|
import { SearchService } from "@bitwarden/common/abstractions/search.service";
|
|
|
|
import { PlanType } from "@bitwarden/common/enums/planType";
|
|
|
|
import { ProviderUserType } from "@bitwarden/common/enums/providerUserType";
|
|
|
|
import { Organization } from "@bitwarden/common/models/domain/organization";
|
|
|
|
import { ProviderOrganizationOrganizationDetailsResponse } from "@bitwarden/common/models/response/provider/providerOrganizationResponse";
|
2021-07-21 11:32:27 +02:00
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
import { WebProviderService } from "../services/webProvider.service";
|
2021-08-05 03:53:15 +02:00
|
|
|
|
2021-07-21 11:32:27 +02:00
|
|
|
import { AddOrganizationComponent } from "./add-organization.component";
|
|
|
|
|
2021-08-09 19:24:12 +02:00
|
|
|
const DisallowedPlanTypes = [
|
|
|
|
PlanType.Free,
|
|
|
|
PlanType.FamiliesAnnually2019,
|
|
|
|
PlanType.FamiliesAnnually,
|
|
|
|
];
|
|
|
|
|
2021-07-21 11:32:27 +02:00
|
|
|
@Component({
|
|
|
|
templateUrl: "clients.component.html",
|
|
|
|
})
|
|
|
|
export class ClientsComponent implements OnInit {
|
|
|
|
@ViewChild("add", { read: ViewContainerRef, static: true }) addModalRef: ViewContainerRef;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2022-08-24 18:33:05 +02:00
|
|
|
providerId: string;
|
2021-07-21 11:32:27 +02:00
|
|
|
searchText: string;
|
2021-08-09 19:24:12 +02:00
|
|
|
addableOrganizations: Organization[];
|
2021-07-21 11:32:27 +02:00
|
|
|
loading = true;
|
2021-08-05 03:53:15 +02:00
|
|
|
manageOrganizations = false;
|
2021-07-21 11:32:27 +02:00
|
|
|
showAddExisting = false;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-07-21 11:32:27 +02:00
|
|
|
clients: ProviderOrganizationOrganizationDetailsResponse[];
|
|
|
|
pagedClients: ProviderOrganizationOrganizationDetailsResponse[];
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-07-21 11:32:27 +02:00
|
|
|
protected didScroll = false;
|
|
|
|
protected pageSize = 100;
|
2022-08-24 18:33:05 +02:00
|
|
|
protected actionPromise: Promise<unknown>;
|
2021-07-21 11:32:27 +02:00
|
|
|
private pagedClientsCount = 0;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private providerService: ProviderService,
|
|
|
|
private apiService: ApiService,
|
|
|
|
private searchService: SearchService,
|
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private validationService: ValidationService,
|
|
|
|
private webProviderService: WebProviderService,
|
|
|
|
private logService: LogService,
|
|
|
|
private modalService: ModalService,
|
2022-08-24 18:33:05 +02:00
|
|
|
private organizationService: OrganizationService,
|
|
|
|
private organizationApiService: OrganizationApiServiceAbstraction
|
2021-12-17 15:57:11 +01:00
|
|
|
) {}
|
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
async ngOnInit() {
|
|
|
|
this.route.parent.params.subscribe(async (params) => {
|
|
|
|
this.providerId = params.providerId;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-07-21 11:32:27 +02:00
|
|
|
await this.load();
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-07-21 11:32:27 +02:00
|
|
|
this.route.queryParams.pipe(first()).subscribe(async (qParams) => {
|
|
|
|
this.searchText = qParams.search;
|
2021-12-17 15:57:11 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-21 11:32:27 +02:00
|
|
|
async load() {
|
|
|
|
const response = await this.apiService.getProviderClients(this.providerId);
|
|
|
|
this.clients = response.data != null && response.data.length > 0 ? response.data : [];
|
2021-12-14 17:10:26 +01:00
|
|
|
this.manageOrganizations =
|
2021-07-21 11:32:27 +02:00
|
|
|
(await this.providerService.get(this.providerId)).type === ProviderUserType.ProviderAdmin;
|
|
|
|
const candidateOrgs = (await this.organizationService.getAll()).filter(
|
2021-12-14 17:10:26 +01:00
|
|
|
(o) => o.isOwner && o.providerId == null
|
2021-07-21 11:32:27 +02:00
|
|
|
);
|
2021-10-15 00:59:43 +02:00
|
|
|
const allowedOrgsIds = await Promise.all(
|
2022-08-24 18:33:05 +02:00
|
|
|
candidateOrgs.map((o) => this.organizationApiService.get(o.id))
|
2021-08-09 19:24:12 +02:00
|
|
|
).then((orgs) =>
|
2021-10-15 00:59:43 +02:00
|
|
|
orgs.filter((o) => !DisallowedPlanTypes.includes(o.planType)).map((o) => o.id)
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
2021-08-09 19:24:12 +02:00
|
|
|
this.addableOrganizations = candidateOrgs.filter((o) => allowedOrgsIds.includes(o.id));
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-10-15 00:59:43 +02:00
|
|
|
this.showAddExisting = this.addableOrganizations.length !== 0;
|
2021-07-21 11:32:27 +02:00
|
|
|
this.loading = false;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 00:59:43 +02:00
|
|
|
isPaging() {
|
2021-07-21 19:53:21 +02:00
|
|
|
const searching = this.isSearching();
|
|
|
|
if (searching && this.didScroll) {
|
2021-07-21 11:32:27 +02:00
|
|
|
this.resetPaging();
|
|
|
|
}
|
|
|
|
return !searching && this.clients && this.clients.length > this.pageSize;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-07-21 11:32:27 +02:00
|
|
|
|
|
|
|
isSearching() {
|
|
|
|
return this.searchService.isSearchable(this.searchText);
|
|
|
|
}
|
|
|
|
|
|
|
|
async resetPaging() {
|
|
|
|
this.pagedClients = [];
|
|
|
|
this.loadMore();
|
|
|
|
}
|
|
|
|
|
|
|
|
loadMore() {
|
|
|
|
if (!this.clients || this.clients.length <= this.pageSize) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const pagedLength = this.pagedClients.length;
|
|
|
|
let pagedSize = this.pageSize;
|
|
|
|
if (pagedLength === 0 && this.pagedClientsCount > this.pageSize) {
|
|
|
|
pagedSize = this.pagedClientsCount;
|
|
|
|
}
|
|
|
|
if (this.clients.length > pagedLength) {
|
|
|
|
this.pagedClients = this.pagedClients.concat(
|
|
|
|
this.clients.slice(pagedLength, pagedLength + pagedSize)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.pagedClientsCount = this.pagedClients.length;
|
|
|
|
this.didScroll = this.pagedClients.length > this.pageSize;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-08-27 14:50:58 +02:00
|
|
|
async addExistingOrganization() {
|
2021-08-31 16:42:43 +02:00
|
|
|
const [modal] = await this.modalService.openViewRef(
|
|
|
|
AddOrganizationComponent,
|
|
|
|
this.addModalRef,
|
|
|
|
(comp) => {
|
2021-08-27 14:50:58 +02:00
|
|
|
comp.providerId = this.providerId;
|
|
|
|
comp.organizations = this.addableOrganizations;
|
|
|
|
comp.onAddedOrganization.subscribe(async () => {
|
|
|
|
try {
|
|
|
|
await this.load();
|
|
|
|
modal.close();
|
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(`Handled exception: ${e}`);
|
|
|
|
}
|
2021-07-21 11:32:27 +02:00
|
|
|
});
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-21 11:32:27 +02:00
|
|
|
async remove(organization: ProviderOrganizationOrganizationDetailsResponse) {
|
|
|
|
const confirmed = await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t("detachOrganizationConfirmation"),
|
|
|
|
organization.organizationName,
|
|
|
|
this.i18nService.t("yes"),
|
|
|
|
this.i18nService.t("no"),
|
2021-12-17 15:57:11 +01:00
|
|
|
"warning"
|
|
|
|
);
|
|
|
|
|
2021-07-21 11:32:27 +02:00
|
|
|
if (!confirmed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
this.actionPromise = this.webProviderService.detachOrganizastion(
|
|
|
|
this.providerId,
|
|
|
|
organization.id
|
|
|
|
);
|
2021-07-21 11:32:27 +02:00
|
|
|
try {
|
|
|
|
await this.actionPromise;
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"success",
|
|
|
|
null,
|
|
|
|
this.i18nService.t("detachedOrganization", organization.organizationName)
|
|
|
|
);
|
2021-07-21 11:32:27 +02:00
|
|
|
await this.load();
|
|
|
|
} catch (e) {
|
|
|
|
this.validationService.showError(e);
|
|
|
|
}
|
|
|
|
this.actionPromise = null;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-07-21 11:32:27 +02:00
|
|
|
}
|