2021-11-09 14:14:54 +00:00
|
|
|
import React from "react";
|
2021-01-22 20:15:32 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
2021-01-20 15:43:52 +00:00
|
|
|
ButtonVariant,
|
|
|
|
DropdownItem,
|
2021-01-22 20:15:32 +00:00
|
|
|
Form,
|
|
|
|
PageSection,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
2020-12-16 07:02:41 +00:00
|
|
|
import { KerberosSettingsRequired } from "./kerberos/KerberosSettingsRequired";
|
2021-02-11 20:51:51 +00:00
|
|
|
import { SettingsCache } from "./shared/SettingsCache";
|
2021-01-22 20:15:32 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
|
|
|
import { convertToFormValues } from "../util";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation";
|
2021-01-20 15:43:52 +00:00
|
|
|
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
2021-11-09 14:14:54 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
2021-01-20 15:43:52 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { useHistory, useParams } from "react-router-dom";
|
|
|
|
|
|
|
|
type KerberosSettingsHeaderProps = {
|
2021-02-08 20:50:03 +00:00
|
|
|
onChange: (value: string) => void;
|
|
|
|
value: string;
|
|
|
|
save: () => void;
|
2021-01-20 15:43:52 +00:00
|
|
|
toggleDeleteDialog: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
const KerberosSettingsHeader = ({
|
|
|
|
onChange,
|
|
|
|
value,
|
2021-02-08 20:50:03 +00:00
|
|
|
save,
|
2021-01-20 15:43:52 +00:00
|
|
|
toggleDeleteDialog,
|
|
|
|
}: KerberosSettingsHeaderProps) => {
|
|
|
|
const { t } = useTranslation("user-federation");
|
2021-11-09 14:14:54 +00:00
|
|
|
const { id } = useParams<{ id?: string }>();
|
2021-01-20 15:43:52 +00:00
|
|
|
const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "user-federation:userFedDisableConfirmTitle",
|
|
|
|
messageKey: "user-federation:userFedDisableConfirm",
|
|
|
|
continueButtonLabel: "common:disable",
|
|
|
|
onConfirm: () => {
|
2021-02-08 20:50:03 +00:00
|
|
|
onChange("false");
|
|
|
|
save();
|
2021-01-20 15:43:52 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DisableConfirm />
|
2021-11-09 14:14:54 +00:00
|
|
|
{!id ? (
|
2021-04-21 13:18:45 +00:00
|
|
|
<ViewHeader titleKey="Kerberos" />
|
2021-02-10 21:54:21 +00:00
|
|
|
) : (
|
|
|
|
<ViewHeader
|
|
|
|
titleKey="Kerberos"
|
|
|
|
dropdownItems={[
|
2021-02-16 19:42:30 +00:00
|
|
|
<DropdownItem
|
|
|
|
key="delete"
|
|
|
|
onClick={() => toggleDeleteDialog()}
|
2021-02-23 20:49:57 +00:00
|
|
|
data-testid="delete-kerberos-cmd"
|
2021-02-16 19:42:30 +00:00
|
|
|
>
|
2021-02-10 21:54:21 +00:00
|
|
|
{t("deleteProvider")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
isEnabled={value === "true"}
|
|
|
|
onToggle={(value) => {
|
|
|
|
if (!value) {
|
|
|
|
toggleDisableDialog();
|
|
|
|
} else {
|
2021-11-09 14:14:54 +00:00
|
|
|
onChange(value.toString());
|
2021-02-10 21:54:21 +00:00
|
|
|
save();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
2021-01-20 15:43:52 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
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" });
|
|
|
|
const history = useHistory();
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
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) => {
|
|
|
|
Object.entries(component).map((entry) => {
|
|
|
|
form.setValue(
|
|
|
|
"config.allowPasswordAuthentication",
|
|
|
|
component.config?.allowPasswordAuthentication
|
|
|
|
);
|
|
|
|
if (entry[0] === "config") {
|
|
|
|
convertToFormValues(entry[1], "config", form.setValue);
|
|
|
|
}
|
|
|
|
form.setValue(entry[0], entry[1]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const save = async (component: ComponentRepresentation) => {
|
|
|
|
try {
|
2021-11-09 14:14:54 +00:00
|
|
|
if (!id) {
|
|
|
|
await adminClient.components.create(component);
|
|
|
|
history.push(`/${realm}/user-federation`);
|
|
|
|
} 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
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "user-federation:userFedDeleteConfirmTitle",
|
|
|
|
messageKey: "user-federation:userFedDeleteConfirm",
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
2021-11-09 14:14:54 +00:00
|
|
|
await adminClient.components.del({ id: id! });
|
2021-01-20 15:43:52 +00:00
|
|
|
addAlert(t("userFedDeletedSuccess"), AlertVariant.success);
|
2021-01-27 22:09:59 +00:00
|
|
|
history.replace(`/${realm}/user-federation`);
|
2021-09-06 07:48:53 +00:00
|
|
|
} catch (error: any) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addAlert("user-federation:userFedDeleteError", error);
|
2021-01-20 15:43:52 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2021-01-22 20:15:32 +00:00
|
|
|
|
2020-11-09 12:31:05 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-01-20 15:43:52 +00:00
|
|
|
<DeleteConfirm />
|
|
|
|
<Controller
|
2021-01-25 16:46:07 +00:00
|
|
|
name="config.enabled[0]"
|
2021-02-10 21:54:21 +00:00
|
|
|
defaultValue={["true"][0]}
|
2021-01-20 15:43:52 +00:00
|
|
|
control={form.control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<KerberosSettingsHeader
|
|
|
|
value={value}
|
2021-02-08 20:50:03 +00:00
|
|
|
onChange={onChange}
|
|
|
|
save={() => save(form.getValues())}
|
2021-01-20 15:43:52 +00:00
|
|
|
toggleDeleteDialog={toggleDeleteDialog}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
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"
|
|
|
|
onClick={() => history.push(`/${realm}/user-federation`)}
|
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
|
|
|
}
|