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>} title={<TabTitleText>{t("credentials")}</TabTitleText>}
{...credentialsTab} {...credentialsTab}
> >
<UserCredentials user={user} /> <UserCredentials user={user} setUser={setUser} />
</Tab> </Tab>
<Tab <Tab
data-testid="role-mapping-tab" data-testid="role-mapping-tab"

View file

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

View file

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

View file

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