From fc0a9be79fea83fb957ccbf4b3cfa394167375c0 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 28 Mar 2023 14:44:01 +0200 Subject: [PATCH] Only show activated required actions in credentials reset dialog (#19048) --- .../admin-ui/public/resources/en/users.json | 1 + js/apps/admin-ui/src/user/UserForm.tsx | 79 ++----------------- ...lect.tsx => RequiredActionMultiSelect.tsx} | 29 ++++--- .../ResetCredentialDialog.tsx | 8 +- 4 files changed, 34 insertions(+), 83 deletions(-) rename js/apps/admin-ui/src/user/user-credentials/{CredentialsResetActionMultiSelect.tsx => RequiredActionMultiSelect.tsx} (80%) diff --git a/js/apps/admin-ui/public/resources/en/users.json b/js/apps/admin-ui/public/resources/en/users.json index 053fec8671..85b3824242 100644 --- a/js/apps/admin-ui/public/resources/en/users.json +++ b/js/apps/admin-ui/public/resources/en/users.json @@ -45,6 +45,7 @@ "emailInvalid": "You must enter a valid email.", "notVerified": "Not verified", "requiredUserActions": "Required user actions", + "requiredActionPlaceholder": "Select action", "federationLink": "Federation link", "addUser": "Add user", "impersonate": "Impersonate", diff --git a/js/apps/admin-ui/src/user/UserForm.tsx b/js/apps/admin-ui/src/user/UserForm.tsx index f35eb79377..9ba19b4577 100644 --- a/js/apps/admin-ui/src/user/UserForm.tsx +++ b/js/apps/admin-ui/src/user/UserForm.tsx @@ -1,6 +1,5 @@ import type GroupRepresentation from "@keycloak/keycloak-admin-client/lib/defs/groupRepresentation"; import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation"; -import type RequiredActionProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/requiredActionProviderRepresentation"; import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation"; import { ActionGroup, @@ -10,8 +9,6 @@ import { ChipGroup, FormGroup, InputGroup, - Select, - SelectOption, Switch, } from "@patternfly/react-core"; import { useState } from "react"; @@ -32,6 +29,7 @@ import useFormatDate from "../utils/useFormatDate"; import useIsFeatureEnabled, { Feature } from "../utils/useIsFeatureEnabled"; import { FederatedUserLink } from "./FederatedUserLink"; import { UserProfileFields } from "./UserProfileFields"; +import { RequiredActionMultiSelect } from "./user-credentials/RequiredActionMultiSelect"; export type BruteForced = { isBruteForceProtected?: boolean; @@ -93,10 +91,6 @@ export const UserForm = ({ const formatDate = useFormatDate(); const isFeatureEnabled = useIsFeatureEnabled(); - const [ - isRequiredUserActionsDropdownOpen, - setRequiredUserActionsDropdownOpen, - ] = useState(false); const navigate = useNavigate(); const { adminClient } = useAdminClient(); const { addAlert, addError } = useAlerts(); @@ -118,22 +112,14 @@ export const UserForm = ({ const [open, setOpen] = useState(false); const [locked, setLocked] = useState(isLocked); const [realm, setRealm] = useState(); - const [requiredActions, setRequiredActions] = useState< - RequiredActionProviderRepresentation[] - >([]); useFetch( - () => - Promise.all([ - adminClient.realms.findOne({ realm: realmName }), - adminClient.authenticationManagement.getRequiredActions(), - ]), - ([realm, actions]) => { + () => adminClient.realms.findOne({ realm: realmName }), + (realm) => { if (!realm) { throw new Error(t("common:notFound")); } setRealm(realm); - setRequiredActions(actions); }, [] ); @@ -147,10 +133,6 @@ export const UserForm = ({ } }; - const clearSelection = () => { - setRequiredUserActionsDropdownOpen(false); - }; - const deleteItem = (id: string) => { setSelectedGroups(selectedGroups.filter((item) => item.name !== id)); onGroupsUpdate?.(selectedGroups); @@ -233,56 +215,11 @@ export const UserForm = ({ )} - - } - > - ( - - )} - /> - + {(user?.federationLink || user?.origin) && ( { +type RequiredActionMultiSelectProps = { + name: string; + label: string; + help: string; +}; + +export const RequiredActionMultiSelect = ({ + name, + label, + help, +}: RequiredActionMultiSelectProps) => { const { t } = useTranslation("users"); const { adminClient } = useAdminClient(); const { control } = useFormContext(); @@ -24,24 +34,22 @@ export const CredentialsResetActionMultiSelect = () => { useFetch( () => adminClient.authenticationManagement.getRequiredActions(), (actions) => { - setRequiredActions(actions); + const enabledUserActions = actions.filter((action) => { + return action.enabled; + }); + setRequiredActions(enabledUserActions); }, [] ); return ( - } + label={t(label)} + labelIcon={} fieldId="actions" > ( @@ -52,6 +60,7 @@ export const CredentialsResetActionMultiSelect = () => { chipGroupProps={{ numChips: 3, }} + placeholderText={t("requiredActionPlaceholder")} menuAppendTo="parent" onToggle={(open) => setOpen(open)} isOpen={open} diff --git a/js/apps/admin-ui/src/user/user-credentials/ResetCredentialDialog.tsx b/js/apps/admin-ui/src/user/user-credentials/ResetCredentialDialog.tsx index a29f7faf4b..c115bd48ca 100644 --- a/js/apps/admin-ui/src/user/user-credentials/ResetCredentialDialog.tsx +++ b/js/apps/admin-ui/src/user/user-credentials/ResetCredentialDialog.tsx @@ -3,7 +3,7 @@ import { FormProvider, useForm, useWatch } from "react-hook-form"; import { ModalVariant, Form, AlertVariant } from "@patternfly/react-core"; import type { RequiredActionAlias } from "@keycloak/keycloak-admin-client/lib/defs/requiredActionProviderRepresentation"; -import { CredentialsResetActionMultiSelect } from "./CredentialsResetActionMultiSelect"; +import { RequiredActionMultiSelect } from "./RequiredActionMultiSelect"; import { ConfirmDialogModal } from "../../components/confirm-dialog/ConfirmDialog"; import { useAdminClient } from "../../context/auth/AdminClient"; import { useAlerts } from "../../components/alert/Alerts"; @@ -84,7 +84,11 @@ export const ResetCredentialDialog = ({ data-testid="credential-reset-modal" > - +