keycloak-scim/js/apps/admin-ui/src/realm-settings/SessionsTab.tsx

392 lines
12 KiB
TypeScript
Raw Normal View History

import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
2021-07-15 14:25:22 +00:00
import {
ActionGroup,
Button,
FormGroup,
PageSection,
Switch,
} from "@patternfly/react-core";
import { useEffect } from "react";
import { Controller, useForm, useWatch } from "react-hook-form";
import { useTranslation } from "react-i18next";
2021-07-15 14:25:22 +00:00
import { FormAccess } from "../components/form/FormAccess";
2023-03-07 09:29:40 +00:00
import { HelpItem } from "ui-shared";
2021-07-15 14:25:22 +00:00
import { FormPanel } from "../components/scroll-form/FormPanel";
import { TimeSelector } from "../components/time-selector/TimeSelector";
import { convertToFormValues } from "../util";
2021-07-15 14:25:22 +00:00
import "./realm-settings-section.css";
2021-07-15 14:25:22 +00:00
type RealmSettingsSessionsTabProps = {
realm: RealmRepresentation;
save: (realm: RealmRepresentation) => void;
2021-07-15 14:25:22 +00:00
};
export const RealmSettingsSessionsTab = ({
realm,
save,
2021-07-15 14:25:22 +00:00
}: RealmSettingsSessionsTabProps) => {
const { t } = useTranslation();
2021-07-15 14:25:22 +00:00
const { setValue, control, handleSubmit, formState } =
useForm<RealmRepresentation>();
2021-07-15 14:25:22 +00:00
const offlineSessionMaxEnabled = useWatch({
control,
name: "offlineSessionMaxLifespanEnabled",
});
const setupForm = () => {
convertToFormValues(realm, setValue);
2021-07-15 14:25:22 +00:00
};
useEffect(setupForm, []);
2021-07-15 14:25:22 +00:00
return (
2021-08-26 12:15:28 +00:00
<PageSection variant="light">
<FormPanel
title={t("SSOSessionSettings")}
className="kc-sso-session-template"
>
<FormAccess
isHorizontal
role="manage-realm"
onSubmit={handleSubmit(save)}
2021-07-15 14:25:22 +00:00
>
2021-08-26 12:15:28 +00:00
<FormGroup
label={t("SSOSessionIdle")}
fieldId="SSOSessionIdle"
labelIcon={
<HelpItem
2023-03-07 09:29:40 +00:00
helpText={t("realm-settings-help:ssoSessionIdle")}
2021-12-14 14:56:36 +00:00
fieldLabelId="realm-settings:SSOSessionIdle"
2021-08-26 12:15:28 +00:00
/>
}
2021-07-15 14:25:22 +00:00
>
2021-08-26 12:15:28 +00:00
<Controller
name="ssoSessionIdleTimeout"
defaultValue={realm.ssoSessionIdleTimeout}
2021-08-26 12:15:28 +00:00
control={control}
render={({ field }) => (
2021-08-26 12:15:28 +00:00
<TimeSelector
className="kc-sso-session-idle"
data-testid="sso-session-idle-input"
value={field.value!}
onChange={field.onChange}
units={["minute", "hour", "day"]}
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
)}
/>
</FormGroup>
2021-07-15 14:25:22 +00:00
2021-08-26 12:15:28 +00:00
<FormGroup
label={t("SSOSessionMax")}
fieldId="SSOSessionMax"
labelIcon={
<HelpItem
2023-03-07 09:29:40 +00:00
helpText={t("realm-settings-help:ssoSessionMax")}
2021-12-14 14:56:36 +00:00
fieldLabelId="realm-settings:SSOSessionMax"
2021-08-26 12:15:28 +00:00
/>
}
>
<Controller
name="ssoSessionMaxLifespan"
control={control}
render={({ field }) => (
2021-08-26 12:15:28 +00:00
<TimeSelector
className="kc-sso-session-max"
data-testid="sso-session-max-input"
value={field.value!}
onChange={field.onChange}
units={["minute", "hour", "day"]}
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
)}
/>
</FormGroup>
<FormGroup
label={t("SSOSessionIdleRememberMe")}
fieldId="SSOSessionIdleRememberMe"
labelIcon={
<HelpItem
2023-03-07 09:29:40 +00:00
helpText={t("realm-settings-help:ssoSessionIdleRememberMe")}
2021-12-14 14:56:36 +00:00
fieldLabelId="realm-settings:SSOSessionIdleRememberMe"
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
}
>
<Controller
name="ssoSessionIdleTimeoutRememberMe"
control={control}
render={({ field }) => (
2021-08-26 12:15:28 +00:00
<TimeSelector
className="kc-sso-session-idle-remember-me"
data-testid="sso-session-idle-remember-me-input"
value={field.value!}
onChange={field.onChange}
units={["minute", "hour", "day"]}
2021-08-26 12:15:28 +00:00
/>
)}
/>
</FormGroup>
2021-07-15 14:25:22 +00:00
2021-08-26 12:15:28 +00:00
<FormGroup
label={t("SSOSessionMaxRememberMe")}
fieldId="SSOSessionMaxRememberMe"
labelIcon={
<HelpItem
2023-03-07 09:29:40 +00:00
helpText={t("realm-settings-help:ssoSessionMaxRememberMe")}
2021-12-14 14:56:36 +00:00
fieldLabelId="realm-settings:SSOSessionMaxRememberMe"
2021-08-26 12:15:28 +00:00
/>
}
>
<Controller
name="ssoSessionMaxLifespanRememberMe"
control={control}
render={({ field }) => (
2021-08-26 12:15:28 +00:00
<TimeSelector
className="kc-sso-session-max-remember-me"
data-testid="sso-session-max-remember-me-input"
value={field.value!}
onChange={field.onChange}
units={["minute", "hour", "day"]}
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
)}
/>
</FormGroup>
</FormAccess>
</FormPanel>
<FormPanel
title={t("clientSessionSettings")}
className="kc-client-session-template"
>
<FormAccess
isHorizontal
role="manage-realm"
className="pf-u-mt-lg"
onSubmit={handleSubmit(save)}
>
<FormGroup
label={t("clientSessionIdle")}
fieldId="clientSessionIdle"
labelIcon={
<HelpItem
helpText={t("clientSessionIdleHelp")}
2021-12-14 14:56:36 +00:00
fieldLabelId="realm-settings:clientSessionIdle"
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
}
>
<Controller
name="clientSessionIdleTimeout"
control={control}
render={({ field }) => (
2021-08-26 12:15:28 +00:00
<TimeSelector
className="kc-client-session-idle"
data-testid="client-session-idle-input"
value={field.value!}
onChange={field.onChange}
units={["minute", "hour", "day"]}
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
)}
/>
</FormGroup>
<FormGroup
label={t("clientSessionMax")}
fieldId="clientSessionMax"
labelIcon={
<HelpItem
helpText={t("clientSessionMaxHelp")}
2021-12-14 14:56:36 +00:00
fieldLabelId="realm-settings:clientSessionMax"
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
}
2021-07-15 14:25:22 +00:00
>
2021-08-26 12:15:28 +00:00
<Controller
name="clientSessionMaxLifespan"
control={control}
render={({ field }) => (
2021-08-26 12:15:28 +00:00
<TimeSelector
className="kc-client-session-max"
data-testid="client-session-max-input"
value={field.value!}
onChange={field.onChange}
units={["minute", "hour", "day"]}
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
)}
/>
</FormGroup>
</FormAccess>
</FormPanel>
<FormPanel
title={t("offlineSessionSettings")}
className="kc-offline-session-template"
>
<FormAccess
isHorizontal
role="manage-realm"
className="pf-u-mt-lg"
onSubmit={handleSubmit(save)}
>
<FormGroup
label={t("offlineSessionIdle")}
fieldId="offlineSessionIdle"
labelIcon={
<HelpItem
helpText={t("offlineSessionIdleHelp")}
2021-12-14 14:56:36 +00:00
fieldLabelId="realm-settings:offlineSessionIdle"
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
}
>
<Controller
name="offlineSessionIdleTimeout"
control={control}
render={({ field }) => (
2021-08-26 12:15:28 +00:00
<TimeSelector
className="kc-offline-session-idle"
data-testid="offline-session-idle-input"
aria-label="offline-session-idle-input"
value={field.value!}
onChange={field.onChange}
units={["minute", "hour", "day"]}
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
)}
/>
</FormGroup>
<FormGroup
hasNoPaddingTop
label={t("offlineSessionMaxLimited")}
fieldId="kc-offlineSessionMaxLimited"
labelIcon={
<HelpItem
helpText={t("offlineSessionMaxLimitedHelp")}
2021-12-14 14:56:36 +00:00
fieldLabelId="realm-settings:offlineSessionMaxLimited"
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
}
2021-07-15 14:25:22 +00:00
>
2021-08-26 12:15:28 +00:00
<Controller
name="offlineSessionMaxLifespanEnabled"
control={control}
defaultValue={false}
render={({ field }) => (
2021-08-26 12:15:28 +00:00
<Switch
id="kc-offline-session-max"
data-testid="offline-session-max-switch"
aria-label={t("offlineSessionMaxLimited")}
2021-08-26 12:15:28 +00:00
label={t("common:enabled")}
labelOff={t("common:disabled")}
isChecked={field.value}
onChange={field.onChange}
2021-08-26 12:15:28 +00:00
/>
)}
/>
</FormGroup>
{offlineSessionMaxEnabled && (
2021-07-15 14:25:22 +00:00
<FormGroup
2021-08-26 12:15:28 +00:00
label={t("offlineSessionMax")}
fieldId="offlineSessionMax"
id="offline-session-max-label"
2021-07-15 14:25:22 +00:00
labelIcon={
<HelpItem
helpText={t("offlineSessionMaxHelp")}
2021-12-14 14:56:36 +00:00
fieldLabelId="realm-settings:offlineSessionMax"
2021-07-15 14:25:22 +00:00
/>
}
>
<Controller
2021-08-26 12:15:28 +00:00
name="offlineSessionMaxLifespan"
2021-07-15 14:25:22 +00:00
control={control}
render={({ field }) => (
2021-07-15 14:25:22 +00:00
<TimeSelector
2021-08-26 12:15:28 +00:00
className="kc-offline-session-max"
data-testid="offline-session-max-input"
value={field.value!}
onChange={field.onChange}
units={["minute", "hour", "day"]}
2021-07-15 14:25:22 +00:00
/>
)}
/>
</FormGroup>
2021-08-26 12:15:28 +00:00
)}
</FormAccess>
</FormPanel>
<FormPanel
className="kc-login-settings-template"
title={t("loginSettings")}
>
<FormAccess
isHorizontal
role="manage-realm"
className="pf-u-mt-lg"
onSubmit={handleSubmit(save)}
2021-07-15 14:25:22 +00:00
>
2021-08-26 12:15:28 +00:00
<FormGroup
label={t("loginTimeout")}
id="kc-login-timeout-label"
fieldId="offlineSessionIdle"
labelIcon={
<HelpItem
helpText={t("loginTimeoutHelp")}
2021-12-14 14:56:36 +00:00
fieldLabelId="realm-settings:loginTimeout"
2021-08-26 12:15:28 +00:00
/>
}
2021-07-15 14:25:22 +00:00
>
2021-08-26 12:15:28 +00:00
<Controller
name="accessCodeLifespanLogin"
control={control}
render={({ field }) => (
2021-08-26 12:15:28 +00:00
<TimeSelector
className="kc-login-timeout"
data-testid="login-timeout-input"
aria-label="login-timeout-input"
value={field.value!}
onChange={field.onChange}
units={["minute", "hour", "day"]}
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
)}
/>
</FormGroup>
<FormGroup
label={t("loginActionTimeout")}
fieldId="loginActionTimeout"
id="login-action-timeout-label"
labelIcon={
<HelpItem
helpText={t("loginActionTimeoutHelp")}
2021-12-14 14:56:36 +00:00
fieldLabelId="realm-settings:loginActionTimeout"
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
}
>
<Controller
name="accessCodeLifespanUserAction"
control={control}
render={({ field }) => (
2021-08-26 12:15:28 +00:00
<TimeSelector
className="kc-login-action-timeout"
data-testid="login-action-timeout-input"
value={field.value!}
onChange={field.onChange}
units={["minute", "hour", "day"]}
2021-07-15 14:25:22 +00:00
/>
2021-08-26 12:15:28 +00:00
)}
/>
</FormGroup>
<ActionGroup>
<Button
variant="primary"
type="submit"
data-testid="sessions-tab-save"
isDisabled={!formState.isDirty}
2021-07-15 14:25:22 +00:00
>
2021-08-26 12:15:28 +00:00
{t("common:save")}
</Button>
<Button variant="link" onClick={setupForm}>
2021-08-26 12:15:28 +00:00
{t("common:revert")}
</Button>
</ActionGroup>
</FormAccess>
</FormPanel>
</PageSection>
2021-07-15 14:25:22 +00:00
);
};