keycloak-scim/src/realm-settings/LoginTab.tsx

283 lines
9.6 KiB
TypeScript
Raw Normal View History

import React from "react";
import { useTranslation } from "react-i18next";
import { FormGroup, PageSection, Switch } from "@patternfly/react-core";
import { FormAccess } from "../components/form-access/FormAccess";
import { HelpItem } from "../components/help-enabler/HelpItem";
import { FormPanel } from "../components/scroll-form/FormPanel";
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
import { Controller, useForm } from "react-hook-form";
type RealmSettingsLoginTabProps = {
save: (realm: RealmRepresentation) => void;
realm: RealmRepresentation;
refresh: () => void;
};
export const RealmSettingsLoginTab = ({
save,
realm,
refresh,
}: RealmSettingsLoginTabProps) => {
const { t } = useTranslation("realm-settings");
const form = useForm<RealmRepresentation>({ mode: "onChange" });
const updateSwitchValue = (
onChange: (newValue: boolean) => void,
value: boolean,
name: string
) => {
save({ ...realm, [name as keyof typeof realm]: value });
onChange(value);
refresh();
};
return (
2021-08-26 12:15:28 +00:00
<PageSection variant="light">
<FormPanel className="kc-login-screen" title="Login screen customization">
<FormAccess isHorizontal role="manage-realm">
<FormGroup
label={t("userRegistration")}
fieldId="kc-user-reg"
labelIcon={
<HelpItem
helpText={t("userRegistrationHelpText")}
forLabel={t("userRegistration")}
forID={t(`common:helpLabel`, {
label: t("userRegistration"),
})}
/>
2021-08-26 12:15:28 +00:00
}
hasNoPaddingTop
>
<Controller
2021-08-26 12:15:28 +00:00
name="registrationAllowed"
defaultValue={realm.registrationAllowed}
control={form.control}
render={({ onChange, value }) => (
<Switch
id="kc-user-reg-switch"
data-testid="user-reg-switch"
name="registrationAllowed"
label={t("common:on")}
labelOff={t("common:off")}
isChecked={value}
onChange={(value) => {
updateSwitchValue(onChange, value, "registrationAllowed");
}}
/>
)}
2021-08-26 12:15:28 +00:00
/>
</FormGroup>
<FormGroup
label={t("forgotPassword")}
fieldId="kc-forgot-pw"
labelIcon={
<HelpItem
helpText={t("forgotPasswordHelpText")}
forLabel={t("forgotPassword")}
forID={t(`common:helpLabel`, { label: t("forgotPassword") })}
/>
2021-08-26 12:15:28 +00:00
}
hasNoPaddingTop
>
<Controller
2021-08-26 12:15:28 +00:00
name="resetPasswordAllowed"
defaultValue={realm.resetPasswordAllowed}
control={form.control}
render={({ onChange, value }) => (
<Switch
id="kc-forgot-pw-switch"
data-testid="forgot-pw-switch"
name="resetPasswordAllowed"
label={t("common:on")}
labelOff={t("common:off")}
isChecked={value}
onChange={(value) => {
updateSwitchValue(onChange, value, "resetPasswordAllowed");
}}
/>
)}
2021-08-26 12:15:28 +00:00
/>
</FormGroup>
<FormGroup
label={t("rememberMe")}
fieldId="kc-remember-me"
labelIcon={
<HelpItem
helpText={t("rememberMeHelpText")}
forLabel={t("rememberMe")}
forID={t(`common:helpLabel`, { label: t("rememberMe") })}
/>
2021-08-26 12:15:28 +00:00
}
hasNoPaddingTop
>
<Controller
2021-08-26 12:15:28 +00:00
name="rememberMe"
defaultValue={realm.rememberMe}
control={form.control}
render={({ onChange, value }) => (
<Switch
id="kc-remember-me-switch"
data-testid="remember-me-switch"
name="rememberMe"
label={t("common:on")}
labelOff={t("common:off")}
isChecked={value}
onChange={(value) => {
updateSwitchValue(onChange, value, "rememberMe");
}}
/>
)}
2021-08-26 12:15:28 +00:00
/>
</FormGroup>
</FormAccess>
</FormPanel>
<FormPanel className="kc-email-settings" title="Email settings">
<FormAccess isHorizontal role="manage-realm">
<FormGroup
label={t("emailAsUsername")}
fieldId="kc-email-as-username"
labelIcon={
<HelpItem
helpText={t("emailAsUsernameHelpText")}
forLabel={t("emailAsUsername")}
forID={t(`common:helpLabel`, { label: t("emailAsUsername") })}
/>
2021-08-26 12:15:28 +00:00
}
hasNoPaddingTop
>
<Controller
2021-08-26 12:15:28 +00:00
name="registrationEmailAsUsername"
defaultValue={realm.registrationEmailAsUsername}
control={form.control}
render={({ onChange, value }) => (
<Switch
id="kc-email-as-username-switch"
data-testid="email-as-username-switch"
name="registrationEmailAsUsername"
label={t("common:on")}
labelOff={t("common:off")}
isChecked={value}
onChange={(value) => {
updateSwitchValue(
onChange,
value,
"registrationEmailAsUsername"
);
}}
/>
)}
2021-08-26 12:15:28 +00:00
/>
</FormGroup>
<FormGroup
label={t("loginWithEmail")}
fieldId="kc-login-with-email"
labelIcon={
<HelpItem
helpText={t("loginWithEmailHelpText")}
forLabel={t("loginWithEmail")}
forID={t(`common:helpLabel`, { label: t("loginWithEmail") })}
/>
2021-08-26 12:15:28 +00:00
}
hasNoPaddingTop
>
<Controller
2021-08-26 12:15:28 +00:00
name="loginWithEmailAllowed"
defaultValue={realm.loginWithEmailAllowed}
control={form.control}
render={({ onChange, value }) => (
<Switch
id="kc-login-with-email-switch"
data-testid="login-with-email-switch"
name="loginWithEmailAllowed"
label={t("common:on")}
labelOff={t("common:off")}
isChecked={value}
onChange={(value) => {
updateSwitchValue(onChange, value, "loginWithEmailAllowed");
}}
/>
)}
2021-08-26 12:15:28 +00:00
/>
</FormGroup>
<FormGroup
label={t("duplicateEmails")}
fieldId="kc-duplicate-emails"
labelIcon={
<HelpItem
helpText={t("duplicateEmailsHelpText")}
forLabel={t("duplicateEmails")}
forID={t(`common:helpLabel`, { label: t("duplicateEmails") })}
/>
2021-08-26 12:15:28 +00:00
}
hasNoPaddingTop
>
<Controller
2021-08-26 12:15:28 +00:00
name="duplicateEmailsAllowed"
defaultValue={realm.duplicateEmailsAllowed}
control={form.control}
render={({ onChange }) => (
<Switch
id="kc-duplicate-emails-switch"
data-testid="duplicate-emails-switch"
label={t("common:on")}
labelOff={t("common:off")}
name="duplicateEmailsAllowed"
isChecked={
form.getValues().duplicateEmailsAllowed &&
!form.getValues().loginWithEmailAllowed &&
!form.getValues().registrationEmailAsUsername
}
onChange={(value) => {
updateSwitchValue(
onChange,
value,
"duplicateEmailsAllowed"
);
}}
isDisabled={
form.getValues().loginWithEmailAllowed ||
form.getValues().registrationEmailAsUsername
}
/>
)}
2021-08-26 12:15:28 +00:00
/>
</FormGroup>
<FormGroup
label={t("verifyEmail")}
fieldId="kc-verify-email"
labelIcon={
<HelpItem
helpText={t("verifyEmailHelpText")}
forLabel={t("verifyEmail")}
forID={t(`common:helpLabel`, { label: t("verifyEmail") })}
/>
2021-08-26 12:15:28 +00:00
}
hasNoPaddingTop
>
<Controller
2021-08-26 12:15:28 +00:00
name="verifyEmail"
defaultValue={realm.verifyEmail}
control={form.control}
render={({ onChange, value }) => (
<Switch
id="kc-verify-email-switch"
data-testid="verify-email-switch"
name="verifyEmail"
label={t("common:on")}
labelOff={t("common:off")}
isChecked={value}
onChange={(value) => {
updateSwitchValue(onChange, value, "verifyEmail");
}}
/>
)}
2021-08-26 12:15:28 +00:00
/>
</FormGroup>
</FormAccess>
</FormPanel>
</PageSection>
);
};