import { AlertVariant, Breadcrumb, BreadcrumbItem, ButtonVariant, DropdownItem, DropdownSeparator, PageSection, Spinner, Tab, Tabs, TabTitleText, } from "@patternfly/react-core"; import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation"; import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation"; import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation"; import React, { useEffect, useState } from "react"; import { Controller, FormProvider, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { Link, useHistory } from "react-router-dom"; import { useAlerts } from "../components/alert/Alerts"; import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; import { KeycloakTabs } from "../components/keycloak-tabs/KeycloakTabs"; import { ViewHeader } from "../components/view-header/ViewHeader"; import { useAdminClient, useFetch } from "../context/auth/AdminClient"; import { useRealm } from "../context/realm-context/RealmContext"; import { useServerInfo } from "../context/server-info/ServerInfoProvider"; import { LocalizationTab } from "./LocalizationTab"; import { useWhoAmI } from "../context/whoami/WhoAmI"; import { convertToFormValues, KEY_PROVIDER_TYPE, toUpperCase } from "../util"; import { RealmSettingsEmailTab } from "./EmailTab"; import { EventsTab } from "./event-config/EventsTab"; import { RealmSettingsGeneralTab } from "./GeneralTab"; import { KeysListTab } from "./KeysListTab"; import { KeysProvidersTab } from "./KeysProvidersTab"; import { RealmSettingsLoginTab } from "./LoginTab"; import { PartialImportDialog } from "./PartialImport"; import { toRealmSettings } from "./routes/RealmSettings"; import { SecurityDefences } from "./security-defences/SecurityDefences"; import { RealmSettingsSessionsTab } from "./SessionsTab"; import { RealmSettingsThemesTab } from "./ThemesTab"; import { RealmSettingsTokensTab } from "./TokensTab"; type RealmSettingsHeaderProps = { onChange: (value: boolean) => void; value: boolean; save: () => void; realmName: string; }; export const EditProviderCrumb = () => { const { t } = useTranslation("realm-settings"); const { realm } = useRealm(); return ( ( {t("keys")} )} /> {t("providers")} {t("editProvider")} ); }; const RealmSettingsHeader = ({ save, onChange, value, realmName, }: RealmSettingsHeaderProps) => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const { addAlert, addError } = useAlerts(); const history = useHistory(); const { refresh } = useRealm(); const [partialImportOpen, setPartialImportOpen] = useState(false); 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); history.push("/master/"); refresh(); } catch (error) { addError("realm-settings:deleteError", error); } }, }); return ( <> setPartialImportOpen(!partialImportOpen)} /> { setPartialImportOpen(true); }} > {t("partialImport")} , {t("partialExport")}, , {t("common:delete")} , ]} isEnabled={value} onToggle={(value) => { if (!value) { toggleDisableDialog(); } else { onChange(value); save(); } }} /> ); }; const sortByPriority = (components: ComponentRepresentation[]) => { const sortedComponents = [...components].sort((a, b) => { const priorityA = Number(a.config?.priority); const priorityB = Number(b.config?.priority); return ( (!isNaN(priorityB) ? priorityB : 0) - (!isNaN(priorityA) ? priorityA : 0) ); }); return sortedComponents; }; export const RealmSettingsSection = () => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const { realm: realmName, refresh: refreshRealm, setRealm: setCurrentRealm, } = useRealm(); const { addAlert, addError } = useAlerts(); const form = useForm({ mode: "onChange" }); const { control, getValues, setValue, reset: resetForm } = form; const [key, setKey] = useState(0); const [realm, setRealm] = useState(); const [activeTab, setActiveTab] = useState(0); const [realmComponents, setRealmComponents] = useState(); const [currentUser, setCurrentUser] = useState(); const { whoAmI } = useWhoAmI(); const history = useHistory(); const kpComponentTypes = useServerInfo().componentTypes?.[KEY_PROVIDER_TYPE] ?? []; useFetch( async () => { const realm = await adminClient.realms.findOne({ realm: realmName }); const realmComponents = await adminClient.components.find({ type: KEY_PROVIDER_TYPE, realm: realmName, }); const user = await adminClient.users.findOne({ id: whoAmI.getUserId() }); return { user, realm, realmComponents }; }, ({ user, realm, realmComponents }) => { setRealmComponents(sortByPriority(realmComponents)); setCurrentUser(user); setRealm(realm); }, [key] ); const refresh = () => { setKey(new Date().getTime()); }; useEffect(() => { if (realm) { Object.entries(realm).map(([key, value]) => { if (key === "attributes") { convertToFormValues(value, "attributes", form.setValue); } else { setValue(key, value); } }); resetForm(getValues()); } }, [realm]); const save = async (realm: RealmRepresentation) => { try { await adminClient.realms.update( { realm: realmName }, { ...realm, id: realmName } ); setRealm(realm); const isRealmRenamed = realmName !== realm.realm; if (isRealmRenamed) { await refreshRealm(); setCurrentRealm(realm.realm!); history.push(toRealmSettings({ realm: realm.realm! })); } addAlert(t("saveSuccess"), AlertVariant.success); } catch (error) { addError("realm-settings:saveError", error); } }; if (!realm || !realmComponents || !currentUser) { return (
); } return ( <> ( save(getValues())} /> )} /> {t("general")}} data-testid="rs-general-tab" aria-label="general-tab" > resetForm(realm)} /> {t("login")}} data-testid="rs-login-tab" aria-label="login-tab" > {t("email")}} data-testid="rs-email-tab" aria-label="email-tab" > {t("themes")}} data-testid="rs-themes-tab" aria-label="themes-tab" > resetForm(realm)} realm={realm} /> {t("realm-settings:keys")}} data-testid="rs-keys-tab" aria-label="keys-tab" > setActiveTab(Number(key))} > {t("keysList")}} > {t("providers")}} > {t("events")}} data-testid="rs-realm-events-tab" aria-label="realm-events-tab" > {t("localization")}} > resetForm(realm)} realm={realm} /> {t("securityDefences")}} > resetForm(realm)} /> {t("realm-settings:sessions")} } > {t("realm-settings:tokens")}} > resetForm(realm)} /> ); };