2022-11-15 15:21:43 +00:00
|
|
|
import type FederatedIdentityRepresentation from "@keycloak/keycloak-admin-client/lib/defs/federatedIdentityRepresentation";
|
2021-08-31 08:13:25 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
ValidatedOptions,
|
|
|
|
} from "@patternfly/react-core";
|
2022-11-15 15:21:43 +00:00
|
|
|
import { capitalize } from "lodash-es";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2021-08-31 08:13:25 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../admin-client";
|
2021-08-31 08:13:25 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2022-04-21 15:03:26 +00:00
|
|
|
import { KeycloakTextInput } from "../components/keycloak-text-input/KeycloakTextInput";
|
2021-08-31 08:13:25 +00:00
|
|
|
|
|
|
|
type UserIdpModalProps = {
|
2022-11-15 15:21:43 +00:00
|
|
|
userId: string;
|
|
|
|
federatedId: string;
|
|
|
|
onClose: () => void;
|
|
|
|
onRefresh: () => void;
|
2021-08-31 08:13:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const UserIdpModal = ({
|
2022-11-15 15:21:43 +00:00
|
|
|
userId,
|
2021-08-31 08:13:25 +00:00
|
|
|
federatedId,
|
2022-11-15 15:21:43 +00:00
|
|
|
onClose,
|
|
|
|
onRefresh,
|
2021-08-31 08:13:25 +00:00
|
|
|
}: UserIdpModalProps) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2021-08-31 08:13:25 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2022-04-08 12:37:31 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
formState: { isValid, errors },
|
2022-11-15 15:21:43 +00:00
|
|
|
} = useForm<FederatedIdentityRepresentation>({
|
2021-08-31 08:13:25 +00:00
|
|
|
mode: "onChange",
|
|
|
|
});
|
|
|
|
|
2022-11-15 15:21:43 +00:00
|
|
|
const onSubmit = async (
|
2023-07-11 14:03:21 +00:00
|
|
|
federatedIdentity: FederatedIdentityRepresentation,
|
2022-11-15 15:21:43 +00:00
|
|
|
) => {
|
2021-08-31 08:13:25 +00:00
|
|
|
try {
|
|
|
|
await adminClient.users.addToFederatedIdentity({
|
2022-11-15 15:21:43 +00:00
|
|
|
id: userId,
|
|
|
|
federatedIdentityId: federatedId,
|
|
|
|
federatedIdentity,
|
2021-08-31 08:13:25 +00:00
|
|
|
});
|
2023-09-25 07:06:56 +00:00
|
|
|
addAlert(t("idpLinkSuccess"), AlertVariant.success);
|
2022-11-15 15:21:43 +00:00
|
|
|
onClose();
|
|
|
|
onRefresh();
|
2021-08-31 08:13:25 +00:00
|
|
|
} catch (error) {
|
2023-09-25 07:06:56 +00:00
|
|
|
addError("couldNotLinkIdP", error);
|
2021-08-31 08:13:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.small}
|
2023-09-25 07:06:56 +00:00
|
|
|
title={t("linkAccountTitle", {
|
2022-02-02 10:33:57 +00:00
|
|
|
provider: capitalize(federatedId),
|
2021-08-31 08:13:25 +00:00
|
|
|
})}
|
2022-11-15 15:21:43 +00:00
|
|
|
onClose={onClose}
|
2021-08-31 08:13:25 +00:00
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
key="confirm"
|
2022-11-15 15:21:43 +00:00
|
|
|
data-testid="confirm"
|
2021-08-31 08:13:25 +00:00
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
form="group-form"
|
2022-04-08 12:37:31 +00:00
|
|
|
isDisabled={!isValid}
|
2021-08-31 08:13:25 +00:00
|
|
|
>
|
|
|
|
{t("link")}
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
key="cancel"
|
2022-11-15 15:21:43 +00:00
|
|
|
data-testid="cancel"
|
2021-08-31 08:13:25 +00:00
|
|
|
variant={ButtonVariant.link}
|
2022-11-15 15:21:43 +00:00
|
|
|
onClick={onClose}
|
2021-08-31 08:13:25 +00:00
|
|
|
>
|
2023-09-14 09:01:15 +00:00
|
|
|
{t("cancel")}
|
2021-08-31 08:13:25 +00:00
|
|
|
</Button>,
|
|
|
|
]}
|
2022-11-15 15:21:43 +00:00
|
|
|
isOpen
|
2021-08-31 08:13:25 +00:00
|
|
|
>
|
2022-11-15 15:21:43 +00:00
|
|
|
<Form id="group-form" onSubmit={handleSubmit(onSubmit)}>
|
2023-09-25 07:06:56 +00:00
|
|
|
<FormGroup label={t("identityProvider")} fieldId="identityProvider">
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2022-11-15 15:21:43 +00:00
|
|
|
id="identityProvider"
|
2021-08-31 08:13:25 +00:00
|
|
|
data-testid="idpNameInput"
|
2022-02-02 10:33:57 +00:00
|
|
|
value={capitalize(federatedId)}
|
2022-11-15 15:21:43 +00:00
|
|
|
isReadOnly
|
2021-08-31 08:13:25 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
2023-09-25 07:06:56 +00:00
|
|
|
label={t("userID")}
|
2022-11-15 15:21:43 +00:00
|
|
|
fieldId="userID"
|
2023-09-25 07:06:56 +00:00
|
|
|
helperText={t("userIdHelperText")}
|
2023-09-14 09:01:15 +00:00
|
|
|
helperTextInvalid={t("required")}
|
2021-08-31 08:13:25 +00:00
|
|
|
validated={
|
|
|
|
errors.userId ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
isRequired
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2022-11-15 15:21:43 +00:00
|
|
|
id="userID"
|
2021-08-31 08:13:25 +00:00
|
|
|
data-testid="userIdInput"
|
|
|
|
validated={
|
|
|
|
errors.userId ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
2022-11-15 15:21:43 +00:00
|
|
|
autoFocus
|
|
|
|
{...register("userId", { required: true })}
|
2021-08-31 08:13:25 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
2023-09-25 07:06:56 +00:00
|
|
|
label={t("username")}
|
2021-08-31 08:13:25 +00:00
|
|
|
fieldId="username"
|
2023-09-25 07:06:56 +00:00
|
|
|
helperText={t("usernameHelperText")}
|
2023-09-14 09:01:15 +00:00
|
|
|
helperTextInvalid={t("required")}
|
2021-08-31 08:13:25 +00:00
|
|
|
validated={
|
2022-11-15 15:21:43 +00:00
|
|
|
errors.userName ? ValidatedOptions.error : ValidatedOptions.default
|
2021-08-31 08:13:25 +00:00
|
|
|
}
|
|
|
|
isRequired
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2022-11-15 15:21:43 +00:00
|
|
|
id="username"
|
2021-08-31 08:13:25 +00:00
|
|
|
data-testid="usernameInput"
|
|
|
|
validated={
|
2022-11-15 15:21:43 +00:00
|
|
|
errors.userName
|
|
|
|
? ValidatedOptions.error
|
|
|
|
: ValidatedOptions.default
|
2021-08-31 08:13:25 +00:00
|
|
|
}
|
2022-11-15 15:21:43 +00:00
|
|
|
{...register("userName", { required: true })}
|
2021-08-31 08:13:25 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|