2021-01-13 19:59:45 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { useHistory } from "react-router-dom";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2021-02-17 21:12:25 +00:00
|
|
|
import { useErrorHandler } from "react-error-boundary";
|
2021-01-13 19:59:45 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
ButtonVariant,
|
|
|
|
DropdownItem,
|
|
|
|
DropdownSeparator,
|
|
|
|
PageSection,
|
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import RealmRepresentation from "keycloak-admin/lib/defs/realmRepresentation";
|
2021-04-19 11:41:07 +00:00
|
|
|
import { toUpperCase } from "../util";
|
2021-01-13 19:59:45 +00:00
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { useAdminClient, asyncStateFetch } from "../context/auth/AdminClient";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-03-31 13:16:58 +00:00
|
|
|
import { KeycloakTabs } from "../components/keycloak-tabs/KeycloakTabs";
|
2021-04-19 11:41:07 +00:00
|
|
|
import { RealmSettingsLoginTab } from "./LoginTab";
|
|
|
|
import { RealmSettingsGeneralTab } from "./GeneralTab";
|
2021-04-14 14:17:41 +00:00
|
|
|
import { PartialImportDialog } from "./PartialImport";
|
2021-01-13 19:59:45 +00:00
|
|
|
|
|
|
|
type RealmSettingsHeaderProps = {
|
2021-02-08 20:50:03 +00:00
|
|
|
onChange: (value: boolean) => void;
|
2021-01-13 19:59:45 +00:00
|
|
|
value: boolean;
|
|
|
|
save: () => void;
|
|
|
|
realmName: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const RealmSettingsHeader = ({
|
|
|
|
save,
|
|
|
|
onChange,
|
|
|
|
value,
|
|
|
|
realmName,
|
|
|
|
}: RealmSettingsHeaderProps) => {
|
|
|
|
const { t } = useTranslation("realm-settings");
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const { addAlert } = useAlerts();
|
|
|
|
const history = useHistory();
|
|
|
|
const { setRealm } = useRealm();
|
2021-04-14 14:17:41 +00:00
|
|
|
const [partialImportOpen, setPartialImportOpen] = useState(false);
|
2021-01-13 19:59:45 +00:00
|
|
|
|
|
|
|
const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "realm-settings:disableConfirmTitle",
|
|
|
|
messageKey: "realm-settings:disableConfirm",
|
|
|
|
continueButtonLabel: "common:disable",
|
|
|
|
onConfirm: () => {
|
|
|
|
onChange(!value);
|
|
|
|
save();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "realm-settings:deleteConfirmTitle",
|
|
|
|
messageKey: "realm-settings:deleteConfirm",
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.realms.del({ realm: realmName });
|
|
|
|
addAlert(t("deletedSuccess"), AlertVariant.success);
|
|
|
|
setRealm("master");
|
2021-02-09 12:32:41 +00:00
|
|
|
history.push("/master/");
|
2021-01-13 19:59:45 +00:00
|
|
|
} catch (error) {
|
|
|
|
addAlert(t("deleteError", { error }), AlertVariant.danger);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DisableConfirm />
|
|
|
|
<DeleteConfirm />
|
2021-04-14 14:17:41 +00:00
|
|
|
<PartialImportDialog
|
|
|
|
open={partialImportOpen}
|
|
|
|
toggleDialog={() => setPartialImportOpen(!partialImportOpen)}
|
|
|
|
/>
|
2021-01-13 19:59:45 +00:00
|
|
|
<ViewHeader
|
|
|
|
titleKey={toUpperCase(realmName)}
|
2021-03-31 13:16:58 +00:00
|
|
|
divider={false}
|
2021-01-13 19:59:45 +00:00
|
|
|
dropdownItems={[
|
2021-04-14 14:17:41 +00:00
|
|
|
<DropdownItem
|
|
|
|
key="import"
|
|
|
|
data-testid="openPartialImportModal"
|
|
|
|
onClick={() => {
|
|
|
|
setPartialImportOpen(true);
|
|
|
|
}}
|
|
|
|
>
|
2021-01-13 19:59:45 +00:00
|
|
|
{t("partialImport")}
|
|
|
|
</DropdownItem>,
|
|
|
|
<DropdownItem key="export" onClick={() => {}}>
|
|
|
|
{t("partialExport")}
|
|
|
|
</DropdownItem>,
|
|
|
|
<DropdownSeparator key="separator" />,
|
|
|
|
<DropdownItem key="delete" onClick={toggleDeleteDialog}>
|
|
|
|
{t("common:delete")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
isEnabled={value}
|
|
|
|
onToggle={(value) => {
|
|
|
|
if (!value) {
|
|
|
|
toggleDisableDialog();
|
|
|
|
} else {
|
|
|
|
onChange(value);
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-09-10 18:04:03 +00:00
|
|
|
export const RealmSettingsSection = () => {
|
2021-01-13 19:59:45 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
|
|
|
const adminClient = useAdminClient();
|
2021-02-17 21:12:25 +00:00
|
|
|
const handleError = useErrorHandler();
|
2021-01-13 19:59:45 +00:00
|
|
|
const { realm: realmName } = useRealm();
|
|
|
|
const { addAlert } = useAlerts();
|
2021-04-19 11:41:07 +00:00
|
|
|
const { control, getValues, setValue } = useForm();
|
2021-01-13 19:59:45 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return asyncStateFetch(
|
|
|
|
() => adminClient.realms.findOne({ realm: realmName }),
|
|
|
|
(realm) => {
|
|
|
|
setupForm(realm);
|
2021-02-17 21:12:25 +00:00
|
|
|
},
|
|
|
|
handleError
|
2021-01-13 19:59:45 +00:00
|
|
|
);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const setupForm = (realm: RealmRepresentation) => {
|
|
|
|
Object.entries(realm).map((entry) => setValue(entry[0], entry[1]));
|
|
|
|
};
|
|
|
|
|
|
|
|
const save = async (realm: RealmRepresentation) => {
|
|
|
|
try {
|
|
|
|
await adminClient.realms.update({ realm: realmName }, realm);
|
|
|
|
addAlert(t("saveSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addAlert(t("saveError", { error }), AlertVariant.danger);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Controller
|
|
|
|
name="enabled"
|
|
|
|
control={control}
|
|
|
|
defaultValue={true}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<RealmSettingsHeader
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
realmName={realmName}
|
|
|
|
save={() => save(getValues())}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2021-03-31 13:16:58 +00:00
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
|
|
|
<KeycloakTabs isBox>
|
|
|
|
<Tab
|
|
|
|
eventKey="general"
|
2021-04-19 11:41:07 +00:00
|
|
|
title={<TabTitleText>{t("realm-settings:general")}</TabTitleText>}
|
|
|
|
data-testid="rs-general-tab"
|
2021-03-31 13:16:58 +00:00
|
|
|
>
|
2021-04-19 11:41:07 +00:00
|
|
|
<RealmSettingsGeneralTab />
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
eventKey="login"
|
|
|
|
title={<TabTitleText>{t("realm-settings:login")}</TabTitleText>}
|
|
|
|
data-testid="rs-login-tab"
|
|
|
|
>
|
|
|
|
<RealmSettingsLoginTab />
|
2021-01-13 19:59:45 +00:00
|
|
|
</Tab>
|
2021-03-31 13:16:58 +00:00
|
|
|
</KeycloakTabs>
|
2021-01-13 19:59:45 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
2020-09-09 09:07:17 +00:00
|
|
|
};
|