2021-06-10 20:00:14 +00:00
|
|
|
import React, { useEffect, useState, useContext } 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-06-08 05:29:56 +00:00
|
|
|
import { EventsTab } from "./event-config/EventsTab";
|
2021-05-18 19:21:45 +00:00
|
|
|
import type ComponentRepresentation from "keycloak-admin/lib/defs/componentRepresentation";
|
2021-07-12 18:19:50 +00:00
|
|
|
import { KeysProvidersTab } from "./KeysProvidersTab";
|
2021-05-03 21:07:22 +00:00
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
2021-07-16 08:10:06 +00:00
|
|
|
//import { LocalizationTab } from "./LocalizationTab";
|
2021-06-10 20:00:14 +00:00
|
|
|
import { WhoAmIContext } from "../context/whoami/WhoAmI";
|
|
|
|
import type UserRepresentation from "keycloak-admin/lib/defs/userRepresentation";
|
2021-07-09 14:23:49 +00:00
|
|
|
import { SecurityDefences } from "./security-defences/SecurityDefences";
|
2021-07-15 14:25:22 +00:00
|
|
|
import { RealmSettingsSessionsTab } from "./SessionsTab";
|
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-07-09 14:23:49 +00:00
|
|
|
const form = useForm({ mode: "onChange" });
|
2021-05-27 07:01:55 +00:00
|
|
|
const { control, getValues, setValue, reset: resetForm } = form;
|
2021-06-16 11:38:36 +00:00
|
|
|
const [key, setKey] = useState(0);
|
2021-04-30 19:08:12 +00:00
|
|
|
const [realm, setRealm] = useState<RealmRepresentation>();
|
2021-05-27 07:01:55 +00:00
|
|
|
const [activeTab, setActiveTab] = useState(0);
|
2021-07-05 11:24:10 +00:00
|
|
|
const [realmComponents, setRealmComponents] =
|
|
|
|
useState<ComponentRepresentation[]>();
|
2021-06-10 20:00:14 +00:00
|
|
|
const [currentUser, setCurrentUser] = useState<UserRepresentation>();
|
|
|
|
const { whoAmI } = useContext(WhoAmIContext);
|
2021-01-13 19:59:45 +00:00
|
|
|
|
2021-07-05 11:24:10 +00:00
|
|
|
const kpComponentTypes =
|
|
|
|
useServerInfo().componentTypes!["org.keycloak.keys.KeyProvider"];
|
2021-05-03 21:07:22 +00:00
|
|
|
|
2021-04-29 06:28:59 +00:00
|
|
|
useFetch(
|
2021-05-27 07:01:55 +00:00
|
|
|
async () => {
|
|
|
|
const realm = await adminClient.realms.findOne({ realm: realmName });
|
|
|
|
const realmComponents = await adminClient.components.find({
|
|
|
|
type: "org.keycloak.keys.KeyProvider",
|
|
|
|
realm: realmName,
|
|
|
|
});
|
2021-07-09 14:23:49 +00:00
|
|
|
const user = await adminClient.users.findOne({ id: whoAmI.getUserId()! });
|
2021-05-27 07:01:55 +00:00
|
|
|
|
2021-07-09 14:23:49 +00:00
|
|
|
return { user, realm, realmComponents };
|
2021-05-27 07:01:55 +00:00
|
|
|
},
|
2021-07-09 14:23:49 +00:00
|
|
|
({ user, realm, realmComponents }) => {
|
|
|
|
setRealmComponents(realmComponents);
|
|
|
|
setCurrentUser(user);
|
|
|
|
setRealm(realm);
|
2021-04-29 06:28:59 +00:00
|
|
|
},
|
2021-06-16 11:38:36 +00:00
|
|
|
[key]
|
2021-04-29 06:28:59 +00:00
|
|
|
);
|
2021-01-13 19:59:45 +00:00
|
|
|
|
2021-06-03 18:31:47 +00:00
|
|
|
const refresh = () => {
|
|
|
|
setKey(new Date().getTime());
|
|
|
|
};
|
|
|
|
|
2021-05-14 18:58:08 +00:00
|
|
|
useEffect(() => {
|
2021-07-09 14:23:49 +00:00
|
|
|
if (realm) {
|
|
|
|
Object.entries(realm).map((entry) => setValue(entry[0], entry[1]));
|
|
|
|
resetForm({ ...realm });
|
|
|
|
}
|
|
|
|
}, [realm, resetForm]);
|
2021-01-13 19:59:45 +00:00
|
|
|
|
|
|
|
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) {
|
2021-05-27 07:01:55 +00:00
|
|
|
addAlert(
|
|
|
|
t("saveError", { error: error.response?.data?.errorMessage || error }),
|
|
|
|
AlertVariant.danger
|
|
|
|
);
|
2021-01-13 19:59:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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"
|
2021-06-08 05:29:56 +00:00
|
|
|
title={<TabTitleText>{t("general")}</TabTitleText>}
|
2021-04-30 19:08:12 +00:00
|
|
|
data-testid="rs-general-tab"
|
2021-07-15 14:25:22 +00:00
|
|
|
aria-label="general-tab"
|
2021-04-30 19:08:12 +00:00
|
|
|
>
|
|
|
|
<RealmSettingsGeneralTab
|
|
|
|
save={save}
|
2021-07-09 14:23:49 +00:00
|
|
|
reset={() => resetForm(realm!)}
|
2021-04-30 19:08:12 +00:00
|
|
|
/>
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
eventKey="login"
|
2021-06-08 05:29:56 +00:00
|
|
|
title={<TabTitleText>{t("login")}</TabTitleText>}
|
2021-04-30 19:08:12 +00:00
|
|
|
data-testid="rs-login-tab"
|
2021-07-15 14:25:22 +00:00
|
|
|
aria-label="login-tab"
|
2021-04-30 19:08:12 +00:00
|
|
|
>
|
|
|
|
<RealmSettingsLoginTab save={save} realm={realm!} />
|
|
|
|
</Tab>
|
2021-05-03 20:00:12 +00:00
|
|
|
<Tab
|
|
|
|
eventKey="email"
|
2021-06-08 05:29:56 +00:00
|
|
|
title={<TabTitleText>{t("email")}</TabTitleText>}
|
2021-05-03 20:00:12 +00:00
|
|
|
data-testid="rs-email-tab"
|
2021-07-15 14:25:22 +00:00
|
|
|
aria-label="email-tab"
|
2021-05-03 20:00:12 +00:00
|
|
|
>
|
2021-06-10 20:00:14 +00:00
|
|
|
{realm && (
|
|
|
|
<RealmSettingsEmailTab user={currentUser!} realm={realm} />
|
|
|
|
)}
|
2021-05-03 20:00:12 +00:00
|
|
|
</Tab>
|
2021-04-30 19:08:12 +00:00
|
|
|
<Tab
|
|
|
|
eventKey="themes"
|
2021-06-08 05:29:56 +00:00
|
|
|
title={<TabTitleText>{t("themes")}</TabTitleText>}
|
2021-04-30 19:08:12 +00:00
|
|
|
data-testid="rs-themes-tab"
|
2021-07-15 14:25:22 +00:00
|
|
|
aria-label="themes-tab"
|
2021-04-30 19:08:12 +00:00
|
|
|
>
|
|
|
|
<RealmSettingsThemesTab
|
|
|
|
save={save}
|
2021-07-09 14:23:49 +00:00
|
|
|
reset={() => resetForm(realm!)}
|
2021-06-16 11:38:36 +00:00
|
|
|
realm={realm!}
|
2021-04-30 19:08:12 +00:00
|
|
|
/>
|
|
|
|
</Tab>
|
2021-05-14 18:58:08 +00:00
|
|
|
<Tab
|
|
|
|
eventKey="keys"
|
|
|
|
title={<TabTitleText>{t("realm-settings:keys")}</TabTitleText>}
|
|
|
|
data-testid="rs-keys-tab"
|
2021-07-15 14:25:22 +00:00
|
|
|
aria-label="keys-tab"
|
2021-05-14 18:58:08 +00:00
|
|
|
>
|
2021-05-27 07:01:55 +00:00
|
|
|
{realmComponents && (
|
|
|
|
<Tabs
|
|
|
|
activeKey={activeTab}
|
|
|
|
onSelect={(_, key) => setActiveTab(key as number)}
|
2021-05-03 17:27:56 +00:00
|
|
|
>
|
2021-05-27 07:01:55 +00:00
|
|
|
<Tab
|
2021-06-03 18:31:47 +00:00
|
|
|
id="keysList"
|
2021-05-27 07:01:55 +00:00
|
|
|
eventKey={0}
|
2021-06-08 15:45:37 +00:00
|
|
|
data-testid="rs-keys-list-tab"
|
2021-07-15 14:25:22 +00:00
|
|
|
aria-label="keys-list-subtab"
|
2021-05-27 07:01:55 +00:00
|
|
|
title={<TabTitleText>{t("keysList")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<KeysListTab realmComponents={realmComponents} />
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
2021-06-03 18:31:47 +00:00
|
|
|
id="providers"
|
|
|
|
data-testid="rs-providers-tab"
|
2021-07-15 14:25:22 +00:00
|
|
|
aria-label="rs-providers-tab"
|
2021-05-27 07:01:55 +00:00
|
|
|
eventKey={1}
|
|
|
|
title={<TabTitleText>{t("providers")}</TabTitleText>}
|
|
|
|
>
|
2021-07-12 18:19:50 +00:00
|
|
|
<KeysProvidersTab
|
2021-06-03 20:40:48 +00:00
|
|
|
realmComponents={realmComponents}
|
2021-05-27 07:01:55 +00:00
|
|
|
keyProviderComponentTypes={kpComponentTypes}
|
2021-06-03 20:40:48 +00:00
|
|
|
refresh={refresh}
|
2021-05-27 07:01:55 +00:00
|
|
|
/>
|
|
|
|
</Tab>
|
|
|
|
</Tabs>
|
|
|
|
)}
|
2021-05-14 18:58:08 +00:00
|
|
|
</Tab>
|
2021-06-08 05:29:56 +00:00
|
|
|
<Tab
|
|
|
|
eventKey="events"
|
|
|
|
title={<TabTitleText>{t("events")}</TabTitleText>}
|
|
|
|
data-testid="rs-realm-events-tab"
|
2021-07-15 14:25:22 +00:00
|
|
|
aria-label="realm-events-tab"
|
2021-06-08 05:29:56 +00:00
|
|
|
>
|
|
|
|
<EventsTab />
|
|
|
|
</Tab>
|
2021-06-16 11:38:36 +00:00
|
|
|
|
2021-07-16 08:10:06 +00:00
|
|
|
{/* <Tab
|
2021-07-09 14:23:49 +00:00
|
|
|
id="localization"
|
|
|
|
eventKey="localization"
|
|
|
|
data-testid="rs-localization-tab"
|
|
|
|
title={<TabTitleText>{t("localization")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
{realm && (
|
2021-06-16 11:38:36 +00:00
|
|
|
<LocalizationTab
|
|
|
|
key={key}
|
|
|
|
refresh={refresh}
|
|
|
|
save={save}
|
2021-07-09 14:23:49 +00:00
|
|
|
reset={() => resetForm(realm)}
|
2021-06-16 11:38:36 +00:00
|
|
|
realm={realm}
|
|
|
|
/>
|
2021-07-09 14:23:49 +00:00
|
|
|
)}
|
2021-07-16 08:10:06 +00:00
|
|
|
</Tab> */}
|
2021-07-09 14:23:49 +00:00
|
|
|
<Tab
|
|
|
|
id="securityDefences"
|
|
|
|
eventKey="securityDefences"
|
|
|
|
title={<TabTitleText>{t("securityDefences")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
{realm && (
|
|
|
|
<SecurityDefences save={save} reset={() => resetForm(realm)} />
|
|
|
|
)}
|
|
|
|
</Tab>
|
2021-07-15 14:25:22 +00:00
|
|
|
<Tab
|
|
|
|
id="sessions"
|
|
|
|
eventKey="sessions"
|
|
|
|
data-testid="rs-sessions-tab"
|
|
|
|
aria-label="sessions-tab"
|
|
|
|
title={
|
|
|
|
<TabTitleText>{t("realm-settings:sessions")}</TabTitleText>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<RealmSettingsSessionsTab key={key} realm={realm} />
|
|
|
|
</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
|
|
|
};
|