2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2021-01-26 01:41:14 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
Form,
|
|
|
|
PageSection,
|
2021-03-16 14:24:32 +00:00
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
2021-01-26 01:41:14 +00:00
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
2020-12-16 07:02:41 +00:00
|
|
|
import { LdapSettingsAdvanced } from "./ldap/LdapSettingsAdvanced";
|
|
|
|
import { LdapSettingsKerberosIntegration } from "./ldap/LdapSettingsKerberosIntegration";
|
2021-02-11 20:51:51 +00:00
|
|
|
import { SettingsCache } from "./shared/SettingsCache";
|
2020-12-16 07:02:41 +00:00
|
|
|
import { LdapSettingsSynchronization } from "./ldap/LdapSettingsSynchronization";
|
|
|
|
import { LdapSettingsGeneral } from "./ldap/LdapSettingsGeneral";
|
|
|
|
import { LdapSettingsConnection } from "./ldap/LdapSettingsConnection";
|
|
|
|
import { LdapSettingsSearching } from "./ldap/LdapSettingsSearching";
|
2021-01-26 01:41:14 +00:00
|
|
|
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation";
|
2021-01-26 01:41:14 +00:00
|
|
|
|
2022-01-05 17:06:53 +00:00
|
|
|
import { FormProvider, useForm, useFormContext } from "react-hook-form";
|
2021-05-18 08:34:16 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
2021-02-08 20:50:03 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2022-08-16 13:09:14 +00:00
|
|
|
import { useParams } from "react-router-dom";
|
|
|
|
import { useNavigate } from "react-router-dom-v5-compat";
|
2021-02-08 20:50:03 +00:00
|
|
|
import { ScrollForm } from "../components/scroll-form/ScrollForm";
|
|
|
|
|
2021-03-16 14:24:32 +00:00
|
|
|
import { KeycloakTabs } from "../components/keycloak-tabs/KeycloakTabs";
|
2021-03-29 15:52:56 +00:00
|
|
|
import { LdapMapperList } from "./ldap/mappers/LdapMapperList";
|
2022-01-05 17:06:53 +00:00
|
|
|
import { toUserFederation } from "./routes/UserFederation";
|
|
|
|
import { ExtendedHeader } from "./shared/ExtendedHeader";
|
2021-03-16 14:24:32 +00:00
|
|
|
|
2021-05-14 19:44:45 +00:00
|
|
|
type ldapComponentRepresentation = ComponentRepresentation & {
|
|
|
|
config?: {
|
|
|
|
periodicChangedUsersSync?: boolean;
|
|
|
|
periodicFullSync?: boolean;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-01-05 17:06:53 +00:00
|
|
|
const AddLdapFormContent = ({
|
2021-02-08 20:50:03 +00:00
|
|
|
save,
|
2022-01-05 17:06:53 +00:00
|
|
|
}: {
|
|
|
|
save: (component: ldapComponentRepresentation) => void;
|
|
|
|
}) => {
|
2021-02-08 20:50:03 +00:00
|
|
|
const { t } = useTranslation("user-federation");
|
2022-01-05 17:06:53 +00:00
|
|
|
const form = useFormContext();
|
2021-02-19 23:13:07 +00:00
|
|
|
const { id } = useParams<{ id: string }>();
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2021-03-09 19:55:08 +00:00
|
|
|
|
2022-01-05 17:06:53 +00:00
|
|
|
const { realm } = useRealm();
|
2021-03-09 19:55:08 +00:00
|
|
|
|
2021-02-08 20:50:03 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-01-05 17:06:53 +00:00
|
|
|
<ScrollForm
|
|
|
|
sections={[
|
2022-05-30 11:07:33 +00:00
|
|
|
{
|
|
|
|
title: t("generalOptions"),
|
|
|
|
panel: <LdapSettingsGeneral form={form} vendorEdit={!!id} />,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("connectionAndAuthenticationSettings"),
|
|
|
|
panel: <LdapSettingsConnection form={form} id={id} />,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("ldapSearchingAndUpdatingSettings"),
|
|
|
|
panel: <LdapSettingsSearching form={form} />,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("synchronizationSettings"),
|
|
|
|
panel: <LdapSettingsSynchronization form={form} />,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("kerberosIntegration"),
|
|
|
|
panel: <LdapSettingsKerberosIntegration form={form} />,
|
|
|
|
},
|
|
|
|
{ title: t("cacheSettings"), panel: <SettingsCache form={form} /> },
|
|
|
|
{
|
|
|
|
title: t("advancedSettings"),
|
|
|
|
panel: <LdapSettingsAdvanced form={form} id={id} />,
|
|
|
|
},
|
2022-01-05 17:06:53 +00:00
|
|
|
]}
|
2022-05-30 11:07:33 +00:00
|
|
|
/>
|
2022-01-05 17:06:53 +00:00
|
|
|
<Form onSubmit={form.handleSubmit(save)}>
|
|
|
|
<ActionGroup className="keycloak__form_actions">
|
|
|
|
<Button
|
|
|
|
isDisabled={!form.formState.isDirty}
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
data-testid="ldap-save"
|
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
2022-08-16 13:09:14 +00:00
|
|
|
onClick={() => navigate(toUserFederation({ realm }))}
|
2022-01-05 17:06:53 +00:00
|
|
|
data-testid="ldap-cancel"
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</Form>
|
2021-02-08 20:50:03 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
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");
|
2021-05-06 13:03:25 +00:00
|
|
|
const form = useForm<ComponentRepresentation>({ mode: "onChange" });
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-01-26 01:41:14 +00:00
|
|
|
const { realm } = useRealm();
|
|
|
|
|
|
|
|
const { id } = useParams<{ id: string }>();
|
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 editMode = component?.config?.editMode;
|
|
|
|
const refresh = () => setRefreshCount((count) => count + 1);
|
2021-01-26 01:41:14 +00:00
|
|
|
|
2021-05-18 08:34:16 +00:00
|
|
|
useFetch(
|
|
|
|
async () => {
|
|
|
|
if (id) {
|
|
|
|
return await adminClient.components.findOne({ id });
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
(fetchedComponent) => {
|
|
|
|
if (fetchedComponent) {
|
|
|
|
setupForm(fetchedComponent);
|
2021-11-09 14:28:03 +00:00
|
|
|
setComponent(fetchedComponent);
|
2021-09-30 08:58:48 +00:00
|
|
|
} else if (id) {
|
|
|
|
throw new Error(t("common:notFound"));
|
2021-05-18 08:34:16 +00:00
|
|
|
}
|
|
|
|
},
|
2021-11-09 14:28:03 +00:00
|
|
|
[refreshCount]
|
2021-05-18 08:34:16 +00:00
|
|
|
);
|
2021-01-26 01:41:14 +00:00
|
|
|
|
|
|
|
const setupForm = (component: ComponentRepresentation) => {
|
2021-12-08 15:08:42 +00:00
|
|
|
form.reset({ ...component });
|
|
|
|
form.setValue(
|
|
|
|
"config.periodicChangedUsersSync",
|
|
|
|
component.config?.["changedSyncPeriod"][0] !== "-1"
|
|
|
|
);
|
2021-05-13 21:45:11 +00:00
|
|
|
|
2021-12-08 15:08:42 +00:00
|
|
|
form.setValue(
|
|
|
|
"config.periodicFullSync",
|
|
|
|
component.config?.["fullSyncPeriod"][0] !== "-1"
|
|
|
|
);
|
2021-01-26 01:41:14 +00:00
|
|
|
};
|
|
|
|
|
2021-05-14 19:44:45 +00:00
|
|
|
const save = async (component: ldapComponentRepresentation) => {
|
2022-01-05 17:06:53 +00:00
|
|
|
if (component.config?.periodicChangedUsersSync !== undefined) {
|
|
|
|
if (component.config.periodicChangedUsersSync === false) {
|
2021-05-13 21:45:11 +00:00
|
|
|
component.config.changedSyncPeriod = ["-1"];
|
|
|
|
}
|
2022-01-05 17:06:53 +00:00
|
|
|
delete component.config.periodicChangedUsersSync;
|
2021-05-13 21:45:11 +00:00
|
|
|
}
|
2022-01-05 17:06:53 +00:00
|
|
|
if (component.config?.periodicFullSync !== undefined) {
|
|
|
|
if (component.config.periodicFullSync === false) {
|
2021-05-13 21:45:11 +00:00
|
|
|
component.config.fullSyncPeriod = ["-1"];
|
|
|
|
}
|
2022-01-05 17:06:53 +00:00
|
|
|
delete component.config.periodicFullSync;
|
2021-05-13 21:45:11 +00:00
|
|
|
}
|
2021-01-26 01:41:14 +00:00
|
|
|
try {
|
2021-04-16 05:35:51 +00:00
|
|
|
if (!id) {
|
|
|
|
await adminClient.components.create(component);
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toUserFederation({ realm }));
|
2021-04-16 05:35:51 +00:00
|
|
|
} else {
|
|
|
|
await adminClient.components.update({ id }, component);
|
2021-02-19 23:13:07 +00:00
|
|
|
}
|
2021-04-16 20:28:52 +00:00
|
|
|
addAlert(t(id ? "saveSuccess" : "createSuccess"), AlertVariant.success);
|
2021-11-09 14:28:03 +00:00
|
|
|
refresh();
|
2021-01-26 01:41:14 +00:00
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError(`user-federation:${id ? "saveError" : "createError"}`, error);
|
2021-01-26 01:41:14 +00:00
|
|
|
}
|
|
|
|
};
|
2020-10-30 20:15:37 +00:00
|
|
|
|
|
|
|
return (
|
2022-01-05 17:06:53 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<ExtendedHeader
|
|
|
|
provider="LDAP"
|
|
|
|
noDivider
|
|
|
|
editMode={editMode}
|
|
|
|
save={() => form.handleSubmit(save)()}
|
2021-02-08 20:50:03 +00:00
|
|
|
/>
|
2022-01-05 17:06:53 +00:00
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
2021-06-09 20:13:56 +00:00
|
|
|
{id ? (
|
|
|
|
<KeycloakTabs isBox>
|
|
|
|
<Tab
|
|
|
|
id="settings"
|
|
|
|
eventKey="settings"
|
|
|
|
title={<TabTitleText>{t("common:settings")}</TabTitleText>}
|
2021-01-26 01:41:14 +00:00
|
|
|
>
|
2022-01-05 17:06:53 +00:00
|
|
|
<PageSection variant="light">
|
|
|
|
<AddLdapFormContent save={save} />
|
|
|
|
</PageSection>
|
2021-06-09 20:13:56 +00:00
|
|
|
</Tab>
|
2021-04-16 20:28:52 +00:00
|
|
|
<Tab
|
|
|
|
id="mappers"
|
|
|
|
eventKey="mappers"
|
|
|
|
title={<TabTitleText>{t("common:mappers")}</TabTitleText>}
|
2021-04-28 05:50:41 +00:00
|
|
|
data-testid="ldap-mappers-tab"
|
2021-04-16 20:28:52 +00:00
|
|
|
>
|
2022-05-11 09:42:46 +00:00
|
|
|
<LdapMapperList />
|
2021-04-16 20:28:52 +00:00
|
|
|
</Tab>
|
2021-06-09 20:13:56 +00:00
|
|
|
</KeycloakTabs>
|
|
|
|
) : (
|
2022-01-05 17:06:53 +00:00
|
|
|
<PageSection variant="light">
|
|
|
|
<AddLdapFormContent save={save} />
|
|
|
|
</PageSection>
|
2021-06-09 20:13:56 +00:00
|
|
|
)}
|
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
|
|
|
}
|