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 { getUserUuid } from "common/getuser";
8 import { ServiceRepository, createServices, setAuthorizationHeader } from "services/services";
9 import { setBreadcrumbs } from "store/breadcrumbs/breadcrumbs-actions";
10 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
11 import { LinkAccountType, AccountToLink, LinkAccountStatus } from "models/link-account";
12 import { authActions, getConfig } from "store/auth/auth-action";
13 import { unionize, ofType, UnionOf } from 'common/unionize';
14 import { UserResource } from "models/user";
15 import { GroupResource } from "models/group";
16 import { LinkAccountPanelError, OriginatingUser } from "./link-account-panel-reducer";
17 import { login, logout } from "store/auth/auth-action";
18 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
19 import { WORKBENCH_LOADING_SCREEN } from 'store/workbench/workbench-actions';
21 export const linkAccountPanelActions = unionize({
23 targetUser: UserResource | undefined
26 originatingUser: OriginatingUser | undefined,
27 targetUser: UserResource | undefined,
28 targetUserToken: string | undefined,
29 userToLink: UserResource | undefined,
30 userToLinkToken: string | undefined
32 LINK_INVALID: ofType<{
33 originatingUser: OriginatingUser | undefined,
34 targetUser: UserResource | undefined,
35 userToLink: UserResource | undefined,
36 error: LinkAccountPanelError
38 SET_SELECTED_CLUSTER: ofType<{
39 selectedCluster: string
41 SET_IS_PROCESSING: ofType<{
47 export type LinkAccountPanelAction = UnionOf<typeof linkAccountPanelActions>;
49 function validateLink(userToLink: UserResource, targetUser: UserResource) {
50 if (userToLink.uuid === targetUser.uuid) {
51 return LinkAccountPanelError.SAME_USER;
53 else if (userToLink.isAdmin && !targetUser.isAdmin) {
54 return LinkAccountPanelError.NON_ADMIN;
56 else if (!targetUser.isActive) {
57 return LinkAccountPanelError.INACTIVE;
59 return LinkAccountPanelError.NONE;
62 const newServices = (dispatch: Dispatch<any>, token: string) => {
63 const config = dispatch<any>(getConfig);
64 const svc = createServices(config, { progressFn: () => { }, errorFn: () => { } });
65 setAuthorizationHeader(svc, token);
69 export const checkForLinkStatus = () =>
70 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
71 const status = services.linkAccountService.getLinkOpStatus();
72 if (status !== undefined) {
74 let msgKind: SnackbarKind;
75 if (status.valueOf() === LinkAccountStatus.CANCELLED) {
76 msg = "Account link cancelled!";
77 msgKind = SnackbarKind.INFO;
79 else if (status.valueOf() === LinkAccountStatus.FAILED) {
80 msg = "Account link failed!";
81 msgKind = SnackbarKind.ERROR;
83 else if (status.valueOf() === LinkAccountStatus.SUCCESS) {
84 msg = "Account link success!";
85 msgKind = SnackbarKind.SUCCESS;
88 msg = "Unknown Error!";
89 msgKind = SnackbarKind.ERROR;
91 dispatch(snackbarActions.OPEN_SNACKBAR({ message: msg, kind: msgKind, hideDuration: 3000 }));
92 services.linkAccountService.removeLinkOpStatus();
96 export const switchUser = (user: UserResource, token: string) =>
97 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
98 dispatch(authActions.INIT_USER({ user, token }));
101 export const linkFailed = () =>
102 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
103 // If the link fails, switch to the user account that originated the link operation
104 const linkState = getState().linkAccountPanel;
105 if (linkState.userToLink && linkState.userToLinkToken && linkState.targetUser && linkState.targetUserToken) {
106 if (linkState.originatingUser === OriginatingUser.TARGET_USER) {
107 dispatch(switchUser(linkState.targetUser, linkState.targetUserToken));
109 else if ((linkState.originatingUser === OriginatingUser.USER_TO_LINK)) {
110 dispatch(switchUser(linkState.userToLink, linkState.userToLinkToken));
113 services.linkAccountService.removeAccountToLink();
114 services.linkAccountService.saveLinkOpStatus(LinkAccountStatus.FAILED);
115 window.location.reload();
118 export const loadLinkAccountPanel = () =>
119 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
121 // If there are remote hosts, set the initial selected cluster by getting the first cluster that isn't the local cluster
122 if (getState().linkAccountPanel.selectedCluster === undefined) {
123 const localCluster = getState().auth.localCluster;
124 let selectedCluster = localCluster;
125 for (const key in getState().auth.remoteHosts) {
126 if (key !== localCluster) {
127 selectedCluster = key;
131 dispatch(linkAccountPanelActions.SET_SELECTED_CLUSTER({ selectedCluster }));
134 // First check if an account link operation has completed
135 dispatch(checkForLinkStatus());
137 // Continue loading the link account panel
138 dispatch(setBreadcrumbs([{ label: 'Link account' }]));
139 const curUser = getState().auth.user;
140 const curToken = getState().auth.apiToken;
141 if (curUser && curToken) {
143 // If there is link account session data, then the user has logged in a second time
144 const linkAccountData = services.linkAccountService.getAccountToLink();
145 if (linkAccountData) {
147 dispatch(linkAccountPanelActions.SET_IS_PROCESSING({ isProcessing: true }));
148 const curUserResource = await services.userService.get(curUser.uuid);
150 // Use the token of the user we are getting data for. This avoids any admin/non-admin permissions
151 // issues since a user will always be able to query the api server for their own user data.
152 const svc = newServices(dispatch, linkAccountData.token);
153 const savedUserResource = await svc.userService.get(linkAccountData.userUuid);
156 if (linkAccountData.type === LinkAccountType.ACCESS_OTHER_ACCOUNT || linkAccountData.type === LinkAccountType.ACCESS_OTHER_REMOTE_ACCOUNT) {
158 originatingUser: OriginatingUser.USER_TO_LINK,
159 targetUser: curUserResource,
160 targetUserToken: curToken,
161 userToLink: savedUserResource,
162 userToLinkToken: linkAccountData.token
165 else if (linkAccountData.type === LinkAccountType.ADD_OTHER_LOGIN || linkAccountData.type === LinkAccountType.ADD_LOCAL_TO_REMOTE) {
167 originatingUser: OriginatingUser.TARGET_USER,
168 targetUser: savedUserResource,
169 targetUserToken: linkAccountData.token,
170 userToLink: curUserResource,
171 userToLinkToken: curToken
175 throw new Error("Unknown link account type");
178 dispatch(switchUser(params.targetUser, params.targetUserToken));
179 const error = validateLink(params.userToLink, params.targetUser);
180 if (error === LinkAccountPanelError.NONE) {
181 dispatch(linkAccountPanelActions.LINK_LOAD(params));
184 dispatch(linkAccountPanelActions.LINK_INVALID({
185 originatingUser: params.originatingUser,
186 targetUser: params.targetUser,
187 userToLink: params.userToLink,
194 // If there is no link account session data, set the state to invoke the initial UI
195 const curUserResource = await services.userService.get(curUser.uuid);
196 dispatch(linkAccountPanelActions.LINK_INIT({ targetUser: curUserResource }));
202 dispatch(linkFailed());
205 dispatch(linkAccountPanelActions.SET_IS_PROCESSING({ isProcessing: false }));
209 export const startLinking = (t: LinkAccountType) =>
210 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
211 const userUuid = getUserUuid(getState());
212 if (!userUuid) { return; }
213 const accountToLink = { type: t, userUuid, token: services.authService.getApiToken() } as AccountToLink;
214 services.linkAccountService.saveAccountToLink(accountToLink);
216 const auth = getState().auth;
217 const isLocalUser = auth.user!.uuid.substring(0, 5) === auth.localCluster;
218 let homeCluster = auth.localCluster;
219 if (isLocalUser && t === LinkAccountType.ACCESS_OTHER_REMOTE_ACCOUNT) {
220 homeCluster = getState().linkAccountPanel.selectedCluster!;
224 dispatch(login(auth.localCluster, homeCluster, auth.loginCluster, auth.remoteHosts));
227 export const getAccountLinkData = () =>
228 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
229 return services.linkAccountService.getAccountToLink();
232 export const cancelLinking = (reload: boolean = false) =>
233 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
234 let user: UserResource | undefined;
236 // When linking is cancelled switch to the originating user (i.e. the user saved in session data)
237 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
238 const linkAccountData = services.linkAccountService.getAccountToLink();
239 if (linkAccountData) {
240 services.linkAccountService.removeAccountToLink();
241 const svc = newServices(dispatch, linkAccountData.token);
242 user = await svc.userService.get(linkAccountData.userUuid);
243 dispatch(switchUser(user, linkAccountData.token));
244 services.linkAccountService.saveLinkOpStatus(LinkAccountStatus.CANCELLED);
249 window.location.reload();
252 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
257 export const linkAccount = () =>
258 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
259 const linkState = getState().linkAccountPanel;
260 if (linkState.userToLink && linkState.userToLinkToken && linkState.targetUser && linkState.targetUserToken) {
262 // First create a project owned by the target user
263 const projectName = `Migrated from ${linkState.userToLink.email} (${linkState.userToLink.uuid})`;
264 let newGroup: GroupResource;
266 newGroup = await services.projectService.create({
268 ensure_unique_name: true
272 dispatch(linkFailed());
277 // The merge api links the user sending the request into the user
278 // specified in the request, so change the authorization header accordingly
279 const svc = newServices(dispatch, linkState.userToLinkToken);
280 await svc.linkAccountService.linkAccounts(linkState.targetUserToken, newGroup.uuid);
281 dispatch(switchUser(linkState.targetUser, linkState.targetUserToken));
282 services.linkAccountService.removeAccountToLink();
283 services.linkAccountService.saveLinkOpStatus(LinkAccountStatus.SUCCESS);
284 window.location.reload();
287 // If the link operation fails, delete the previously made project
289 const svc = newServices(dispatch, linkState.targetUserToken);
290 await svc.projectService.delete(newGroup.uuid);
293 dispatch(linkFailed());