2021-10-29 02:50:25 +00:00
|
|
|
import {
|
|
|
|
Alert,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
2022-06-27 08:46:49 +00:00
|
|
|
Form,
|
|
|
|
FormGroup,
|
2021-10-29 02:50:25 +00:00
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
2022-06-27 08:46:49 +00:00
|
|
|
Switch,
|
2021-10-29 02:50:25 +00:00
|
|
|
Text,
|
|
|
|
TextContent,
|
|
|
|
} from "@patternfly/react-core";
|
2022-11-18 16:13:39 +00:00
|
|
|
import { saveAs } from "file-saver";
|
2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2021-10-29 02:50:25 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-05-03 13:51:02 +00:00
|
|
|
|
|
|
|
import { adminClient } from "../admin-client";
|
2021-10-29 02:50:25 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
|
|
|
import { prettyPrintJSON } from "../util";
|
|
|
|
|
2022-06-27 08:46:49 +00:00
|
|
|
import "./partial-export.css";
|
|
|
|
|
2021-10-29 02:50:25 +00:00
|
|
|
export type PartialExportDialogProps = {
|
|
|
|
isOpen: boolean;
|
|
|
|
onClose: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const PartialExportDialog = ({
|
|
|
|
isOpen,
|
|
|
|
onClose,
|
|
|
|
}: PartialExportDialogProps) => {
|
2022-06-27 08:46:49 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
2021-10-29 02:50:25 +00:00
|
|
|
const { realm } = useRealm();
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
|
|
|
|
const [exportGroupsAndRoles, setExportGroupsAndRoles] = useState(false);
|
|
|
|
const [exportClients, setExportClients] = useState(false);
|
|
|
|
const [isExporting, setIsExporting] = useState(false);
|
|
|
|
|
|
|
|
const showWarning = exportGroupsAndRoles || exportClients;
|
|
|
|
|
|
|
|
async function exportRealm() {
|
|
|
|
setIsExporting(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const realmExport = await adminClient.realms.export({
|
|
|
|
realm,
|
|
|
|
exportClients,
|
|
|
|
exportGroupsAndRoles,
|
|
|
|
});
|
|
|
|
|
2022-11-18 16:13:39 +00:00
|
|
|
saveAs(
|
2021-10-29 02:50:25 +00:00
|
|
|
new Blob([prettyPrintJSON(realmExport)], {
|
|
|
|
type: "application/json",
|
|
|
|
}),
|
2023-07-11 14:03:21 +00:00
|
|
|
"realm-export.json",
|
2021-10-29 02:50:25 +00:00
|
|
|
);
|
|
|
|
|
2022-06-27 08:46:49 +00:00
|
|
|
addAlert(t("exportSuccess"), AlertVariant.success);
|
2021-10-29 02:50:25 +00:00
|
|
|
onClose();
|
|
|
|
} catch (error) {
|
2022-06-27 08:46:49 +00:00
|
|
|
addError("exportFail", error);
|
2021-10-29 02:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setIsExporting(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.small}
|
2022-06-27 08:46:49 +00:00
|
|
|
title={t("partialExport")}
|
2021-10-29 02:50:25 +00:00
|
|
|
isOpen={isOpen}
|
|
|
|
onClose={onClose}
|
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
key="export"
|
|
|
|
data-testid="export-button"
|
|
|
|
isDisabled={isExporting}
|
|
|
|
onClick={exportRealm}
|
|
|
|
>
|
|
|
|
{t("common:export")}
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
key="cancel"
|
|
|
|
data-testid="cancel-button"
|
|
|
|
variant={ButtonVariant.link}
|
|
|
|
onClick={onClose}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
2022-06-27 08:46:49 +00:00
|
|
|
<TextContent>
|
|
|
|
<Text>{t("partialExportHeaderText")}</Text>
|
|
|
|
</TextContent>
|
|
|
|
<Form
|
|
|
|
isHorizontal
|
|
|
|
className="keycloak__realm-settings__partial-import_form"
|
|
|
|
>
|
|
|
|
<FormGroup
|
|
|
|
label={t("includeGroupsAndRoles")}
|
|
|
|
fieldId="include-groups-and-roles-check"
|
|
|
|
hasNoPaddingTop
|
|
|
|
>
|
|
|
|
<Switch
|
2021-10-29 02:50:25 +00:00
|
|
|
id="include-groups-and-roles-check"
|
2022-06-27 08:46:49 +00:00
|
|
|
data-testid="include-groups-and-roles-check"
|
2021-10-29 02:50:25 +00:00
|
|
|
isChecked={exportGroupsAndRoles}
|
|
|
|
onChange={setExportGroupsAndRoles}
|
2022-06-27 08:46:49 +00:00
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
2022-08-30 13:07:51 +00:00
|
|
|
aria-label={t("includeGroupsAndRoles")}
|
2021-10-29 02:50:25 +00:00
|
|
|
/>
|
2022-06-27 08:46:49 +00:00
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("includeClients")}
|
|
|
|
fieldId="include-clients-check"
|
|
|
|
hasNoPaddingTop
|
|
|
|
>
|
|
|
|
<Switch
|
2021-10-29 02:50:25 +00:00
|
|
|
id="include-clients-check"
|
2022-06-27 08:46:49 +00:00
|
|
|
data-testid="include-clients-check"
|
2021-10-29 02:50:25 +00:00
|
|
|
onChange={setExportClients}
|
2022-06-27 08:46:49 +00:00
|
|
|
isChecked={exportClients}
|
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
2022-08-30 13:07:51 +00:00
|
|
|
aria-label={t("includeClients")}
|
2021-10-29 02:50:25 +00:00
|
|
|
/>
|
2022-06-27 08:46:49 +00:00
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
|
|
|
|
{showWarning && (
|
|
|
|
<Alert
|
|
|
|
data-testid="warning-message"
|
|
|
|
variant="warning"
|
|
|
|
title={t("exportWarningTitle")}
|
|
|
|
isInline
|
|
|
|
>
|
|
|
|
{t("exportWarningDescription")}
|
|
|
|
</Alert>
|
|
|
|
)}
|
2021-10-29 02:50:25 +00:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|