shows user type fields only for sys admins

This commit is contained in:
Alex Corey
2021-06-14 12:09:21 -04:00
committed by Shane McDonald
parent c76a7d638f
commit 1c70773cc2
2 changed files with 41 additions and 17 deletions

View File

@@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { Formik, useField, useFormikContext } from 'formik'; import { Formik, useField, useFormikContext } from 'formik';
import { Form, FormGroup } from '@patternfly/react-core'; import { Form, FormGroup } from '@patternfly/react-core';
import { useConfig } from '../../../contexts/Config';
import AnsibleSelect from '../../../components/AnsibleSelect'; import AnsibleSelect from '../../../components/AnsibleSelect';
import FormActionGroup from '../../../components/FormActionGroup/FormActionGroup'; import FormActionGroup from '../../../components/FormActionGroup/FormActionGroup';
import FormField, { import FormField, {
@@ -16,7 +17,7 @@ import { FormColumnLayout } from '../../../components/FormLayout';
function UserFormFields({ user }) { function UserFormFields({ user }) {
const { setFieldValue, setFieldTouched } = useFormikContext(); const { setFieldValue, setFieldTouched } = useFormikContext();
const { me = {} } = useConfig();
const ldapUser = user.ldap_dn; const ldapUser = user.ldap_dn;
const socialAuthUser = user.auth?.length > 0; const socialAuthUser = user.auth?.length > 0;
const externalAccount = user.external_account; const externalAccount = user.external_account;
@@ -119,22 +120,24 @@ function UserFormFields({ user }) {
validate={required(t`Select a value for this field`)} validate={required(t`Select a value for this field`)}
/> />
)} )}
<FormGroup {me.is_superuser && (
fieldId="user-type" <FormGroup
helperTextInvalid={userTypeMeta.error} fieldId="user-type"
isRequired helperTextInvalid={userTypeMeta.error}
validated={ isRequired
!userTypeMeta.touched || !userTypeMeta.error ? 'default' : 'error' validated={
} !userTypeMeta.touched || !userTypeMeta.error ? 'default' : 'error'
label={t`User Type`} }
> label={t`User Type`}
<AnsibleSelect >
isValid={!userTypeMeta.touched || !userTypeMeta.error} <AnsibleSelect
id="user-type" isValid={!userTypeMeta.touched || !userTypeMeta.error}
data={userTypeOptions} id="user-type"
{...userTypeField} data={userTypeOptions}
/> {...userTypeField}
</FormGroup> />
</FormGroup>
)}
</> </>
); );
} }

View File

@@ -230,4 +230,25 @@ describe('<UserForm />', () => {
wrapper.find('button[aria-label="Cancel"]').invoke('onClick')(); wrapper.find('button[aria-label="Cancel"]').invoke('onClick')();
expect(handleCancel).toBeCalled(); expect(handleCancel).toBeCalled();
}); });
test('should not show user type field', async () => {
const handleCancel = jest.fn();
await act(async () => {
wrapper = mountWithContexts(
<UserForm
user={mockData}
handleSubmit={jest.fn()}
handleCancel={handleCancel}
/>,
{
context: {
config: {
me: { is_superuser: false },
},
},
}
);
});
expect(wrapper.find('FormGroup[label="User Type"]')).toHaveLength(0);
});
}); });