From d5f11bb2fbb20c85e8ef857caedd58adf333e522 Mon Sep 17 00:00:00 2001 From: Stephen Smith Date: Tue, 29 Mar 2022 09:16:30 -0400 Subject: [PATCH] 18559: Add better error handling for loginAs action Arvados-DCO-1.1-Signed-off-by: Stephen Smith --- src/store/users/users-actions.ts | 36 +++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/store/users/users-actions.ts b/src/store/users/users-actions.ts index c34c7f44..e3c965bd 100644 --- a/src/store/users/users-actions.ts +++ b/src/store/users/users-actions.ts @@ -40,13 +40,35 @@ export const openUserAttributes = (uuid: string) => export const loginAs = (uuid: string) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { - const { resources } = getState(); - const data = getResource(uuid)(resources); - const client = await services.apiClientAuthorizationService.create({ ownerUuid: uuid }); - if (data) { - dispatch(authActions.INIT_USER({ user: data, token: getTokenV2(client) })); - window.location.reload(); - dispatch(navigateToRootProject); + const userUuid = getUserUuid(getState()); + if (userUuid === uuid) { + dispatch(snackbarActions.OPEN_SNACKBAR({ + message: 'You are already logged in as this user', + kind: SnackbarKind.WARNING + })); + } else { + try { + const { resources } = getState(); + const data = getResource(uuid)(resources); + const client = await services.apiClientAuthorizationService.create({ ownerUuid: uuid }, false); + if (data) { + dispatch(authActions.INIT_USER({ user: data, token: getTokenV2(client) })); + window.location.reload(); + dispatch(navigateToRootProject); + } + } catch (e) { + if (e.status === 403) { + dispatch(snackbarActions.OPEN_SNACKBAR({ + message: 'You do not have permission to login as this user', + kind: SnackbarKind.WARNING + })); + } else { + dispatch(snackbarActions.OPEN_SNACKBAR({ + message: 'Failed to login as this user', + kind: SnackbarKind.ERROR + })); + } + } } }; -- 2.30.2