2021-05-18 08:34:16 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2021-01-13 19:59:45 +00:00
|
|
|
import { useHistory } from "react-router-dom";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-04-30 19:08:12 +00:00
|
|
|
import { Controller, FormProvider, useForm } from "react-hook-form";
|
2021-01-13 19:59:45 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
ButtonVariant,
|
|
|
|
DropdownItem,
|
|
|
|
DropdownSeparator,
|
|
|
|
PageSection,
|
|
|
|
Tab,
|
2021-05-14 18:58:08 +00:00
|
|
|
Tabs,
|
2021-01-13 19:59:45 +00:00
|
|
|
TabTitleText,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
2021-05-04 17:58:18 +00:00
|
|
|
import type 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";
|
2021-04-29 06:28:59 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
2021-01-13 19:59:45 +00:00
|
|
|
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-04-29 17:39:19 +00:00
|
|
|
import { RealmSettingsThemesTab } from "./ThemesTab";
|
2021-05-03 20:00:12 +00:00
|
|
|
import { RealmSettingsEmailTab } from "./EmailTab";
|
2021-05-14 18:58:08 +00:00
|
|
|
import { KeysListTab } from "./KeysListTab";
|
2021-05-03 17:27:56 +00:00
|
|
|
import { KeyMetadataRepresentation } from "keycloak-admin/lib/defs/keyMetadataRepresentation";
|
|
|
|
import ComponentRepresentation from "keycloak-admin/lib/defs/componentRepresentation";
|
|
|
|
import { KeysProviderTab } from "./KeysProvidersTab";
|
2021-05-03 21:07:22 +00:00
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
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();
|
2021-05-03 16:44:47 +00:00
|
|
|
const { refresh } = 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);
|
2021-02-09 12:32:41 +00:00
|
|
|
history.push("/master/");
|
2021-05-03 16:44:47 +00:00
|
|
|
refresh();
|
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();
|
|
|
|
const { realm: realmName } = useRealm();
|
|
|
|
const { addAlert } = useAlerts();
|
2021-04-30 19:08:12 +00:00
|
|
|
const form = useForm();
|
|
|
|
const { control, getValues, setValue } = form;
|
|
|
|
const [realm, setRealm] = useState<RealmRepresentation>();
|
2021-05-14 18:58:08 +00:00
|
|
|
const [activeTab2, setActiveTab2] = useState(0);
|
|
|
|
const [keys, setKeys] = useState<KeyMetadataRepresentation[]>([]);
|
|
|
|
const [realmComponents, setRealmComponents] = useState<
|
|
|
|
ComponentRepresentation[]
|
|
|
|
>([]);
|
2021-01-13 19:59:45 +00:00
|
|
|
|
2021-05-03 21:07:22 +00:00
|
|
|
const kpComponentTypes = useServerInfo().componentTypes![
|
|
|
|
"org.keycloak.keys.KeyProvider"
|
|
|
|
];
|
|
|
|
|
2021-04-29 06:28:59 +00:00
|
|
|
useFetch(
|
|
|
|
() => adminClient.realms.findOne({ realm: realmName }),
|
|
|
|
(realm) => {
|
|
|
|
setupForm(realm);
|
|
|
|
setRealm(realm);
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
2021-01-13 19:59:45 +00:00
|
|
|
|
2021-05-14 18:58:08 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const update = async () => {
|
|
|
|
const keysMetaData = await adminClient.realms.getKeys({
|
|
|
|
realm: realmName,
|
|
|
|
});
|
|
|
|
setKeys(keysMetaData.keys!);
|
|
|
|
const realmComponents = await adminClient.components.find({
|
|
|
|
type: "org.keycloak.keys.KeyProvider",
|
|
|
|
realm: realmName,
|
|
|
|
});
|
|
|
|
setRealmComponents(realmComponents);
|
|
|
|
};
|
|
|
|
setTimeout(update, 100);
|
|
|
|
}, []);
|
|
|
|
|
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);
|
2021-04-30 19:08:12 +00:00
|
|
|
setRealm(realm);
|
2021-01-13 19:59:45 +00:00
|
|
|
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">
|
2021-04-30 19:08:12 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<KeycloakTabs isBox>
|
|
|
|
<Tab
|
|
|
|
eventKey="general"
|
|
|
|
title={<TabTitleText>{t("realm-settings:general")}</TabTitleText>}
|
|
|
|
data-testid="rs-general-tab"
|
|
|
|
>
|
|
|
|
<RealmSettingsGeneralTab
|
|
|
|
save={save}
|
|
|
|
reset={() => setupForm(realm!)}
|
|
|
|
/>
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
eventKey="login"
|
|
|
|
title={<TabTitleText>{t("realm-settings:login")}</TabTitleText>}
|
|
|
|
data-testid="rs-login-tab"
|
|
|
|
>
|
|
|
|
<RealmSettingsLoginTab save={save} realm={realm!} />
|
|
|
|
</Tab>
|
2021-05-03 20:00:12 +00:00
|
|
|
<Tab
|
|
|
|
eventKey="email"
|
|
|
|
title={<TabTitleText>{t("realm-settings:email")}</TabTitleText>}
|
|
|
|
data-testid="rs-email-tab"
|
|
|
|
>
|
|
|
|
<RealmSettingsEmailTab
|
|
|
|
save={save}
|
|
|
|
reset={() => setupForm(realm!)}
|
|
|
|
/>
|
|
|
|
</Tab>
|
2021-04-30 19:08:12 +00:00
|
|
|
<Tab
|
|
|
|
eventKey="themes"
|
|
|
|
title={<TabTitleText>{t("realm-settings:themes")}</TabTitleText>}
|
|
|
|
data-testid="rs-themes-tab"
|
|
|
|
>
|
|
|
|
<RealmSettingsThemesTab
|
|
|
|
save={save}
|
|
|
|
reset={() => setupForm(realm!)}
|
|
|
|
/>
|
|
|
|
</Tab>
|
2021-05-14 18:58:08 +00:00
|
|
|
<Tab
|
|
|
|
eventKey="keys"
|
|
|
|
title={<TabTitleText>{t("realm-settings:keys")}</TabTitleText>}
|
|
|
|
data-testid="rs-keys-tab"
|
|
|
|
>
|
|
|
|
<Tabs
|
|
|
|
activeKey={activeTab2}
|
|
|
|
onSelect={(_, key) => setActiveTab2(key as number)}
|
|
|
|
>
|
|
|
|
<Tab
|
|
|
|
id="setup"
|
|
|
|
eventKey={0}
|
|
|
|
title={<TabTitleText>{t("keysList")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<KeysListTab keys={keys} realmComponents={realmComponents} />
|
|
|
|
</Tab>
|
2021-05-03 17:27:56 +00:00
|
|
|
<Tab
|
|
|
|
id="evaluate"
|
|
|
|
eventKey={1}
|
|
|
|
title={<TabTitleText>{t("providers")}</TabTitleText>}
|
|
|
|
>
|
2021-05-03 21:07:22 +00:00
|
|
|
<KeysProviderTab
|
|
|
|
components={realmComponents}
|
|
|
|
realmComponents={realmComponents}
|
|
|
|
keyProviderComponentTypes={kpComponentTypes}
|
|
|
|
/>
|
2021-05-03 17:27:56 +00:00
|
|
|
</Tab>
|
2021-05-14 18:58:08 +00:00
|
|
|
</Tabs>
|
|
|
|
</Tab>
|
2021-04-30 19:08:12 +00:00
|
|
|
</KeycloakTabs>
|
|
|
|
</FormProvider>
|
2021-01-13 19:59:45 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
2020-09-09 09:07:17 +00:00
|
|
|
};
|