mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 03:03:43 +00:00
[PM-25931] Integrations - can save only if owner (#16570)
This commit is contained in:
@@ -6,6 +6,7 @@ import { BehaviorSubject, of } from "rxjs";
|
||||
import { SYSTEM_THEME_OBSERVABLE } from "@bitwarden/angular/services/injection-tokens";
|
||||
import { OrganizationIntegrationServiceType } from "@bitwarden/bit-common/dirt/organization-integrations/models/organization-integration-service-type";
|
||||
import { HecOrganizationIntegrationService } from "@bitwarden/bit-common/dirt/organization-integrations/services/hec-organization-integration-service";
|
||||
import { ErrorResponse } from "@bitwarden/common/models/response/error.response";
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { ThemeType } from "@bitwarden/common/platform/enums";
|
||||
import { ThemeStateService } from "@bitwarden/common/platform/theming/theme-state.service";
|
||||
@@ -314,7 +315,7 @@ describe("IntegrationCardComponent", () => {
|
||||
|
||||
jest.spyOn(component, "isUpdateAvailable", "get").mockReturnValue(false);
|
||||
|
||||
mockIntegrationService.saveHec.mockResolvedValue(undefined);
|
||||
mockIntegrationService.saveHec.mockResolvedValue({ mustBeOwner: false, success: true });
|
||||
|
||||
await component.setupConnection();
|
||||
|
||||
@@ -340,7 +341,7 @@ describe("IntegrationCardComponent", () => {
|
||||
}),
|
||||
});
|
||||
|
||||
mockIntegrationService.deleteHec.mockResolvedValue(undefined);
|
||||
mockIntegrationService.deleteHec.mockResolvedValue({ mustBeOwner: false, success: true });
|
||||
|
||||
await component.setupConnection();
|
||||
|
||||
@@ -368,7 +369,7 @@ describe("IntegrationCardComponent", () => {
|
||||
}),
|
||||
});
|
||||
|
||||
mockIntegrationService.deleteHec.mockResolvedValue(undefined);
|
||||
mockIntegrationService.deleteHec.mockResolvedValue({ mustBeOwner: false, success: true });
|
||||
|
||||
await component.setupConnection();
|
||||
|
||||
@@ -407,6 +408,52 @@ describe("IntegrationCardComponent", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should show mustBeOwner toast on error while inserting data", async () => {
|
||||
(openHecConnectDialog as jest.Mock).mockReturnValue({
|
||||
closed: of({
|
||||
success: HecConnectDialogResultStatus.Edited,
|
||||
url: "test-url",
|
||||
bearerToken: "token",
|
||||
index: "index",
|
||||
}),
|
||||
});
|
||||
|
||||
jest.spyOn(component, "isUpdateAvailable", "get").mockReturnValue(true);
|
||||
mockIntegrationService.updateHec.mockRejectedValue(new ErrorResponse("Not Found", 404));
|
||||
|
||||
await component.setupConnection();
|
||||
|
||||
expect(mockIntegrationService.updateHec).toHaveBeenCalled();
|
||||
expect(toastService.showToast).toHaveBeenCalledWith({
|
||||
variant: "error",
|
||||
title: "",
|
||||
message: mockI18nService.t("mustBeOrgOwnerToPerformAction"),
|
||||
});
|
||||
});
|
||||
|
||||
it("should show mustBeOwner toast on error while updating data", async () => {
|
||||
(openHecConnectDialog as jest.Mock).mockReturnValue({
|
||||
closed: of({
|
||||
success: HecConnectDialogResultStatus.Edited,
|
||||
url: "test-url",
|
||||
bearerToken: "token",
|
||||
index: "index",
|
||||
}),
|
||||
});
|
||||
|
||||
jest.spyOn(component, "isUpdateAvailable", "get").mockReturnValue(true);
|
||||
mockIntegrationService.updateHec.mockRejectedValue(new ErrorResponse("Not Found", 404));
|
||||
|
||||
await component.setupConnection();
|
||||
|
||||
expect(mockIntegrationService.updateHec).toHaveBeenCalled();
|
||||
expect(toastService.showToast).toHaveBeenCalledWith({
|
||||
variant: "error",
|
||||
title: "",
|
||||
message: mockI18nService.t("mustBeOrgOwnerToPerformAction"),
|
||||
});
|
||||
});
|
||||
|
||||
it("should show toast on error while deleting", async () => {
|
||||
(openHecConnectDialog as jest.Mock).mockReturnValue({
|
||||
closed: of({
|
||||
@@ -429,5 +476,28 @@ describe("IntegrationCardComponent", () => {
|
||||
message: mockI18nService.t("failedToDeleteIntegration"),
|
||||
});
|
||||
});
|
||||
|
||||
it("should show mustbeOwner toast on 404 while deleting", async () => {
|
||||
(openHecConnectDialog as jest.Mock).mockReturnValue({
|
||||
closed: of({
|
||||
success: HecConnectDialogResultStatus.Delete,
|
||||
url: "test-url",
|
||||
bearerToken: "token",
|
||||
index: "index",
|
||||
}),
|
||||
});
|
||||
|
||||
jest.spyOn(component, "isUpdateAvailable", "get").mockReturnValue(true);
|
||||
mockIntegrationService.deleteHec.mockRejectedValue(new ErrorResponse("Not Found", 404));
|
||||
|
||||
await component.setupConnection();
|
||||
|
||||
expect(mockIntegrationService.deleteHec).toHaveBeenCalled();
|
||||
expect(toastService.showToast).toHaveBeenCalledWith({
|
||||
variant: "error",
|
||||
title: "",
|
||||
message: mockI18nService.t("mustBeOrgOwnerToPerformAction"),
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -171,6 +171,7 @@ export class IntegrationCardComponent implements AfterViewInit, OnDestroy {
|
||||
}
|
||||
|
||||
async saveHec(result: HecConnectDialogResult) {
|
||||
let saveResponse = { mustBeOwner: false, success: false };
|
||||
if (this.isUpdateAvailable) {
|
||||
// retrieve org integration and configuration ids
|
||||
const orgIntegrationId = this.integrationSettings.organizationIntegration?.id;
|
||||
@@ -182,7 +183,7 @@ export class IntegrationCardComponent implements AfterViewInit, OnDestroy {
|
||||
}
|
||||
|
||||
// update existing integration and configuration
|
||||
await this.hecOrganizationIntegrationService.updateHec(
|
||||
saveResponse = await this.hecOrganizationIntegrationService.updateHec(
|
||||
this.organizationId,
|
||||
orgIntegrationId,
|
||||
orgIntegrationConfigurationId,
|
||||
@@ -193,7 +194,7 @@ export class IntegrationCardComponent implements AfterViewInit, OnDestroy {
|
||||
);
|
||||
} else {
|
||||
// create new integration and configuration
|
||||
await this.hecOrganizationIntegrationService.saveHec(
|
||||
saveResponse = await this.hecOrganizationIntegrationService.saveHec(
|
||||
this.organizationId,
|
||||
this.integrationSettings.name as OrganizationIntegrationServiceType,
|
||||
result.url,
|
||||
@@ -201,6 +202,12 @@ export class IntegrationCardComponent implements AfterViewInit, OnDestroy {
|
||||
result.index,
|
||||
);
|
||||
}
|
||||
|
||||
if (saveResponse.mustBeOwner) {
|
||||
this.showMustBeOwnerToast();
|
||||
return;
|
||||
}
|
||||
|
||||
this.toastService.showToast({
|
||||
variant: "success",
|
||||
title: "",
|
||||
@@ -217,16 +224,29 @@ export class IntegrationCardComponent implements AfterViewInit, OnDestroy {
|
||||
throw Error("Organization Integration ID or Configuration ID is missing");
|
||||
}
|
||||
|
||||
await this.hecOrganizationIntegrationService.deleteHec(
|
||||
const response = await this.hecOrganizationIntegrationService.deleteHec(
|
||||
this.organizationId,
|
||||
orgIntegrationId,
|
||||
orgIntegrationConfigurationId,
|
||||
);
|
||||
|
||||
if (response.mustBeOwner) {
|
||||
this.showMustBeOwnerToast();
|
||||
return;
|
||||
}
|
||||
|
||||
this.toastService.showToast({
|
||||
variant: "success",
|
||||
title: "",
|
||||
message: this.i18nService.t("success"),
|
||||
});
|
||||
}
|
||||
|
||||
private showMustBeOwnerToast() {
|
||||
this.toastService.showToast({
|
||||
variant: "error",
|
||||
title: "",
|
||||
message: this.i18nService.t("mustBeOrgOwnerToPerformAction"),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,16 +5,14 @@ import { firstValueFrom, Observable, Subject, switchMap, takeUntil, takeWhile }
|
||||
import { Integration } from "@bitwarden/bit-common/dirt/organization-integrations/models/integration";
|
||||
import { OrganizationIntegrationServiceType } from "@bitwarden/bit-common/dirt/organization-integrations/models/organization-integration-service-type";
|
||||
import { HecOrganizationIntegrationService } from "@bitwarden/bit-common/dirt/organization-integrations/services/hec-organization-integration-service";
|
||||
import {
|
||||
getOrganizationById,
|
||||
OrganizationService,
|
||||
} from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
|
||||
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
|
||||
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
|
||||
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
||||
import { getUserId } from "@bitwarden/common/auth/services/account.service";
|
||||
import { IntegrationType } from "@bitwarden/common/enums";
|
||||
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
|
||||
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
|
||||
import { getById } from "@bitwarden/common/platform/misc";
|
||||
import { HeaderModule } from "@bitwarden/web-vault/app/layouts/header/header.module";
|
||||
import { SharedModule } from "@bitwarden/web-vault/app/shared";
|
||||
|
||||
@@ -218,7 +216,7 @@ export class AdminConsoleIntegrationsComponent implements OnInit, OnDestroy {
|
||||
this.organization$ = this.route.params.pipe(
|
||||
switchMap((params) =>
|
||||
this.organizationService.organizations$(userId).pipe(
|
||||
getOrganizationById(params.organizationId),
|
||||
getById(params.organizationId),
|
||||
// Filter out undefined values
|
||||
takeWhile((org: Organization | undefined) => !!org),
|
||||
),
|
||||
@@ -229,6 +227,24 @@ export class AdminConsoleIntegrationsComponent implements OnInit, OnDestroy {
|
||||
this.organization$.pipe(takeUntil(this.destroy$)).subscribe((org) => {
|
||||
this.hecOrganizationIntegrationService.setOrganizationIntegrations(org.id);
|
||||
});
|
||||
|
||||
// For all existing event based configurations loop through and assign the
|
||||
// organizationIntegration for the correct services.
|
||||
this.hecOrganizationIntegrationService.integrations$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe((integrations) => {
|
||||
// reset all integrations to null first - in case one was deleted
|
||||
this.integrationsList.forEach((i) => {
|
||||
i.organizationIntegration = null;
|
||||
});
|
||||
|
||||
integrations.map((integration) => {
|
||||
const item = this.integrationsList.find((i) => i.name === integration.serviceType);
|
||||
if (item) {
|
||||
item.organizationIntegration = integration;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
constructor(
|
||||
@@ -258,24 +274,6 @@ export class AdminConsoleIntegrationsComponent implements OnInit, OnDestroy {
|
||||
|
||||
this.integrationsList.push(crowdstrikeIntegration);
|
||||
}
|
||||
|
||||
// For all existing event based configurations loop through and assign the
|
||||
// organizationIntegration for the correct services.
|
||||
this.hecOrganizationIntegrationService.integrations$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe((integrations) => {
|
||||
// reset all integrations to null first - in case one was deleted
|
||||
this.integrationsList.forEach((i) => {
|
||||
i.organizationIntegration = null;
|
||||
});
|
||||
|
||||
integrations.map((integration) => {
|
||||
const item = this.integrationsList.find((i) => i.name === integration.serviceType);
|
||||
if (item) {
|
||||
item.organizationIntegration = integration;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
ngOnDestroy(): void {
|
||||
this.destroy$.next();
|
||||
|
||||
Reference in New Issue
Block a user