2022-12-07 14:23:12 +00:00
|
|
|
import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation";
|
2021-01-22 20:15:32 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
Form,
|
|
|
|
PageSection,
|
|
|
|
} from "@patternfly/react-core";
|
2022-01-05 17:06:53 +00:00
|
|
|
import { FormProvider, useForm } from "react-hook-form";
|
2021-01-20 15:43:52 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-08-16 13:09:14 +00:00
|
|
|
import { useNavigate } from "react-router-dom-v5-compat";
|
2022-12-07 14:23:12 +00:00
|
|
|
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
|
|
|
import { useParams } from "../utils/useParams";
|
|
|
|
import { KerberosSettingsRequired } from "./kerberos/KerberosSettingsRequired";
|
2022-01-05 17:06:53 +00:00
|
|
|
import { toUserFederation } from "./routes/UserFederation";
|
2022-12-07 14:23:12 +00:00
|
|
|
import { Header } from "./shared/Header";
|
|
|
|
import { SettingsCache } from "./shared/SettingsCache";
|
2020-11-09 12:31:05 +00:00
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function UserFederationKerberosSettings() {
|
2021-01-22 20:15:32 +00:00
|
|
|
const { t } = useTranslation("user-federation");
|
|
|
|
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-22 20:15:32 +00:00
|
|
|
const { realm } = useRealm();
|
|
|
|
|
2021-11-09 14:14:54 +00:00
|
|
|
const { id } = useParams<{ id?: string }>();
|
2021-01-22 20:15:32 +00:00
|
|
|
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-01-22 20:15:32 +00:00
|
|
|
|
2021-11-09 14:14:54 +00:00
|
|
|
useFetch(
|
|
|
|
async () => {
|
|
|
|
if (id) {
|
|
|
|
return adminClient.components.findOne({ id });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(fetchedComponent) => {
|
|
|
|
if (fetchedComponent) {
|
|
|
|
setupForm(fetchedComponent);
|
|
|
|
} else if (id) {
|
|
|
|
throw new Error(t("common:notFound"));
|
2021-01-22 20:15:32 +00:00
|
|
|
}
|
2021-11-09 14:14:54 +00:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
2021-01-22 20:15:32 +00:00
|
|
|
|
|
|
|
const setupForm = (component: ComponentRepresentation) => {
|
2021-12-08 15:08:42 +00:00
|
|
|
form.reset({ ...component });
|
2021-01-22 20:15:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const save = async (component: ComponentRepresentation) => {
|
|
|
|
try {
|
2021-11-09 14:14:54 +00:00
|
|
|
if (!id) {
|
|
|
|
await adminClient.components.create(component);
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(`/${realm}/user-federation`);
|
2021-11-09 14:14:54 +00:00
|
|
|
} else {
|
|
|
|
await adminClient.components.update({ id }, component);
|
2021-02-09 21:12:50 +00:00
|
|
|
}
|
2021-01-22 20:15:32 +00:00
|
|
|
setupForm(component as ComponentRepresentation);
|
2021-11-09 14:14:54 +00:00
|
|
|
addAlert(t(!id ? "createSuccess" : "saveSuccess"), AlertVariant.success);
|
2021-01-22 20:15:32 +00:00
|
|
|
} catch (error) {
|
2021-11-09 14:14:54 +00:00
|
|
|
addError(`user-federation:${!id ? "createError" : "saveError"}`, error);
|
2021-01-22 20:15:32 +00:00
|
|
|
}
|
|
|
|
};
|
2021-01-20 15:43:52 +00:00
|
|
|
|
2020-11-09 12:31:05 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-01-05 17:06:53 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<Header provider="Kerberos" save={() => form.handleSubmit(save)()} />
|
|
|
|
</FormProvider>
|
2020-11-09 12:31:05 +00:00
|
|
|
<PageSection variant="light">
|
2021-01-22 20:15:32 +00:00
|
|
|
<KerberosSettingsRequired form={form} showSectionHeading />
|
2020-11-09 12:31:05 +00:00
|
|
|
</PageSection>
|
|
|
|
<PageSection variant="light" isFilled>
|
2021-02-11 20:51:51 +00:00
|
|
|
<SettingsCache form={form} showSectionHeading />
|
2021-01-22 20:15:32 +00:00
|
|
|
<Form onSubmit={form.handleSubmit(save)}>
|
|
|
|
<ActionGroup>
|
2021-03-04 16:02:33 +00:00
|
|
|
<Button
|
|
|
|
isDisabled={!form.formState.isDirty}
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
data-testid="kerberos-save"
|
|
|
|
>
|
2021-01-22 20:15:32 +00:00
|
|
|
{t("common:save")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
2022-08-16 13:09:14 +00:00
|
|
|
onClick={() => navigate(toUserFederation({ realm }))}
|
2021-02-19 21:22:05 +00:00
|
|
|
data-testid="kerberos-cancel"
|
2021-01-22 20:15:32 +00:00
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</Form>
|
2020-11-09 12:31:05 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|