1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { RootState } from "~/store/store";
7 import { ServiceRepository } from "~/services/services";
8 import { setBreadcrumbs } from "~/store/breadcrumbs/breadcrumbs-actions";
9 import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions";
10 import { LinkAccountType, AccountToLink, LinkAccountStatus } from "~/models/link-account";
11 import { saveApiToken, saveUser } from "~/store/auth/auth-action";
12 import { unionize, ofType, UnionOf } from '~/common/unionize';
13 import { UserResource } from "~/models/user";
14 import { GroupResource } from "~/models/group";
15 import { LinkAccountPanelError, OriginatingUser } from "./link-account-panel-reducer";
16 import { login, logout } from "~/store/auth/auth-action";
17 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
18 import { WORKBENCH_LOADING_SCREEN } from '~/store/workbench/workbench-actions';
20 export const linkAccountPanelActions = unionize({
22 targetUser: UserResource | undefined }>(),
24 originatingUser: OriginatingUser | undefined,
25 targetUser: UserResource | undefined,
26 targetUserToken: string | undefined,
27 userToLink: UserResource | undefined,
28 userToLinkToken: string | undefined }>(),
29 LINK_INVALID: ofType<{
30 originatingUser: OriginatingUser | undefined,
31 targetUser: UserResource | undefined,
32 userToLink: UserResource | undefined,
33 error: LinkAccountPanelError }>(),
34 SET_SELECTED_CLUSTER: ofType<{
35 selectedCluster: string }>(),
39 export type LinkAccountPanelAction = UnionOf<typeof linkAccountPanelActions>;
41 function validateLink(userToLink: UserResource, targetUser: UserResource) {
42 if (userToLink.uuid === targetUser.uuid) {
43 return LinkAccountPanelError.SAME_USER;
45 else if (userToLink.isAdmin && !targetUser.isAdmin) {
46 return LinkAccountPanelError.NON_ADMIN;
48 else if (!targetUser.isActive) {
49 return LinkAccountPanelError.INACTIVE;
51 return LinkAccountPanelError.NONE;
54 export const checkForLinkStatus = () =>
55 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
56 const status = services.linkAccountService.getLinkOpStatus();
57 if (status !== undefined) {
59 let msgKind: SnackbarKind;
60 if (status.valueOf() === LinkAccountStatus.CANCELLED) {
61 msg = "Account link cancelled!", msgKind = SnackbarKind.INFO;
63 else if (status.valueOf() === LinkAccountStatus.FAILED) {
64 msg = "Account link failed!", msgKind = SnackbarKind.ERROR;
66 else if (status.valueOf() === LinkAccountStatus.SUCCESS) {
67 msg = "Account link success!", msgKind = SnackbarKind.SUCCESS;
70 msg = "Unknown Error!", msgKind = SnackbarKind.ERROR;
72 dispatch(snackbarActions.OPEN_SNACKBAR({ message: msg, kind: msgKind, hideDuration: 3000 }));
73 services.linkAccountService.removeLinkOpStatus();
77 export const switchUser = (user: UserResource, token: string) =>
78 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
79 dispatch(saveUser(user));
80 dispatch(saveApiToken(token));
83 export const linkFailed = () =>
84 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
85 // If the link fails, switch to the user account that originated the link operation
86 const linkState = getState().linkAccountPanel;
87 if (linkState.userToLink && linkState.userToLinkToken && linkState.targetUser && linkState.targetUserToken) {
88 if (linkState.originatingUser === OriginatingUser.TARGET_USER) {
89 dispatch(switchUser(linkState.targetUser, linkState.targetUserToken));
91 else if ((linkState.originatingUser === OriginatingUser.USER_TO_LINK)) {
92 dispatch(switchUser(linkState.userToLink, linkState.userToLinkToken));
95 services.linkAccountService.removeAccountToLink();
96 services.linkAccountService.saveLinkOpStatus(LinkAccountStatus.FAILED);
100 export const loadLinkAccountPanel = () =>
101 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
102 // If there are remote hosts, set the initial selected cluster by getting the first cluster that isn't the local cluster
103 if (getState().linkAccountPanel.selectedCluster === undefined) {
104 const localCluster = getState().auth.localCluster;
105 let selectedCluster = localCluster;
106 for (const key in getState().auth.remoteHosts) {
107 if (key !== localCluster) {
108 selectedCluster = key;
112 dispatch(linkAccountPanelActions.SET_SELECTED_CLUSTER({ selectedCluster }));
115 // First check if an account link operation has completed
116 dispatch(checkForLinkStatus());
118 // Continue loading the link account panel
119 dispatch(setBreadcrumbs([{ label: 'Link account'}]));
120 const curUser = getState().auth.user;
121 const curToken = getState().auth.apiToken;
122 if (curUser && curToken) {
123 const curUserResource = await services.userService.get(curUser.uuid);
124 const linkAccountData = services.linkAccountService.getAccountToLink();
126 // If there is link account session data, then the user has logged in a second time
127 if (linkAccountData) {
129 // Use the token of the user we are getting data for. This avoids any admin/non-admin permissions
130 // issues since a user will always be able to query the api server for their own user data.
131 dispatch(saveApiToken(linkAccountData.token));
132 const savedUserResource = await services.userService.get(linkAccountData.userUuid);
133 dispatch(saveApiToken(curToken));
136 if (linkAccountData.type === LinkAccountType.ACCESS_OTHER_ACCOUNT || linkAccountData.type === LinkAccountType.ACCESS_OTHER_REMOTE_ACCOUNT) {
138 originatingUser: OriginatingUser.USER_TO_LINK,
139 targetUser: curUserResource,
140 targetUserToken: curToken,
141 userToLink: savedUserResource,
142 userToLinkToken: linkAccountData.token
145 else if (linkAccountData.type === LinkAccountType.ADD_OTHER_LOGIN || linkAccountData.type === LinkAccountType.ADD_LOCAL_TO_REMOTE) {
147 originatingUser: OriginatingUser.TARGET_USER,
148 targetUser: savedUserResource,
149 targetUserToken: linkAccountData.token,
150 userToLink: curUserResource,
151 userToLinkToken: curToken
155 // This should never really happen, but just in case, switch to the user that
156 // originated the linking operation (i.e. the user saved in session data)
157 dispatch(switchUser(savedUserResource, linkAccountData.token));
158 services.linkAccountService.removeAccountToLink();
159 dispatch(linkAccountPanelActions.LINK_INIT({targetUser:savedUserResource}));
162 dispatch(switchUser(params.targetUser, params.targetUserToken));
163 const error = validateLink(params.userToLink, params.targetUser);
164 if (error === LinkAccountPanelError.NONE) {
165 dispatch(linkAccountPanelActions.LINK_LOAD(params));
168 dispatch(linkAccountPanelActions.LINK_INVALID({
169 originatingUser: params.originatingUser,
170 targetUser: params.targetUser,
171 userToLink: params.userToLink,
177 // If there is no link account session data, set the state to invoke the initial UI
178 dispatch(linkAccountPanelActions.LINK_INIT({ targetUser: curUserResource }));
184 export const startLinking = (t: LinkAccountType) =>
185 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
186 const accountToLink = {type: t, userUuid: services.authService.getUuid(), token: services.authService.getApiToken()} as AccountToLink;
187 services.linkAccountService.saveAccountToLink(accountToLink);
189 const auth = getState().auth;
190 const isLocalUser = auth.user!.uuid.substring(0,5) === auth.localCluster;
191 let homeCluster = auth.localCluster;
192 if (isLocalUser && t === LinkAccountType.ACCESS_OTHER_REMOTE_ACCOUNT) {
193 homeCluster = getState().linkAccountPanel.selectedCluster!;
197 dispatch(login(auth.localCluster, homeCluster, auth.remoteHosts));
200 export const getAccountLinkData = () =>
201 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
202 return services.linkAccountService.getAccountToLink();
205 export const cancelLinking = () =>
206 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
207 let user: UserResource | undefined;
209 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
210 // When linking is cancelled switch to the originating user (i.e. the user saved in session data)
211 const linkAccountData = services.linkAccountService.getAccountToLink();
212 if (linkAccountData) {
213 dispatch(saveApiToken(linkAccountData.token));
214 user = await services.userService.get(linkAccountData.userUuid);
215 dispatch(switchUser(user, linkAccountData.token));
219 services.linkAccountService.removeAccountToLink();
220 dispatch(linkAccountPanelActions.LINK_INIT({targetUser:user}));
221 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
225 export const linkAccount = () =>
226 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
227 const linkState = getState().linkAccountPanel;
228 if (linkState.userToLink && linkState.userToLinkToken && linkState.targetUser && linkState.targetUserToken) {
230 // First create a project owned by the target user
231 const projectName = `Migrated from ${linkState.userToLink.email} (${linkState.userToLink.uuid})`;
232 let newGroup: GroupResource;
234 newGroup = await services.projectService.create({
236 ensure_unique_name: true
240 dispatch(linkFailed());
245 // The merge api links the user sending the request into the user
246 // specified in the request, so switch users for this api call
247 dispatch(saveApiToken(linkState.userToLinkToken));
248 await services.linkAccountService.linkAccounts(linkState.targetUserToken, newGroup.uuid);
249 dispatch(switchUser(linkState.targetUser, linkState.targetUserToken));
250 services.linkAccountService.removeAccountToLink();
251 services.linkAccountService.saveLinkOpStatus(LinkAccountStatus.SUCCESS);
255 // If the link operation fails, delete the previously made project
257 dispatch(saveApiToken(linkState.targetUserToken));
258 await services.projectService.delete(newGroup.uuid);
261 dispatch(linkFailed());