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

View File

@ -230,4 +230,25 @@ describe('<UserForm />', () => {
wrapper.find('button[aria-label="Cancel"]').invoke('onClick')();
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);
});
});