2023-01-05 11:38:16 +00:00
|
|
|
import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation";
|
2021-01-26 01:41:14 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
PageSection,
|
2021-03-16 14:24:32 +00:00
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
2021-01-26 01:41:14 +00:00
|
|
|
} from "@patternfly/react-core";
|
2023-01-05 11:38:16 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { FormProvider, useForm } from "react-hook-form";
|
2021-02-08 20:50:03 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-01-05 11:38:16 +00:00
|
|
|
import { useParams } from "react-router-dom-v5-compat";
|
2021-02-08 20:50:03 +00:00
|
|
|
|
2023-01-05 11:38:16 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner";
|
2022-08-22 14:29:35 +00:00
|
|
|
import {
|
|
|
|
RoutableTabs,
|
2023-01-05 11:38:16 +00:00
|
|
|
useRoutableTab,
|
2022-08-22 14:29:35 +00:00
|
|
|
} from "../components/routable-tabs/RoutableTabs";
|
2023-01-05 11:38:16 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
|
|
|
import { LdapMapperList } from "./ldap/mappers/LdapMapperList";
|
|
|
|
import {
|
|
|
|
toUserFederationLdap,
|
|
|
|
UserFederationLdapParams,
|
|
|
|
UserFederationLdapTab,
|
|
|
|
} from "./routes/UserFederationLdap";
|
|
|
|
import { ExtendedHeader } from "./shared/ExtendedHeader";
|
|
|
|
import {
|
|
|
|
LdapComponentRepresentation,
|
|
|
|
serializeFormData,
|
|
|
|
UserFederationLdapForm,
|
|
|
|
} from "./UserFederationLdapForm";
|
2020-10-30 20:15:37 +00:00
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function UserFederationLdapSettings() {
|
2020-10-30 20:15:37 +00:00
|
|
|
const { t } = useTranslation("user-federation");
|
2023-01-05 11:38:16 +00:00
|
|
|
const form = useForm<LdapComponentRepresentation>({ mode: "onChange" });
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-01-26 01:41:14 +00:00
|
|
|
const { realm } = useRealm();
|
2023-01-05 11:38:16 +00:00
|
|
|
const { id } = useParams<UserFederationLdapParams>();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-11-09 14:28:03 +00:00
|
|
|
const [component, setComponent] = useState<ComponentRepresentation>();
|
|
|
|
const [refreshCount, setRefreshCount] = useState(0);
|
|
|
|
|
|
|
|
const refresh = () => setRefreshCount((count) => count + 1);
|
2021-01-26 01:41:14 +00:00
|
|
|
|
2021-05-18 08:34:16 +00:00
|
|
|
useFetch(
|
2023-01-05 11:38:16 +00:00
|
|
|
() => adminClient.components.findOne({ id: id! }),
|
|
|
|
(component) => {
|
|
|
|
if (!component) {
|
2021-09-30 08:58:48 +00:00
|
|
|
throw new Error(t("common:notFound"));
|
2021-05-18 08:34:16 +00:00
|
|
|
}
|
2023-01-05 11:38:16 +00:00
|
|
|
|
|
|
|
setComponent(component);
|
|
|
|
setupForm(component);
|
2021-05-18 08:34:16 +00:00
|
|
|
},
|
2023-01-05 11:38:16 +00:00
|
|
|
[id, refreshCount]
|
2021-05-18 08:34:16 +00:00
|
|
|
);
|
2021-01-26 01:41:14 +00:00
|
|
|
|
2023-01-05 11:38:16 +00:00
|
|
|
const useTab = (tab: UserFederationLdapTab) =>
|
|
|
|
useRoutableTab(toUserFederationLdap({ realm, id: id!, tab }));
|
|
|
|
|
|
|
|
const settingsTab = useTab("settings");
|
|
|
|
const mappersTab = useTab("mappers");
|
|
|
|
|
2021-01-26 01:41:14 +00:00
|
|
|
const setupForm = (component: ComponentRepresentation) => {
|
2023-01-05 11:38:16 +00:00
|
|
|
form.reset(component);
|
2021-12-08 15:08:42 +00:00
|
|
|
form.setValue(
|
|
|
|
"config.periodicChangedUsersSync",
|
2022-11-18 15:54:21 +00:00
|
|
|
component.config?.["changedSyncPeriod"]?.[0] !== "-1"
|
2021-12-08 15:08:42 +00:00
|
|
|
);
|
2021-05-13 21:45:11 +00:00
|
|
|
|
2021-12-08 15:08:42 +00:00
|
|
|
form.setValue(
|
|
|
|
"config.periodicFullSync",
|
2022-11-18 15:54:21 +00:00
|
|
|
component.config?.["fullSyncPeriod"]?.[0] !== "-1"
|
2021-12-08 15:08:42 +00:00
|
|
|
);
|
2021-01-26 01:41:14 +00:00
|
|
|
};
|
|
|
|
|
2023-01-05 11:38:16 +00:00
|
|
|
const onSubmit = async (formData: LdapComponentRepresentation) => {
|
2021-01-26 01:41:14 +00:00
|
|
|
try {
|
2023-01-05 11:38:16 +00:00
|
|
|
await adminClient.components.update(
|
|
|
|
{ id: id! },
|
|
|
|
serializeFormData(formData)
|
|
|
|
);
|
|
|
|
addAlert(t("saveSuccess"), AlertVariant.success);
|
2021-11-09 14:28:03 +00:00
|
|
|
refresh();
|
2021-01-26 01:41:14 +00:00
|
|
|
} catch (error) {
|
2023-01-05 11:38:16 +00:00
|
|
|
addError("user-federation:saveError", error);
|
2021-01-26 01:41:14 +00:00
|
|
|
}
|
|
|
|
};
|
2020-10-30 20:15:37 +00:00
|
|
|
|
2023-01-05 11:38:16 +00:00
|
|
|
if (!component) {
|
|
|
|
return <KeycloakSpinner />;
|
|
|
|
}
|
|
|
|
|
2020-10-30 20:15:37 +00:00
|
|
|
return (
|
2022-01-05 17:06:53 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<ExtendedHeader
|
|
|
|
provider="LDAP"
|
|
|
|
noDivider
|
2023-01-05 11:38:16 +00:00
|
|
|
editMode={component.config?.editMode}
|
|
|
|
save={() => form.handleSubmit(onSubmit)()}
|
2021-02-08 20:50:03 +00:00
|
|
|
/>
|
2022-01-05 17:06:53 +00:00
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
2023-01-05 11:38:16 +00:00
|
|
|
<RoutableTabs
|
|
|
|
defaultLocation={toUserFederationLdap({
|
|
|
|
realm,
|
|
|
|
id: id!,
|
|
|
|
tab: "settings",
|
|
|
|
})}
|
|
|
|
isBox
|
|
|
|
>
|
|
|
|
<Tab
|
|
|
|
id="settings"
|
|
|
|
title={<TabTitleText>{t("common:settings")}</TabTitleText>}
|
|
|
|
{...settingsTab}
|
|
|
|
>
|
|
|
|
<PageSection variant="light">
|
|
|
|
<UserFederationLdapForm id={id} onSubmit={onSubmit} />
|
|
|
|
</PageSection>
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
id="mappers"
|
|
|
|
title={<TabTitleText>{t("common:mappers")}</TabTitleText>}
|
|
|
|
data-testid="ldap-mappers-tab"
|
|
|
|
{...mappersTab}
|
2022-08-22 14:29:35 +00:00
|
|
|
>
|
2023-01-05 11:38:16 +00:00
|
|
|
<LdapMapperList />
|
|
|
|
</Tab>
|
|
|
|
</RoutableTabs>
|
2020-10-30 20:15:37 +00:00
|
|
|
</PageSection>
|
2022-01-05 17:06:53 +00:00
|
|
|
</FormProvider>
|
2020-10-30 20:15:37 +00:00
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|