2022-10-24 11:43:46 +00:00
|
|
|
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
|
2021-05-03 20:00:12 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
2022-10-24 11:43:46 +00:00
|
|
|
ActionListItem,
|
|
|
|
Alert,
|
|
|
|
AlertActionLink,
|
2021-05-27 07:01:55 +00:00
|
|
|
AlertVariant,
|
2021-05-03 20:00:12 +00:00
|
|
|
Button,
|
|
|
|
Checkbox,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Switch,
|
|
|
|
} from "@patternfly/react-core";
|
2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { Controller, useForm, useWatch } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2022-10-24 11:43:46 +00:00
|
|
|
import { Link } from "react-router-dom-v5-compat";
|
|
|
|
|
2021-07-21 09:30:18 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-05-03 20:00:12 +00:00
|
|
|
import { FormAccess } from "../components/form-access/FormAccess";
|
|
|
|
import { HelpItem } from "../components/help-enabler/HelpItem";
|
2022-04-21 15:03:26 +00:00
|
|
|
import { KeycloakTextInput } from "../components/keycloak-text-input/KeycloakTextInput";
|
2022-10-24 11:43:46 +00:00
|
|
|
import { PasswordInput } from "../components/password-input/PasswordInput";
|
|
|
|
import { FormPanel } from "../components/scroll-form/FormPanel";
|
2021-05-27 07:01:55 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2022-10-24 11:43:46 +00:00
|
|
|
import { toUser } from "../user/routes/User";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { emailRegexPattern } from "../util";
|
2022-10-24 11:43:46 +00:00
|
|
|
import { useCurrentUser } from "../utils/useCurrentUser";
|
|
|
|
|
2022-02-16 11:39:08 +00:00
|
|
|
import "./realm-settings-section.css";
|
2022-10-26 09:45:32 +00:00
|
|
|
import useToggle from "../utils/useToggle";
|
2021-05-03 20:00:12 +00:00
|
|
|
|
|
|
|
type RealmSettingsEmailTabProps = {
|
2021-05-27 07:01:55 +00:00
|
|
|
realm: RealmRepresentation;
|
2021-05-03 20:00:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const RealmSettingsEmailTab = ({
|
2021-05-27 07:01:55 +00:00
|
|
|
realm: initialRealm,
|
2021-05-03 20:00:12 +00:00
|
|
|
}: RealmSettingsEmailTabProps) => {
|
|
|
|
const { t } = useTranslation("realm-settings");
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-05-27 07:01:55 +00:00
|
|
|
const { realm: realmName } = useRealm();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2022-10-24 11:43:46 +00:00
|
|
|
const currentUser = useCurrentUser();
|
2021-05-27 07:01:55 +00:00
|
|
|
|
|
|
|
const [realm, setRealm] = useState(initialRealm);
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
control,
|
|
|
|
handleSubmit,
|
2021-06-22 14:17:45 +00:00
|
|
|
watch,
|
2021-05-27 07:01:55 +00:00
|
|
|
reset: resetForm,
|
2021-06-21 14:46:05 +00:00
|
|
|
getValues,
|
2022-04-08 12:37:31 +00:00
|
|
|
formState: { errors },
|
2021-09-02 20:14:43 +00:00
|
|
|
} = useForm<RealmRepresentation>({ defaultValues: realm });
|
2021-05-27 07:01:55 +00:00
|
|
|
|
2021-10-14 21:08:42 +00:00
|
|
|
const reset = () => resetForm(realm);
|
2021-06-22 14:17:45 +00:00
|
|
|
const watchFromValue = watch("smtpServer.from", "");
|
|
|
|
const watchHostValue = watch("smtpServer.host", "");
|
2022-10-26 09:45:32 +00:00
|
|
|
const [isTesting, toggleTest] = useToggle();
|
2021-06-10 20:00:14 +00:00
|
|
|
|
2021-06-22 15:19:13 +00:00
|
|
|
const authenticationEnabled = useWatch({
|
|
|
|
control,
|
2022-05-30 10:20:17 +00:00
|
|
|
name: "smtpServer.auth",
|
2021-10-06 20:54:33 +00:00
|
|
|
defaultValue: "",
|
2021-06-22 15:19:13 +00:00
|
|
|
});
|
|
|
|
|
2021-05-27 07:01:55 +00:00
|
|
|
const save = async (form: RealmRepresentation) => {
|
|
|
|
try {
|
|
|
|
const savedRealm = { ...realm, ...form };
|
2022-08-17 16:59:34 +00:00
|
|
|
|
|
|
|
// For default value, back end is expecting null instead of empty string
|
|
|
|
if (savedRealm.smtpServer?.port === "") savedRealm.smtpServer.port = null;
|
|
|
|
|
2021-05-27 07:01:55 +00:00
|
|
|
await adminClient.realms.update({ realm: realmName }, savedRealm);
|
|
|
|
setRealm(savedRealm);
|
|
|
|
addAlert(t("saveSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("realm-settings:saveError", error);
|
2021-05-27 07:01:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-10 20:00:14 +00:00
|
|
|
const testConnection = async () => {
|
2022-05-30 10:20:17 +00:00
|
|
|
const toNumber = (value: string) => Number(value);
|
|
|
|
const toBoolean = (value: string) => value === true.toString();
|
|
|
|
const valueMapper = new Map<string, (value: string) => unknown>([
|
|
|
|
["port", toNumber],
|
|
|
|
["ssl", toBoolean],
|
|
|
|
["starttls", toBoolean],
|
|
|
|
["auth", toBoolean],
|
|
|
|
]);
|
2021-10-06 20:54:33 +00:00
|
|
|
|
2022-05-30 10:20:17 +00:00
|
|
|
const serverSettings = { ...getValues()["smtpServer"] };
|
2021-10-06 20:54:33 +00:00
|
|
|
|
2022-05-30 10:20:17 +00:00
|
|
|
for (const [key, mapperFn] of valueMapper.entries()) {
|
|
|
|
serverSettings[key] = mapperFn(serverSettings[key]);
|
2021-10-06 20:54:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:59:34 +00:00
|
|
|
// For default value, back end is expecting null instead of 0
|
|
|
|
if (serverSettings.port === 0) serverSettings.port = null;
|
|
|
|
|
2021-06-28 06:02:35 +00:00
|
|
|
try {
|
2022-10-26 09:45:32 +00:00
|
|
|
toggleTest();
|
2021-06-28 06:02:35 +00:00
|
|
|
await adminClient.realms.testSMTPConnection(
|
|
|
|
{ realm: realm.realm! },
|
2021-10-06 20:54:33 +00:00
|
|
|
serverSettings
|
2021-06-28 06:02:35 +00:00
|
|
|
);
|
|
|
|
addAlert(t("testConnectionSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("realm-settings:testConnectionError", error);
|
2021-06-28 06:02:35 +00:00
|
|
|
}
|
2022-10-26 09:45:32 +00:00
|
|
|
toggleTest();
|
2021-06-10 20:00:14 +00:00
|
|
|
};
|
|
|
|
|
2021-05-03 20:00:12 +00:00
|
|
|
return (
|
2022-10-24 11:43:46 +00:00
|
|
|
<PageSection variant="light">
|
|
|
|
<FormPanel title={t("template")} className="kc-email-template">
|
|
|
|
<FormAccess
|
|
|
|
isHorizontal
|
|
|
|
role="manage-realm"
|
|
|
|
className="pf-u-mt-lg"
|
|
|
|
onSubmit={handleSubmit(save)}
|
|
|
|
>
|
|
|
|
<FormGroup
|
|
|
|
label={t("from")}
|
|
|
|
fieldId="kc-display-name"
|
|
|
|
isRequired
|
|
|
|
validated={errors.smtpServer?.from ? "error" : "default"}
|
|
|
|
helperTextInvalid={t("users:emailInvalid")}
|
2021-05-03 20:00:12 +00:00
|
|
|
>
|
2022-10-24 11:43:46 +00:00
|
|
|
<KeycloakTextInput
|
|
|
|
type="email"
|
|
|
|
id="kc-sender-email-address"
|
|
|
|
data-testid="sender-email-address"
|
|
|
|
name="smtpServer.from"
|
|
|
|
ref={register({
|
|
|
|
pattern: emailRegexPattern,
|
|
|
|
required: true,
|
|
|
|
})}
|
|
|
|
placeholder="Sender email address"
|
2021-05-27 07:01:55 +00:00
|
|
|
validated={errors.smtpServer?.from ? "error" : "default"}
|
2022-10-24 11:43:46 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("fromDisplayName")}
|
|
|
|
fieldId="kc-from-display-name"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:fromDisplayName"
|
|
|
|
fieldLabelId="realm-settings:authentication"
|
2021-05-03 20:00:12 +00:00
|
|
|
/>
|
2022-10-24 11:43:46 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
<KeycloakTextInput
|
|
|
|
type="text"
|
|
|
|
id="kc-from-display-name"
|
|
|
|
data-testid="from-display-name"
|
|
|
|
name="smtpServer.fromDisplayName"
|
|
|
|
ref={register}
|
|
|
|
placeholder="Display name for Sender email address"
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("replyTo")}
|
|
|
|
fieldId="kc-reply-to"
|
|
|
|
validated={errors.smtpServer?.replyTo ? "error" : "default"}
|
|
|
|
helperTextInvalid={t("users:emailInvalid")}
|
|
|
|
>
|
|
|
|
<KeycloakTextInput
|
|
|
|
type="email"
|
|
|
|
id="kc-reply-to"
|
|
|
|
name="smtpServer.replyTo"
|
|
|
|
ref={register({
|
|
|
|
pattern: emailRegexPattern,
|
|
|
|
})}
|
|
|
|
placeholder="Reply to email address"
|
2021-05-27 07:01:55 +00:00
|
|
|
validated={errors.smtpServer?.replyTo ? "error" : "default"}
|
2022-10-24 11:43:46 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("replyToDisplayName")}
|
|
|
|
fieldId="kc-reply-to-display-name"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:replyToDisplayName"
|
|
|
|
fieldLabelId="realm-settings:replyToDisplayName"
|
2021-05-03 20:00:12 +00:00
|
|
|
/>
|
2022-10-24 11:43:46 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
<KeycloakTextInput
|
|
|
|
type="text"
|
|
|
|
id="kc-reply-to-display-name"
|
|
|
|
name="smtpServer.replyToDisplayName"
|
|
|
|
ref={register}
|
|
|
|
placeholder='Display name for "reply to" email address'
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("envelopeFrom")}
|
|
|
|
fieldId="kc-envelope-from"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:envelopeFrom"
|
|
|
|
fieldLabelId="realm-settings:envelopeFrom"
|
2021-05-03 20:00:12 +00:00
|
|
|
/>
|
2022-10-24 11:43:46 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
<KeycloakTextInput
|
|
|
|
type="text"
|
|
|
|
id="kc-envelope-from"
|
|
|
|
name="smtpServer.envelopeFrom"
|
|
|
|
ref={register}
|
|
|
|
placeholder="Sender envelope email address"
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</FormPanel>
|
|
|
|
<FormPanel
|
|
|
|
className="kc-email-connection"
|
|
|
|
title={t("connectionAndAuthentication")}
|
|
|
|
>
|
|
|
|
<FormAccess
|
|
|
|
isHorizontal
|
|
|
|
role="manage-realm"
|
|
|
|
className="pf-u-mt-lg"
|
|
|
|
onSubmit={handleSubmit(save)}
|
2021-05-03 20:00:12 +00:00
|
|
|
>
|
2022-10-24 11:43:46 +00:00
|
|
|
<FormGroup
|
|
|
|
label={t("host")}
|
|
|
|
fieldId="kc-host"
|
|
|
|
isRequired
|
|
|
|
validated={errors.smtpServer?.host ? "error" : "default"}
|
|
|
|
helperTextInvalid={t("common:required")}
|
2021-05-03 20:00:12 +00:00
|
|
|
>
|
2022-10-24 11:43:46 +00:00
|
|
|
<KeycloakTextInput
|
|
|
|
type="text"
|
|
|
|
id="kc-host"
|
|
|
|
name="smtpServer.host"
|
|
|
|
ref={register({ required: true })}
|
|
|
|
placeholder="SMTP host"
|
2021-05-27 07:01:55 +00:00
|
|
|
validated={errors.smtpServer?.host ? "error" : "default"}
|
2022-10-24 11:43:46 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup label={t("port")} fieldId="kc-port">
|
|
|
|
<KeycloakTextInput
|
|
|
|
type="text"
|
|
|
|
id="kc-port"
|
|
|
|
name="smtpServer.port"
|
|
|
|
ref={register}
|
|
|
|
placeholder="SMTP port (defaults to 25)"
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup label={t("encryption")} fieldId="kc-html-display-name">
|
|
|
|
<Controller
|
|
|
|
name="smtpServer.ssl"
|
|
|
|
control={control}
|
|
|
|
defaultValue="false"
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Checkbox
|
|
|
|
id="kc-enable-ssl"
|
|
|
|
data-testid="enable-ssl"
|
|
|
|
label={t("enableSSL")}
|
|
|
|
ref={register}
|
|
|
|
isChecked={value === "true"}
|
|
|
|
onChange={(value) => onChange("" + value)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<Controller
|
|
|
|
name="smtpServer.starttls"
|
|
|
|
control={control}
|
|
|
|
defaultValue="false"
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Checkbox
|
|
|
|
id="kc-enable-start-tls"
|
|
|
|
data-testid="enable-start-tls"
|
|
|
|
label={t("enableStartTLS")}
|
|
|
|
ref={register}
|
|
|
|
isChecked={value === "true"}
|
|
|
|
onChange={(value) => onChange("" + value)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
hasNoPaddingTop
|
|
|
|
label={t("authentication")}
|
|
|
|
fieldId="kc-authentication"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="smtpServer.auth"
|
|
|
|
control={control}
|
|
|
|
defaultValue=""
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Switch
|
|
|
|
id="kc-authentication-switch"
|
|
|
|
data-testid="email-authentication-switch"
|
|
|
|
label={t("common:enabled")}
|
|
|
|
labelOff={t("common:disabled")}
|
|
|
|
isChecked={value === "true"}
|
|
|
|
onChange={(value) => {
|
|
|
|
onChange("" + value);
|
|
|
|
}}
|
|
|
|
aria-label={t("authentication")}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
{authenticationEnabled === "true" && (
|
|
|
|
<>
|
|
|
|
<FormGroup
|
|
|
|
label={t("username")}
|
|
|
|
fieldId="kc-username"
|
|
|
|
isRequired
|
|
|
|
validated={errors.smtpServer?.user ? "error" : "default"}
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
>
|
|
|
|
<KeycloakTextInput
|
|
|
|
type="text"
|
|
|
|
id="kc-username"
|
|
|
|
data-testid="username-input"
|
|
|
|
name="smtpServer.user"
|
|
|
|
ref={register({ required: true })}
|
|
|
|
placeholder="Login username"
|
2021-05-27 07:01:55 +00:00
|
|
|
validated={errors.smtpServer?.user ? "error" : "default"}
|
2022-10-24 11:43:46 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("password")}
|
|
|
|
fieldId="kc-username"
|
|
|
|
isRequired
|
|
|
|
validated={errors.smtpServer?.password ? "error" : "default"}
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:password"
|
|
|
|
fieldLabelId="realm-settings:password"
|
2021-05-03 20:00:12 +00:00
|
|
|
/>
|
2022-10-24 11:43:46 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
<PasswordInput
|
|
|
|
id="kc-password"
|
|
|
|
data-testid="password-input"
|
|
|
|
name="smtpServer.password"
|
|
|
|
aria-label={t("password")}
|
2021-05-27 07:01:55 +00:00
|
|
|
validated={errors.smtpServer?.password ? "error" : "default"}
|
2022-10-24 11:43:46 +00:00
|
|
|
ref={register({ required: true })}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</>
|
|
|
|
)}
|
2021-05-03 20:00:12 +00:00
|
|
|
|
2022-10-24 11:43:46 +00:00
|
|
|
<ActionGroup>
|
|
|
|
<ActionListItem>
|
2021-05-03 20:00:12 +00:00
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
data-testid="email-tab-save"
|
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>
|
2022-10-24 11:43:46 +00:00
|
|
|
</ActionListItem>
|
|
|
|
<ActionListItem>
|
2021-06-10 20:00:14 +00:00
|
|
|
<Button
|
|
|
|
variant="secondary"
|
2021-10-14 21:08:42 +00:00
|
|
|
onClick={() => testConnection()}
|
2021-06-10 20:00:14 +00:00
|
|
|
data-testid="test-connection-button"
|
2021-06-22 14:17:45 +00:00
|
|
|
isDisabled={
|
2022-10-24 11:43:46 +00:00
|
|
|
!(emailRegexPattern.test(watchFromValue) && watchHostValue) ||
|
|
|
|
!currentUser?.email
|
2021-06-22 14:17:45 +00:00
|
|
|
}
|
2022-10-24 11:43:46 +00:00
|
|
|
aria-describedby="descriptionTestConnection"
|
2022-10-26 09:45:32 +00:00
|
|
|
isLoading={isTesting}
|
|
|
|
spinnerAriaValueText={t("testingConnection")}
|
2021-06-10 20:00:14 +00:00
|
|
|
>
|
2021-10-05 10:31:39 +00:00
|
|
|
{t("common:testConnection")}
|
2021-06-10 20:00:14 +00:00
|
|
|
</Button>
|
2022-10-24 11:43:46 +00:00
|
|
|
</ActionListItem>
|
|
|
|
<ActionListItem>
|
2022-10-17 10:08:49 +00:00
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
onClick={reset}
|
|
|
|
data-testid="email-tab-revert"
|
|
|
|
>
|
2021-05-03 20:00:12 +00:00
|
|
|
{t("common:revert")}
|
|
|
|
</Button>
|
2022-10-24 11:43:46 +00:00
|
|
|
</ActionListItem>
|
|
|
|
</ActionGroup>
|
|
|
|
{currentUser && (
|
|
|
|
<FormGroup id="descriptionTestConnection">
|
|
|
|
{currentUser.email ? (
|
|
|
|
<Alert
|
|
|
|
variant="info"
|
|
|
|
isInline
|
|
|
|
title={t("testConnectionHint.withEmail", {
|
|
|
|
email: currentUser.email,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Alert
|
|
|
|
variant="warning"
|
|
|
|
isInline
|
|
|
|
title={t("testConnectionHint.withoutEmail", {
|
|
|
|
userName: currentUser.username,
|
|
|
|
})}
|
|
|
|
actionLinks={
|
|
|
|
<AlertActionLink
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
|
|
|
to={toUser({
|
|
|
|
realm: realmName,
|
|
|
|
id: currentUser.id!,
|
|
|
|
tab: "settings",
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{t("testConnectionHint.withoutEmailAction")}
|
|
|
|
</AlertActionLink>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
|
|
|
</FormAccess>
|
|
|
|
</FormPanel>
|
|
|
|
</PageSection>
|
2021-05-03 20:00:12 +00:00
|
|
|
);
|
|
|
|
};
|