From db2f598bd4488b03cd28e7aebe19146040364bad Mon Sep 17 00:00:00 2001 From: Michal Gold Date: Thu, 2 Jan 2025 16:05:20 +0200 Subject: [PATCH] wizard: add Administrator checkbox to users step (HMS-4903) this commit add Administrator checkbox to users step --- .../steps/Users/component/UserInfo.tsx | 25 ++++++++++++++++- .../utilities/requestMapper.ts | 3 +++ src/store/wizardSlice.ts | 27 +++++++++++++++++++ .../steps/Users/Users.test.tsx | 1 + src/test/fixtures/editMode.ts | 1 + 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/Components/CreateImageWizard/steps/Users/component/UserInfo.tsx b/src/Components/CreateImageWizard/steps/Users/component/UserInfo.tsx index 48b433cdc..6c479962f 100644 --- a/src/Components/CreateImageWizard/steps/Users/component/UserInfo.tsx +++ b/src/Components/CreateImageWizard/steps/Users/component/UserInfo.tsx @@ -1,17 +1,19 @@ import React from 'react'; -import { Button, FormGroup } from '@patternfly/react-core'; +import { Button, FormGroup, Checkbox } from '@patternfly/react-core'; import { ExternalLinkAltIcon } from '@patternfly/react-icons'; import { GENERATING_SSH_KEY_PAIRS_URL } from '../../../../../constants'; import { useAppDispatch, useAppSelector } from '../../../../../store/hooks'; import { + selectUserAdministrator, selectUserNameByIndex, selectUserPasswordByIndex, selectUserSshKeyByIndex, setUserNameByIndex, setUserPasswordByIndex, setUserSshKeyByIndex, + setUserAdministratorByIndex, } from '../../../../../store/wizardSlice'; import { HookValidatedInput } from '../../../ValidatedTextInput'; const UserInfo = () => { @@ -23,6 +25,8 @@ const UserInfo = () => { const userPassword = useAppSelector(userPasswordSelector); const userSshKeySelector = selectUserSshKeyByIndex(index); const userSshKey = useAppSelector(userSshKeySelector); + const userIsAdministratorSelector = selectUserAdministrator(index); + const userIsAdministrator = useAppSelector(userIsAdministratorSelector); const handleNameChange = ( _e: React.FormEvent, @@ -45,6 +49,15 @@ const UserInfo = () => { dispatch(setUserSshKeyByIndex({ index: index, sshKey: value })); }; + const handleCheckboxChange = ( + _event: React.FormEvent, + value: boolean + ) => { + dispatch( + setUserAdministratorByIndex({ index: index, isAdministrator: value }) + ); + }; + const stepValidation = { errors: {}, disabledNext: false, @@ -94,6 +107,16 @@ const UserInfo = () => { Learn more about SSH keys + + handleCheckboxChange(_e, value)} + aria-label="Administrator" + id="user Administrator" + name="user Administrator" + /> + ); }; diff --git a/src/Components/CreateImageWizard/utilities/requestMapper.ts b/src/Components/CreateImageWizard/utilities/requestMapper.ts index 5a05c3d01..904e5cc0f 100644 --- a/src/Components/CreateImageWizard/utilities/requestMapper.ts +++ b/src/Components/CreateImageWizard/utilities/requestMapper.ts @@ -587,6 +587,9 @@ const getUsers = (state: RootState): User[] | undefined => { if (user.ssh_key !== '') { result.ssh_key = user.ssh_key; } + if (user.groups !== undefined) { + result.groups = user.groups; + } return result as User; }); }; diff --git a/src/store/wizardSlice.ts b/src/store/wizardSlice.ts index eb00d938a..e3e2ee454 100644 --- a/src/store/wizardSlice.ts +++ b/src/store/wizardSlice.ts @@ -46,6 +46,8 @@ export type ComplianceType = 'openscap' | 'compliance'; export type UserWithAdditionalInfo = { [K in keyof User]-?: NonNullable; +} & { + isAdministrator: boolean; }; type UserPayload = { @@ -63,6 +65,11 @@ type UserSshKeyPayload = { sshKey: string; }; +type UserAdministratorPayload = { + index: number; + isAdministrator: boolean; +}; + export type wizardState = { env: { serverUrl: string; @@ -378,6 +385,11 @@ export const selectUserSshKeyByIndex = return state.wizard.users[userIndex]?.ssh_key; }; +export const selectUserAdministrator = + (userIndex: number) => (state: RootState) => { + return state.wizard.users[userIndex]?.isAdministrator; + }; + export const selectKernel = (state: RootState) => { return state.wizard.kernel; }; @@ -838,6 +850,20 @@ export const wizardSlice = createSlice({ setUserSshKeyByIndex: (state, action: PayloadAction) => { state.users[action.payload.index].ssh_key = action.payload.sshKey; }, + setUserAdministratorByIndex: ( + state, + action: PayloadAction + ) => { + const { index, isAdministrator } = action.payload; + state.users[index].isAdministrator = isAdministrator; + if (isAdministrator) { + state.users[index].groups?.push('wheel'); + } else { + state.users[index].groups = state.users[index].groups?.filter( + (group) => group !== 'wheel' + ); + } + }, }, }); @@ -910,5 +936,6 @@ export const { setUserNameByIndex, setUserPasswordByIndex, setUserSshKeyByIndex, + setUserAdministratorByIndex, } = wizardSlice.actions; export default wizardSlice.reducer; diff --git a/src/test/Components/CreateImageWizard/steps/Users/Users.test.tsx b/src/test/Components/CreateImageWizard/steps/Users/Users.test.tsx index f66b833b2..ac05e5974 100644 --- a/src/test/Components/CreateImageWizard/steps/Users/Users.test.tsx +++ b/src/test/Components/CreateImageWizard/steps/Users/Users.test.tsx @@ -135,6 +135,7 @@ describe('Step Users', () => { { name: 'best', ssh_key: 'ssh-rsa d', + groups: [], }, ], }, diff --git a/src/test/fixtures/editMode.ts b/src/test/fixtures/editMode.ts index 6f965ba88..ad9158b9b 100644 --- a/src/test/fixtures/editMode.ts +++ b/src/test/fixtures/editMode.ts @@ -448,6 +448,7 @@ export const usersCreateBlueprintRequest: CreateBlueprintRequest = { { name: 'best', ssh_key: 'ssh-rsa d', + groups: [], }, ], },