2021-05-18 08:34:16 +00:00
|
|
|
import React from "react";
|
2021-01-26 01:41:14 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
2021-02-08 20:50:03 +00:00
|
|
|
ButtonVariant,
|
|
|
|
DropdownItem,
|
|
|
|
DropdownSeparator,
|
2021-01-26 01:41:14 +00:00
|
|
|
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";
|
|
|
|
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-26 01:41:14 +00:00
|
|
|
|
2021-02-08 20:50:03 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
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";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { useHistory, useParams } from "react-router-dom";
|
|
|
|
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";
|
2021-03-16 14:24:32 +00:00
|
|
|
|
2021-05-14 19:44:45 +00:00
|
|
|
type ldapComponentRepresentation = ComponentRepresentation & {
|
|
|
|
config?: {
|
|
|
|
periodicChangedUsersSync?: boolean;
|
|
|
|
periodicFullSync?: boolean;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-08 20:50:03 +00:00
|
|
|
type LdapSettingsHeaderProps = {
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
value: string;
|
|
|
|
save: () => void;
|
|
|
|
toggleDeleteDialog: () => void;
|
|
|
|
toggleRemoveUsersDialog: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
const LdapSettingsHeader = ({
|
|
|
|
onChange,
|
|
|
|
value,
|
|
|
|
save,
|
|
|
|
toggleDeleteDialog,
|
|
|
|
toggleRemoveUsersDialog,
|
|
|
|
}: LdapSettingsHeaderProps) => {
|
|
|
|
const { t } = useTranslation("user-federation");
|
2021-02-19 23:13:07 +00:00
|
|
|
const { id } = useParams<{ id: string }>();
|
2021-03-09 19:55:08 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-02-08 20:50:03 +00:00
|
|
|
const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "user-federation:userFedDisableConfirmTitle",
|
|
|
|
messageKey: "user-federation:userFedDisableConfirm",
|
|
|
|
continueButtonLabel: "common:disable",
|
|
|
|
onConfirm: () => {
|
|
|
|
onChange("false");
|
|
|
|
save();
|
|
|
|
},
|
|
|
|
});
|
2021-03-09 19:55:08 +00:00
|
|
|
|
|
|
|
const syncChangedUsers = async () => {
|
|
|
|
try {
|
|
|
|
if (id) {
|
|
|
|
const response = await adminClient.userStorageProvider.sync({
|
|
|
|
id: id,
|
|
|
|
action: "triggerChangedUsersSync",
|
|
|
|
});
|
|
|
|
if (response.ignored) {
|
2021-03-10 16:06:53 +00:00
|
|
|
addAlert(`${response.status}.`, AlertVariant.warning);
|
2021-03-09 19:55:08 +00:00
|
|
|
} else {
|
|
|
|
addAlert(
|
|
|
|
t("syncUsersSuccess") +
|
2021-03-10 16:06:53 +00:00
|
|
|
`${response.added} users added, ${response.updated} users updated, ${response.removed} users removed, ${response.failed} users failed.`,
|
2021-03-09 19:55:08 +00:00
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("user-federation:syncUsersError", error);
|
2021-03-09 19:55:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const syncAllUsers = async () => {
|
|
|
|
try {
|
|
|
|
if (id) {
|
|
|
|
const response = await adminClient.userStorageProvider.sync({
|
|
|
|
id: id,
|
|
|
|
action: "triggerFullSync",
|
|
|
|
});
|
|
|
|
if (response.ignored) {
|
2021-03-10 16:06:53 +00:00
|
|
|
addAlert(`${response.status}.`, AlertVariant.warning);
|
2021-03-09 19:55:08 +00:00
|
|
|
} else {
|
|
|
|
addAlert(
|
|
|
|
t("syncUsersSuccess") +
|
2021-03-10 16:06:53 +00:00
|
|
|
`${response.added} users added, ${response.updated} users updated, ${response.removed} users removed, ${response.failed} users failed.`,
|
2021-03-09 19:55:08 +00:00
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("user-federation:syncUsersError", error);
|
2021-03-09 19:55:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const unlinkUsers = async () => {
|
|
|
|
try {
|
|
|
|
if (id) {
|
|
|
|
await adminClient.userStorageProvider.unlinkUsers({ id });
|
|
|
|
}
|
|
|
|
addAlert(t("unlinkUsersSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("user-federation:unlinkUsersError", error);
|
2021-03-09 19:55:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-08 20:50:03 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DisableConfirm />
|
2021-04-16 20:28:52 +00:00
|
|
|
{!id ? (
|
2021-06-09 20:13:56 +00:00
|
|
|
<ViewHeader titleKey={t("addOneLdap")} />
|
2021-02-19 23:13:07 +00:00
|
|
|
) : (
|
|
|
|
<ViewHeader
|
|
|
|
titleKey="LDAP"
|
|
|
|
dropdownItems={[
|
2021-03-10 15:42:31 +00:00
|
|
|
<DropdownItem key="sync" onClick={syncChangedUsers}>
|
2021-02-19 23:13:07 +00:00
|
|
|
{t("syncChangedUsers")}
|
|
|
|
</DropdownItem>,
|
2021-03-10 15:42:31 +00:00
|
|
|
<DropdownItem key="syncall" onClick={syncAllUsers}>
|
2021-02-19 23:13:07 +00:00
|
|
|
{t("syncAllUsers")}
|
|
|
|
</DropdownItem>,
|
2021-03-10 15:42:31 +00:00
|
|
|
<DropdownItem key="unlink" onClick={unlinkUsers}>
|
2021-02-19 23:13:07 +00:00
|
|
|
{t("unlinkUsers")}
|
|
|
|
</DropdownItem>,
|
2021-03-10 15:42:31 +00:00
|
|
|
<DropdownItem key="remove" onClick={toggleRemoveUsersDialog}>
|
2021-02-19 23:13:07 +00:00
|
|
|
{t("removeImported")}
|
|
|
|
</DropdownItem>,
|
|
|
|
<DropdownSeparator key="separator" />,
|
2021-02-23 20:49:57 +00:00
|
|
|
<DropdownItem
|
|
|
|
key="delete"
|
2021-03-10 15:42:31 +00:00
|
|
|
onClick={toggleDeleteDialog}
|
2021-02-23 20:49:57 +00:00
|
|
|
data-testid="delete-ldap-cmd"
|
|
|
|
>
|
2021-02-19 23:13:07 +00:00
|
|
|
{t("deleteProvider")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
isEnabled={value === "true"}
|
|
|
|
onToggle={(value) => {
|
|
|
|
if (!value) {
|
|
|
|
toggleDisableDialog();
|
|
|
|
} else {
|
|
|
|
onChange("" + value);
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
2021-02-08 20:50:03 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2020-10-30 20:15:37 +00:00
|
|
|
|
2020-12-16 07:02:41 +00:00
|
|
|
export const 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" });
|
2021-01-26 01:41:14 +00:00
|
|
|
const history = useHistory();
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const { realm } = useRealm();
|
|
|
|
|
|
|
|
const { id } = useParams<{ id: string }>();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
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-01-26 01:41:14 +00:00
|
|
|
|
|
|
|
const setupForm = (component: ComponentRepresentation) => {
|
|
|
|
Object.entries(component).map((entry) => {
|
|
|
|
if (entry[0] === "config") {
|
2021-05-13 21:45:11 +00:00
|
|
|
form.setValue(
|
|
|
|
"config.periodicChangedUsersSync",
|
|
|
|
entry[1].changedSyncPeriod[0] !== "-1"
|
|
|
|
);
|
|
|
|
|
|
|
|
form.setValue(
|
|
|
|
"config.periodicFullSync",
|
|
|
|
entry[1].fullSyncPeriod[0] !== "-1"
|
|
|
|
);
|
|
|
|
|
2021-01-26 01:41:14 +00:00
|
|
|
convertToFormValues(entry[1], "config", form.setValue);
|
|
|
|
}
|
2021-05-07 13:15:14 +00:00
|
|
|
form.setValue(entry[0], entry[1]);
|
2021-01-26 01:41:14 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-03-09 19:55:08 +00:00
|
|
|
const removeImportedUsers = async () => {
|
|
|
|
try {
|
|
|
|
if (id) {
|
|
|
|
await adminClient.userStorageProvider.removeImportedUsers({ id });
|
|
|
|
}
|
|
|
|
addAlert(t("removeImportedUsersSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("user-federation:removeImportedUsersError", error);
|
2021-03-09 19:55:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-14 19:44:45 +00:00
|
|
|
const save = async (component: ldapComponentRepresentation) => {
|
2021-05-13 21:45:11 +00:00
|
|
|
if (component?.config?.periodicChangedUsersSync !== null) {
|
|
|
|
if (component?.config?.periodicChangedUsersSync === false) {
|
|
|
|
component.config.changedSyncPeriod = ["-1"];
|
|
|
|
}
|
|
|
|
delete component?.config?.periodicChangedUsersSync;
|
|
|
|
}
|
|
|
|
if (component?.config?.periodicFullSync !== null) {
|
|
|
|
if (component?.config?.periodicFullSync === false) {
|
|
|
|
component.config.fullSyncPeriod = ["-1"];
|
|
|
|
}
|
|
|
|
delete component?.config?.periodicFullSync;
|
|
|
|
}
|
2021-01-26 01:41:14 +00:00
|
|
|
try {
|
2021-04-16 05:35:51 +00:00
|
|
|
if (!id) {
|
|
|
|
await adminClient.components.create(component);
|
|
|
|
history.push(`/${realm}/user-federation`);
|
|
|
|
} 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-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
|
|
|
|
2021-02-08 20:50:03 +00:00
|
|
|
const [toggleRemoveUsersDialog, RemoveUsersConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("removeImportedUsers"),
|
|
|
|
messageKey: t("removeImportedUsersMessage"),
|
|
|
|
continueButtonLabel: "common:remove",
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
2021-03-09 19:55:08 +00:00
|
|
|
removeImportedUsers();
|
2021-02-08 20:50:03 +00:00
|
|
|
addAlert(t("removeImportedUsersSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("user-federation:removeImportedUsersError", error);
|
2021-02-08 20:50:03 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "user-federation:userFedDeleteConfirmTitle",
|
|
|
|
messageKey: "user-federation:userFedDeleteConfirm",
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.components.del({ id });
|
|
|
|
addAlert(t("userFedDeletedSuccess"), AlertVariant.success);
|
|
|
|
history.replace(`/${realm}/user-federation`);
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("user-federation:userFedDeleteError", error);
|
2021-02-08 20:50:03 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-06-09 20:13:56 +00:00
|
|
|
const addLdapFormContent = () => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ScrollForm
|
|
|
|
sections={[
|
|
|
|
t("generalOptions"),
|
|
|
|
t("connectionAndAuthenticationSettings"),
|
|
|
|
t("ldapSearchingAndUpdatingSettings"),
|
|
|
|
t("synchronizationSettings"),
|
|
|
|
t("kerberosIntegration"),
|
|
|
|
t("cacheSettings"),
|
|
|
|
t("advancedSettings"),
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<LdapSettingsGeneral form={form} />
|
2021-08-17 12:58:07 +00:00
|
|
|
<LdapSettingsConnection form={form} edit={!!id} />
|
2021-06-09 20:13:56 +00:00
|
|
|
<LdapSettingsSearching form={form} />
|
|
|
|
<LdapSettingsSynchronization form={form} />
|
|
|
|
<LdapSettingsKerberosIntegration form={form} />
|
|
|
|
<SettingsCache form={form} />
|
|
|
|
<LdapSettingsAdvanced form={form} />
|
|
|
|
</ScrollForm>
|
|
|
|
<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"
|
|
|
|
onClick={() => history.push(`/${realm}/user-federation`)}
|
|
|
|
data-testid="ldap-cancel"
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</Form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-10-30 20:15:37 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-02-08 20:50:03 +00:00
|
|
|
<DeleteConfirm />
|
|
|
|
<RemoveUsersConfirm />
|
|
|
|
<Controller
|
|
|
|
name="config.enabled[0]"
|
2021-02-19 23:13:07 +00:00
|
|
|
defaultValue={["true"][0]}
|
2021-02-08 20:50:03 +00:00
|
|
|
control={form.control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<LdapSettingsHeader
|
|
|
|
value={value}
|
|
|
|
save={() => save(form.getValues())}
|
|
|
|
onChange={onChange}
|
|
|
|
toggleDeleteDialog={toggleDeleteDialog}
|
|
|
|
toggleRemoveUsersDialog={toggleRemoveUsersDialog}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2020-10-30 20:15:37 +00:00
|
|
|
<PageSection variant="light" isFilled>
|
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
|
|
|
>
|
2021-06-09 20:13:56 +00:00
|
|
|
{addLdapFormContent()}
|
|
|
|
</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
|
|
|
>
|
|
|
|
<LdapMapperList />
|
|
|
|
</Tab>
|
2021-06-09 20:13:56 +00:00
|
|
|
</KeycloakTabs>
|
|
|
|
) : (
|
|
|
|
addLdapFormContent()
|
|
|
|
)}
|
2020-10-30 20:15:37 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|