Skip to content

Commit

Permalink
wizard: add Administrator checkbox to users step (HMS-4903)
Browse files Browse the repository at this point in the history
this commit add Administrator checkbox to users step
  • Loading branch information
mgold1234 authored and lucasgarfield committed Jan 13, 2025
1 parent 681006f commit db2f598
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand All @@ -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<HTMLInputElement>,
Expand All @@ -45,6 +49,15 @@ const UserInfo = () => {
dispatch(setUserSshKeyByIndex({ index: index, sshKey: value }));
};

const handleCheckboxChange = (
_event: React.FormEvent<HTMLInputElement>,
value: boolean
) => {
dispatch(
setUserAdministratorByIndex({ index: index, isAdministrator: value })
);
};

const stepValidation = {
errors: {},
disabledNext: false,
Expand Down Expand Up @@ -94,6 +107,16 @@ const UserInfo = () => {
Learn more about SSH keys
</Button>
</FormGroup>
<FormGroup>
<Checkbox
label="Administrator"
isChecked={userIsAdministrator}
onChange={(_e, value) => handleCheckboxChange(_e, value)}
aria-label="Administrator"
id="user Administrator"
name="user Administrator"
/>
</FormGroup>
</>
);
};
Expand Down
3 changes: 3 additions & 0 deletions src/Components/CreateImageWizard/utilities/requestMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
};
Expand Down
27 changes: 27 additions & 0 deletions src/store/wizardSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export type ComplianceType = 'openscap' | 'compliance';

export type UserWithAdditionalInfo = {
[K in keyof User]-?: NonNullable<User[K]>;
} & {
isAdministrator: boolean;
};

type UserPayload = {
Expand All @@ -63,6 +65,11 @@ type UserSshKeyPayload = {
sshKey: string;
};

type UserAdministratorPayload = {
index: number;
isAdministrator: boolean;
};

export type wizardState = {
env: {
serverUrl: string;
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -838,6 +850,20 @@ export const wizardSlice = createSlice({
setUserSshKeyByIndex: (state, action: PayloadAction<UserSshKeyPayload>) => {
state.users[action.payload.index].ssh_key = action.payload.sshKey;
},
setUserAdministratorByIndex: (
state,
action: PayloadAction<UserAdministratorPayload>
) => {
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'
);
}
},
},
});

Expand Down Expand Up @@ -910,5 +936,6 @@ export const {
setUserNameByIndex,
setUserPasswordByIndex,
setUserSshKeyByIndex,
setUserAdministratorByIndex,
} = wizardSlice.actions;
export default wizardSlice.reducer;
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ describe('Step Users', () => {
{
name: 'best',
ssh_key: 'ssh-rsa d',
groups: [],
},
],
},
Expand Down
1 change: 1 addition & 0 deletions src/test/fixtures/editMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ export const usersCreateBlueprintRequest: CreateBlueprintRequest = {
{
name: 'best',
ssh_key: 'ssh-rsa d',
groups: [],
},
],
},
Expand Down

0 comments on commit db2f598

Please sign in to comment.