Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upcoming: [M3-7530] - Restrict proxy users from updating username/email #10103

Merged
merged 8 commits into from
Jan 29, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Upcoming Features
---

Restrict proxy users from updating username/email ([#10103](https://github.com/linode/manager/pull/10103))
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Box, BoxProps } from '../Box';

interface ActionButtonsProps extends ButtonProps {
'data-node-idx'?: number;
'data-testid'?: string;
'data-qa-form-data-loading'?: boolean;
'data-testid'?: string;
label: string;
}

Expand Down Expand Up @@ -78,9 +78,6 @@ const StyledBox = styled(Box)(({ theme: { spacing } }) => ({
'& > :only-child': {
marginRight: 0,
},
'& > button': {
marginBottom: spacing(1),
},
justifyContent: 'flex-end',
marginTop: spacing(1),
paddingBottom: spacing(1),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { APIError } from '@linode/api-v4/lib/types';
import { Theme } from '@mui/material/styles';
import * as React from 'react';

import { ActionsPanel } from 'src/components/ActionsPanel/ActionsPanel';
Expand Down Expand Up @@ -69,9 +68,11 @@ export const SingleTextFieldForm = React.memo((props: Props) => {
<Box
sx={(theme) => ({
[theme.breakpoints.down('md')]: {
alignItems: 'flex-start',
flexDirection: 'column',
},
})}
alignItems="flex-end"
display="flex"
justifyContent="space-between"
>
Expand All @@ -88,7 +89,6 @@ export const SingleTextFieldForm = React.memo((props: Props) => {
label={label}
onBlur={(e) => setValue(e.target.value)}
onChange={(e) => setValue(e.target.value)}
tooltipText={tooltipText ? tooltipText : undefined}
trimmed={trimmed}
value={value}
/>
Expand All @@ -98,12 +98,17 @@ export const SingleTextFieldForm = React.memo((props: Props) => {
label: `Update ${label}`,
loading: submitting,
onClick: handleSubmit,
sx: (theme: Theme) => ({
sx: {
margin: '0',
minWidth: 180,
[theme.breakpoints.up('md')]: {
marginTop: 2,
},
}),
},
tooltipText:
tooltipText && typeof tooltipText === 'string'
? tooltipText
: undefined,
}}
sx={{
padding: 0,
}}
/>
</Box>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { screen } from '@testing-library/react';
import React from 'react';

import { profileFactory } from 'src/factories/profile';
import { DisplaySettings } from 'src/features/Profile/DisplaySettings/DisplaySettings';
import { renderWithTheme } from 'src/utilities/testHelpers';

const queryMocks = vi.hoisted(() => ({
useProfile: vi.fn().mockReturnValue({}),
}));

vi.mock('src/queries/profile', async () => {
const actual = await vi.importActual('src/queries/profile');
return {
...actual,
useProfile: queryMocks.useProfile,
};
});

describe('DisplaySettings component', () => {
it('should disable SingleTextFieldForm components based on isProxyUser', () => {
queryMocks.useProfile.mockReturnValue({
data: profileFactory.build({ user_type: 'proxy' }),
});

renderWithTheme(<DisplaySettings />);

const usernameInput = screen.getByLabelText('Username');
expect(usernameInput).toHaveAttribute('disabled');

const emailInput = screen.getByLabelText('Email');
expect(emailInput).toHaveAttribute('disabled');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const DisplaySettings = () => {
const location = useLocation<{ focusEmail: boolean }>();
const emailRef = React.createRef<HTMLInputElement>();

const isProxyUser = profile?.user_type === 'proxy';

React.useEffect(() => {
if (location.state?.focusEmail && emailRef.current) {
emailRef.current.focus();
Expand Down Expand Up @@ -63,6 +65,8 @@ export const DisplaySettings = () => {
</>
);

const restrictedProxyUserTooltip = 'Proxy users cannot update this field.';

return (
<Paper>
<Box
Expand Down Expand Up @@ -102,9 +106,11 @@ export const DisplaySettings = () => {
tooltipText={
profile?.restricted
? 'Restricted users cannot update their username. Please contact an account administrator.'
: isProxyUser
? restrictedProxyUserTooltip
: undefined
}
disabled={profile?.restricted}
disabled={profile?.restricted || isProxyUser}
initialValue={profile?.username}
key={usernameResetToken}
label="Username"
Expand All @@ -125,11 +131,13 @@ export const DisplaySettings = () => {
refetch();
}
}}
disabled={isProxyUser}
initialValue={profile?.email}
inputRef={emailRef}
key={emailResetToken}
label="Email"
submitForm={updateEmail}
tooltipText={isProxyUser ? restrictedProxyUserTooltip : undefined}
trimmed
type="email"
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Theme, styled } from '@mui/material/styles';
import { styled } from '@mui/material/styles';
import { DateTime } from 'luxon';
import { useSnackbar } from 'notistack';
import * as React from 'react';
Expand Down Expand Up @@ -86,7 +86,17 @@ export const TimezoneForm = (props: Props) => {
</Typography>
</StyledLoggedInAsCustomerNotice>
) : null}
<StyledRootContainer display="flex" justifyContent="space-between">
<StyledRootContainer
sx={(theme) => ({
[theme.breakpoints.down('md')]: {
alignItems: 'flex-start',
flexDirection: 'column',
},
})}
alignItems="flex-end"
display="flex"
justifyContent="space-between"
>
<Select
data-qa-tz-select
defaultValue={defaultTimeZone}
Expand All @@ -104,10 +114,13 @@ export const TimezoneForm = (props: Props) => {
loading: isLoading,
onClick: onSubmit,
sx: {
marginTop: (theme: Theme) => (theme.breakpoints.up('md') ? 2 : 0),
margin: '0',
minWidth: 180,
},
}}
sx={{
padding: 0,
}}
/>
</StyledRootContainer>
</>
Expand Down