Skip to content

Commit b0ac008

Browse files
fix: [UIE-10253] - IAM Delegate: hide company name for users with no permission
1 parent b220502 commit b0ac008

File tree

6 files changed

+64
-74
lines changed

6 files changed

+64
-74
lines changed

packages/manager/src/OAuth/oauth.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '@linode/utilities';
66
import * as Sentry from '@sentry/react';
77

8-
import { clearStorage, clearUserInput, storage } from 'src/utilities/storage';
8+
import { clearUserInput, storage } from 'src/utilities/storage';
99

1010
import { getAppRoot, getClientId, getLoginURL } from './constants';
1111
import { generateCodeChallenge, generateCodeVerifier } from './pkce';
@@ -45,7 +45,6 @@ function clearNonceAndCodeVerifierFromLocalStorage() {
4545
function clearAllAuthDataFromLocalStorage() {
4646
clearNonceAndCodeVerifierFromLocalStorage();
4747
clearAuthDataFromLocalStorage();
48-
clearStorage('switch_account/company_name');
4948
}
5049

5150
export function clearStorageAndRedirectToLogout() {
@@ -106,7 +105,6 @@ export async function logout() {
106105

107106
clearUserInput();
108107
clearAuthDataFromLocalStorage();
109-
clearStorage('switch_account/company_name');
110108

111109
if (token) {
112110
const tokenWithoutPrefix = token.split(' ')[1];

packages/manager/src/features/Account/SwitchAccountDrawer.tsx

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,21 @@ import { useSwitchToParentAccount } from 'src/features/Account/SwitchAccounts/us
2020
import { setTokenInLocalStorage } from 'src/features/Account/SwitchAccounts/utils';
2121
import { useIsIAMDelegationEnabled } from 'src/features/IAM/hooks/useIsIAMEnabled';
2222
import { sendSwitchToParentAccountEvent } from 'src/utilities/analytics/customEventAnalytics';
23-
import { getStorage, setStorage, storage } from 'src/utilities/storage';
23+
import { getStorage, storage } from 'src/utilities/storage';
2424

2525
import { ChildAccountList } from './SwitchAccounts/ChildAccountList';
2626
import { ChildAccountsTable } from './SwitchAccounts/ChildAccountsTable';
2727
import { updateParentTokenInLocalStorage } from './SwitchAccounts/utils';
2828

2929
import type { APIError, Filter, UserType } from '@linode/api-v4';
3030

31-
const SWITCH_ACCOUNT_COMPANY_KEY = 'switch_account/company_name';
32-
3331
interface Props {
3432
onClose: () => void;
3533
open: boolean;
3634
userType: undefined | UserType;
3735
}
3836

3937
interface HandleSwitchToChildAccountProps {
40-
company?: string;
4138
currentTokenWithBearer?: string;
4239
euuid: string;
4340
event: React.MouseEvent<HTMLElement>;
@@ -131,12 +128,11 @@ export const SwitchAccountDrawer = (props: Props) => {
131128
event,
132129
onClose,
133130
userType,
134-
company,
135131
}: HandleSwitchToChildAccountProps) => {
136132
const isProxyOrDelegateUserType =
137133
userType === 'proxy' || userType === 'delegate';
134+
138135
try {
139-
setStorage(SWITCH_ACCOUNT_COMPANY_KEY, company ?? '');
140136
if (isProxyOrDelegateUserType) {
141137
// Revoke proxy token before switching accounts.
142138
await revokeToken().catch(() => {
@@ -166,7 +162,6 @@ export const SwitchAccountDrawer = (props: Props) => {
166162
location.replace('/linodes');
167163
} catch {
168164
// Error is handled by createTokenError.
169-
setStorage(SWITCH_ACCOUNT_COMPANY_KEY, '');
170165
}
171166
},
172167
[createToken, isProxyUserType, updateCurrentToken, revokeToken]
@@ -248,7 +243,7 @@ export const SwitchAccountDrawer = (props: Props) => {
248243
.
249244
</Typography>
250245

251-
{hasError && (
246+
{hasError ? (
252247
<Stack alignItems="center" gap={1} justifyContent="center">
253248
<ErrorStateCloud />
254249
<Typography>Unable to load data.</Typography>
@@ -265,8 +260,7 @@ export const SwitchAccountDrawer = (props: Props) => {
265260
Try again
266261
</Button>
267262
</Stack>
268-
)}
269-
{!hasError && (
263+
) : (
270264
<>
271265
<DebouncedSearchTextField
272266
clearable
@@ -292,51 +286,52 @@ export const SwitchAccountDrawer = (props: Props) => {
292286
No search results
293287
</Typography>
294288
)}
289+
290+
{isIAMDelegationEnabled && (
291+
<ChildAccountsTable
292+
childAccounts={childAccounts}
293+
currentTokenWithBearer={
294+
isProxyOrDelegateUserType
295+
? currentParentTokenWithBearer
296+
: currentTokenWithBearer
297+
}
298+
filter={filter}
299+
isLoading={isLoading}
300+
isSwitchingChildAccounts={isSwitchingChildAccounts}
301+
onClose={onClose}
302+
onPageChange={handlePageChange}
303+
onPageSizeChange={handlePageSizeChange}
304+
onSwitchAccount={handleSwitchToChildAccount}
305+
page={page}
306+
pageSize={pageSize}
307+
setIsSwitchingChildAccounts={setIsSwitchingChildAccounts}
308+
totalResults={delegatedChildAccounts?.results || 0}
309+
userType={userType}
310+
/>
311+
)}
312+
{!isIAMDelegationEnabled && (
313+
<ChildAccountList
314+
childAccounts={childAccounts}
315+
currentTokenWithBearer={
316+
isProxyOrDelegateUserType
317+
? currentParentTokenWithBearer
318+
: currentTokenWithBearer
319+
}
320+
fetchNextPage={fetchNextPage}
321+
filter={filter}
322+
hasNextPage={hasNextPage}
323+
isFetchingNextPage={isFetchingNextPage}
324+
isLoading={isLoading}
325+
isSwitchingChildAccounts={isSwitchingChildAccounts}
326+
onClose={onClose}
327+
onSwitchAccount={handleSwitchToChildAccount}
328+
refetchFn={refetchFn}
329+
setIsSwitchingChildAccounts={setIsSwitchingChildAccounts}
330+
userType={userType}
331+
/>
332+
)}
295333
</>
296334
)}
297-
{isIAMDelegationEnabled && (
298-
<ChildAccountsTable
299-
childAccounts={childAccounts}
300-
currentTokenWithBearer={
301-
isProxyOrDelegateUserType
302-
? currentParentTokenWithBearer
303-
: currentTokenWithBearer
304-
}
305-
filter={filter}
306-
isLoading={isLoading}
307-
isSwitchingChildAccounts={isSwitchingChildAccounts}
308-
onClose={onClose}
309-
onPageChange={handlePageChange}
310-
onPageSizeChange={handlePageSizeChange}
311-
onSwitchAccount={handleSwitchToChildAccount}
312-
page={page}
313-
pageSize={pageSize}
314-
setIsSwitchingChildAccounts={setIsSwitchingChildAccounts}
315-
totalResults={delegatedChildAccounts?.results || 0}
316-
userType={userType}
317-
/>
318-
)}
319-
{!isIAMDelegationEnabled && (
320-
<ChildAccountList
321-
childAccounts={childAccounts}
322-
currentTokenWithBearer={
323-
isProxyOrDelegateUserType
324-
? currentParentTokenWithBearer
325-
: currentTokenWithBearer
326-
}
327-
fetchNextPage={fetchNextPage}
328-
filter={filter}
329-
hasNextPage={hasNextPage}
330-
isFetchingNextPage={isFetchingNextPage}
331-
isLoading={isLoading}
332-
isSwitchingChildAccounts={isSwitchingChildAccounts}
333-
onClose={onClose}
334-
onSwitchAccount={handleSwitchToChildAccount}
335-
refetchFn={refetchFn}
336-
setIsSwitchingChildAccounts={setIsSwitchingChildAccounts}
337-
userType={userType}
338-
/>
339-
)}
340335
</Drawer>
341336
);
342337
};

packages/manager/src/features/Account/SwitchAccounts/ChildAccountsTable.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ export interface ChildAccountsTableProps {
2626
euuid,
2727
event,
2828
onClose,
29-
company,
3029
userType,
3130
}: {
32-
company?: string;
3331
currentTokenWithBearer?: string;
3432
euuid: string;
3533
event: React.MouseEvent<HTMLElement>;
@@ -109,7 +107,6 @@ export const ChildAccountsTable = (props: ChildAccountsTableProps) => {
109107
onSwitchAccount({
110108
currentTokenWithBearer,
111109
euuid: childAccount.euuid,
112-
company: childAccount.company,
113110
event,
114111
onClose,
115112
userType,

packages/manager/src/features/Account/SwitchAccounts/useSwitchToParentAccount.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22

33
import { PARENT_USER_SESSION_EXPIRED } from 'src/features/Account/constants';
4-
import { clearStorage, setStorage } from 'src/utilities/storage';
4+
import { setStorage } from 'src/utilities/storage';
55

66
import { useParentChildAuthentication } from './useParentChildAuthentication';
77

@@ -38,8 +38,7 @@ export const useSwitchToParentAccount = ({
3838

3939
// Flag to prevent multiple clicks on the switch account button.
4040
setSubmitting(true);
41-
// Clean up the company name in storage to prevent it from being used in the parent account after switching back from a child account.
42-
clearStorage('switch_account/company_name');
41+
4342
try {
4443
// Revoke proxy or delegate token before switching to parent account.
4544
await revokeToken().catch(() => {

packages/manager/src/features/TopMenu/UserMenu/UserMenu.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ export const UserMenu = React.memo(() => {
4343
const open = Boolean(anchorEl);
4444
const id = open ? 'user-menu-popover' : undefined;
4545

46-
const switchAccountCompanyName = getStorage('switch_account/company_name');
47-
4846
const companyNameOrEmail = getCompanyNameOrEmail({
49-
company: account?.company ? account?.company : switchAccountCompanyName,
47+
company: account?.company,
5048
profile,
5149
});
5250

@@ -85,9 +83,10 @@ export const UserMenu = React.memo(() => {
8583
setStorage('is_delegate_user_type', 'true');
8684
}
8785

88-
enqueueSnackbar(`Account switched to ${companyNameOrEmail}.`, {
89-
variant: 'success',
90-
});
86+
const message = companyNameOrEmail
87+
? `Account switched to ${companyNameOrEmail}.`
88+
: 'Account switched.';
89+
enqueueSnackbar(message, { variant: 'success' });
9190
}
9291
}, [
9392
isProxyOrDelegateUserType,

packages/manager/src/features/TopMenu/UserMenu/UserMenuPopover.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ export const UserMenuPopover = (props: UserMenuPopoverProps) => {
121121
}
122122
: undefined;
123123

124-
const switchAccountCompanyName = getStorage('switch_account/company_name');
125124
const companyNameOrEmail = getCompanyNameOrEmail({
126-
company: account?.company ? account?.company : switchAccountCompanyName,
125+
company: account?.company,
127126
profile,
128127
});
129128
const { data: parentProfile } = useProfile({ headers: proxyHeaders });
@@ -237,8 +236,11 @@ export const UserMenuPopover = (props: UserMenuPopoverProps) => {
237236
gap={(theme) => theme.tokens.spacing.S16}
238237
minWidth={250}
239238
>
240-
<Stack display="flex" gap={(theme) => theme.tokens.spacing.S8}>
241-
{canSwitchBetweenParentOrProxyAccount && (
239+
<Stack
240+
display="flex"
241+
gap={(theme) => (companyNameOrEmail ? theme.tokens.spacing.S8 : 0)}
242+
>
243+
{canSwitchBetweenParentOrProxyAccount && companyNameOrEmail && (
242244
<Typography
243245
sx={(theme) => ({
244246
color: theme.tokens.alias.Content.Text.Primary.Default,
@@ -255,8 +257,8 @@ export const UserMenuPopover = (props: UserMenuPopoverProps) => {
255257
overflowWrap: 'break-word',
256258
})}
257259
>
258-
{canSwitchBetweenParentOrProxyAccount && companyNameOrEmail
259-
? companyNameOrEmail
260+
{canSwitchBetweenParentOrProxyAccount
261+
? companyNameOrEmail || null
260262
: userName}
261263
</Typography>
262264
{canSwitchBetweenParentOrProxyAccount && (

0 commit comments

Comments
 (0)