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-18 12:09:49 +00:00
|
|
|
import { useParams } from "react-router-dom";
|
2021-02-08 20:50:03 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../admin-client";
|
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 { useRealm } from "../context/realm-context/RealmContext";
|
2023-05-03 15:40:27 +00:00
|
|
|
import { useFetch } from "../utils/useFetch";
|
2023-05-03 13:51:02 +00:00
|
|
|
import {
|
|
|
|
LdapComponentRepresentation,
|
|
|
|
UserFederationLdapForm,
|
|
|
|
serializeFormData,
|
|
|
|
} from "./UserFederationLdapForm";
|
2023-01-05 11:38:16 +00:00
|
|
|
import { LdapMapperList } from "./ldap/mappers/LdapMapperList";
|
|
|
|
import {
|
|
|
|
UserFederationLdapParams,
|
|
|
|
UserFederationLdapTab,
|
2023-05-03 13:51:02 +00:00
|
|
|
toUserFederationLdap,
|
2023-01-05 11:38:16 +00:00
|
|
|
} from "./routes/UserFederationLdap";
|
2023-01-13 13:26:22 +00:00
|
|
|
import { toUserFederationLdapMapper } from "./routes/UserFederationLdapMapper";
|
2023-01-05 11:38:16 +00:00
|
|
|
import { ExtendedHeader } from "./shared/ExtendedHeader";
|
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" });
|
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-07-11 14:03:21 +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",
|
2023-07-11 14:03: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",
|
2023-07-11 14:03: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! },
|
2023-07-11 14:03:21 +00:00
|
|
|
serializeFormData(formData),
|
2023-01-05 11:38:16 +00:00
|
|
|
);
|
|
|
|
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-13 13:26:22 +00:00
|
|
|
<LdapMapperList
|
|
|
|
toCreate={toUserFederationLdapMapper({
|
|
|
|
realm,
|
|
|
|
id: id!,
|
|
|
|
mapperId: "new",
|
|
|
|
})}
|
|
|
|
toDetail={(mapperId) =>
|
|
|
|
toUserFederationLdapMapper({
|
|
|
|
realm,
|
|
|
|
id: id!,
|
|
|
|
mapperId,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
2023-01-05 11:38:16 +00:00
|
|
|
</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
|
|
|
}
|