Append required user actions and only when the password is temporary (#27185)

Signed-off-by: Peter Keuter <github@peterkeuter.nl>
This commit is contained in:
Peter Keuter 2024-03-01 16:43:07 +01:00 committed by GitHub
parent 66e2591792
commit 6609b591e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 5 deletions

View file

@ -337,7 +337,7 @@ export default function EditUser() {
title={<TabTitleText>{t("credentials")}</TabTitleText>}
{...credentialsTab}
>
<UserCredentials user={user} />
<UserCredentials user={user} setUser={setUser} />
</Tab>
<Tab
data-testid="role-mapping-tab"

View file

@ -44,6 +44,7 @@ import "./user-credentials.css";
type UserCredentialsProps = {
user: UserRepresentation;
setUser: (user: UserRepresentation) => void;
};
type ExpandableCredentialRepresentation = {
@ -52,7 +53,7 @@ type ExpandableCredentialRepresentation = {
isExpanded: boolean;
};
export const UserCredentials = ({ user }: UserCredentialsProps) => {
export const UserCredentials = ({ user, setUser }: UserCredentialsProps) => {
const { t } = useTranslation();
const { addAlert, addError } = useAlerts();
const [key, setKey] = useState(0);
@ -286,6 +287,13 @@ export const UserCredentials = ({ user }: UserCredentialsProps) => {
}
};
const onAddRequiredActions = (requiredActions: string[]) => {
setUser({
...user,
requiredActions: [...(user.requiredActions ?? []), ...requiredActions],
});
};
const onDragEnd = ({ target }: ReactDragEvent) => {
if (!(target instanceof HTMLTableRowElement)) {
return;
@ -357,6 +365,7 @@ export const UserCredentials = ({ user }: UserCredentialsProps) => {
<ResetPasswordDialog
user={user}
isResetPassword={isResetPassword}
onAddRequiredActions={onAddRequiredActions}
refresh={refresh}
onClose={() => setIsOpen(false)}
/>

View file

@ -13,7 +13,7 @@ import {
Switch,
} from "@patternfly/react-core";
import { TFunction } from "i18next";
import { useState } from "react";
import { useState, useEffect } from "react";
import { Controller, UseFormReturn } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
@ -82,7 +82,9 @@ export const UserForm = ({
const [open, setOpen] = useState(false);
const [locked, setLocked] = useState(isLocked);
useEffect(() => {
setValue("requiredActions", user?.requiredActions || []);
}, [user, setValue]);
const unLockUser = async () => {
try {

View file

@ -1,4 +1,5 @@
import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
import { RequiredActionAlias } from "@keycloak/keycloak-admin-client/lib/defs/requiredActionProviderRepresentation";
import {
AlertVariant,
ButtonVariant,
@ -23,6 +24,7 @@ import useToggle from "../../utils/useToggle";
type ResetPasswordDialogProps = {
user: UserRepresentation;
isResetPassword: boolean;
onAddRequiredActions?: (requiredActions: string[]) => void;
refresh: () => void;
onClose: () => void;
};
@ -42,6 +44,7 @@ const credFormDefaultValues: CredentialsForm = {
export const ResetPasswordDialog = ({
user,
isResetPassword,
onAddRequiredActions,
refresh,
onClose,
}: ResetPasswordDialogProps) => {
@ -88,7 +91,9 @@ export const ResetPasswordDialog = ({
value: password,
},
});
user.requiredActions = ["UPDATE_PASSWORD"];
if (temporaryPassword) {
onAddRequiredActions?.([RequiredActionAlias.UPDATE_PASSWORD]);
}
const credentials = await adminClient.users.getCredentials({
id: user.id!,
});