2021-08-31 08:13:25 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
ValidatedOptions,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
|
|
|
import type GroupRepresentation from "@keycloak/keycloak-admin-client/lib/defs/groupRepresentation";
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2022-02-02 10:33:57 +00:00
|
|
|
import { capitalize } from "lodash-es";
|
2021-08-31 08:13:25 +00:00
|
|
|
import { useParams } from "react-router-dom";
|
|
|
|
import type FederatedIdentityRepresentation from "@keycloak/keycloak-admin-client/lib/defs/federatedIdentityRepresentation";
|
|
|
|
import type { UserParams } from "./routes/User";
|
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 = {
|
|
|
|
federatedId?: string;
|
|
|
|
handleModalToggle: () => void;
|
|
|
|
refresh: (group?: GroupRepresentation) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const UserIdpModal = ({
|
|
|
|
federatedId,
|
|
|
|
handleModalToggle,
|
|
|
|
refresh,
|
|
|
|
}: UserIdpModalProps) => {
|
|
|
|
const { t } = useTranslation("users");
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
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 },
|
|
|
|
} = useForm({
|
2021-08-31 08:13:25 +00:00
|
|
|
mode: "onChange",
|
|
|
|
});
|
|
|
|
|
|
|
|
const { id } = useParams<UserParams>();
|
|
|
|
|
|
|
|
const submitForm = async (fedIdentity: FederatedIdentityRepresentation) => {
|
|
|
|
try {
|
|
|
|
await adminClient.users.addToFederatedIdentity({
|
|
|
|
id: id!,
|
|
|
|
federatedIdentityId: federatedId!,
|
|
|
|
federatedIdentity: fedIdentity,
|
|
|
|
});
|
|
|
|
addAlert(t("users:idpLinkSuccess"), AlertVariant.success);
|
|
|
|
handleModalToggle();
|
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
|
|
|
addError("users:couldNotLinkIdP", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.small}
|
|
|
|
title={t("users:linkAccountTitle", {
|
2022-02-02 10:33:57 +00:00
|
|
|
provider: capitalize(federatedId),
|
2021-08-31 08:13:25 +00:00
|
|
|
})}
|
|
|
|
isOpen={true}
|
|
|
|
onClose={handleModalToggle}
|
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
data-testid={t("link")}
|
|
|
|
key="confirm"
|
|
|
|
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
|
|
|
|
id="modal-cancel"
|
2022-02-02 11:44:52 +00:00
|
|
|
data-testid="cancel"
|
2021-08-31 08:13:25 +00:00
|
|
|
key="cancel"
|
|
|
|
variant={ButtonVariant.link}
|
|
|
|
onClick={() => {
|
|
|
|
handleModalToggle();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Form id="group-form" onSubmit={handleSubmit(submitForm)}>
|
|
|
|
<FormGroup
|
|
|
|
name="idp-name-group"
|
|
|
|
label={t("users:identityProvider")}
|
|
|
|
fieldId="idp-name"
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
validated={
|
|
|
|
errors.identityProvider
|
|
|
|
? ValidatedOptions.error
|
|
|
|
: ValidatedOptions.default
|
|
|
|
}
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2021-08-31 08:13:25 +00:00
|
|
|
data-testid="idpNameInput"
|
|
|
|
aria-label="Identity provider name input"
|
|
|
|
ref={register({ required: true })}
|
|
|
|
autoFocus
|
|
|
|
isReadOnly
|
|
|
|
type="text"
|
|
|
|
id="link-idp-name"
|
|
|
|
name="identityProvider"
|
2022-02-02 10:33:57 +00:00
|
|
|
value={capitalize(federatedId)}
|
2021-08-31 08:13:25 +00:00
|
|
|
validated={
|
|
|
|
errors.identityProvider
|
|
|
|
? ValidatedOptions.error
|
|
|
|
: ValidatedOptions.default
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
name="user-id-group"
|
|
|
|
label={t("users:userID")}
|
|
|
|
fieldId="user-id"
|
|
|
|
helperText={t("users-help:userIdHelperText")}
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
validated={
|
|
|
|
errors.userId ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
isRequired
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2021-08-31 08:13:25 +00:00
|
|
|
data-testid="userIdInput"
|
|
|
|
aria-label="user ID input"
|
|
|
|
ref={register({ required: true })}
|
|
|
|
autoFocus
|
|
|
|
type="text"
|
|
|
|
id="link-idp-user-id"
|
|
|
|
name="userId"
|
|
|
|
validated={
|
|
|
|
errors.userId ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
name="username-group"
|
|
|
|
label={t("users:username")}
|
|
|
|
fieldId="username"
|
|
|
|
helperText={t("users-help:usernameHelperText")}
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
validated={
|
|
|
|
errors.name ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
isRequired
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2021-08-31 08:13:25 +00:00
|
|
|
data-testid="usernameInput"
|
|
|
|
aria-label="username input"
|
|
|
|
ref={register({ required: true })}
|
|
|
|
autoFocus
|
|
|
|
type="text"
|
|
|
|
id="link-idp-username"
|
|
|
|
name="userName"
|
|
|
|
validated={
|
|
|
|
errors.name ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|