Update package build
[arvados-workbench2.git] / src / views / link-account-panel / link-account-panel-root.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import {
7     StyleRulesCallback,
8     WithStyles,
9     withStyles,
10     Card,
11     CardContent,
12     Button,
13     Grid,
14     Select,
15     CircularProgress
16 } from '@material-ui/core';
17 import { ArvadosTheme } from '~/common/custom-theme';
18 import { UserResource } from "~/models/user";
19 import { LinkAccountType } from "~/models/link-account";
20 import { formatDate } from "~/common/formatters";
21 import { LinkAccountPanelStatus, LinkAccountPanelError } from "~/store/link-account-panel/link-account-panel-reducer";
22 import { Config } from '~/common/config';
23
24 type CssRules = 'root';
25
26 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
27     root: {
28         width: '100%',
29         overflow: 'auto'
30     }
31 });
32
33 export interface LinkAccountPanelRootDataProps {
34     targetUser?: UserResource;
35     userToLink?: UserResource;
36     remoteHostsConfig: { [key: string]: Config };
37     hasRemoteHosts: boolean;
38     localCluster: string;
39     loginCluster: string;
40     status: LinkAccountPanelStatus;
41     error: LinkAccountPanelError;
42     selectedCluster?: string;
43     isProcessing: boolean;
44 }
45
46 export interface LinkAccountPanelRootActionProps {
47     startLinking: (type: LinkAccountType) => void;
48     cancelLinking: () => void;
49     linkAccount: () => void;
50     setSelectedCluster: (cluster: string) => void;
51 }
52
53 function displayUser(user: UserResource, showCreatedAt: boolean = false, showCluster: boolean = false) {
54     const disp = [];
55     disp.push(<span><b>{user.email}</b> ({user.username}, {user.uuid})</span>);
56     if (showCluster) {
57         const homeCluster = user.uuid.substr(0, 5);
58         disp.push(<span> hosted on cluster <b>{homeCluster}</b> and </span>);
59     }
60     if (showCreatedAt) {
61         disp.push(<span> created on <b>{formatDate(user.createdAt)}</b></span>);
62     }
63     return disp;
64 }
65
66 function isLocalUser(uuid: string, localCluster: string) {
67     return uuid.substring(0, 5) === localCluster;
68 }
69
70 type LinkAccountPanelRootProps = LinkAccountPanelRootDataProps & LinkAccountPanelRootActionProps & WithStyles<CssRules>;
71
72 export const LinkAccountPanelRoot = withStyles(styles)(
73     ({ classes, targetUser, userToLink, status, isProcessing, error, startLinking, cancelLinking, linkAccount,
74         remoteHostsConfig, hasRemoteHosts, selectedCluster, setSelectedCluster, localCluster, loginCluster }: LinkAccountPanelRootProps) => {
75         return <Card className={classes.root}>
76             <CardContent>
77                 {isProcessing && <Grid container item direction="column" alignContent="center" spacing={24}>
78                     <Grid item>
79                         Loading user info. Please wait.
80                </Grid>
81                     <Grid item style={{ alignSelf: 'center' }}>
82                         <CircularProgress />
83                     </Grid>
84                 </Grid>}
85                 {!isProcessing && status === LinkAccountPanelStatus.INITIAL && targetUser && <div>
86                     {isLocalUser(targetUser.uuid, localCluster) ? <Grid container spacing={24}>
87                         <Grid container item direction="column" spacing={24}>
88                             <Grid item>
89                                 You are currently logged in as {displayUser(targetUser, true)}
90                             </Grid>
91                             <Grid item>
92                                 You can link Arvados accounts. After linking, either login will take you to the same account.
93                        </Grid >
94                         </Grid>
95                         <Grid container item direction="row" spacing={24}>
96                             <Grid item>
97                                 <Button disabled={!targetUser.isActive} color="primary" variant="contained" onClick={() => startLinking(LinkAccountType.ADD_OTHER_LOGIN)}>
98                                     Add another login to this account
99                            </Button>
100                             </Grid>
101                             <Grid item>
102                                 <Button color="primary" variant="contained" onClick={() => startLinking(LinkAccountType.ACCESS_OTHER_ACCOUNT)}>
103                                     Use this login to access another account
104                            </Button>
105                             </Grid>
106                         </Grid>
107                         {hasRemoteHosts && selectedCluster && <Grid container item direction="column" spacing={24}>
108                             <Grid item>
109                                 You can also link {displayUser(targetUser, false)} with an account from a remote cluster.
110                        </Grid>
111                             <Grid item>
112                                 Please select the cluster that hosts the account you want to link with:
113                            <Select id="remoteHostsDropdown" native defaultValue={selectedCluster} style={{ marginLeft: "1em" }}
114                                     onChange={(event) => setSelectedCluster(event.target.value)}>
115                                     {Object.keys(remoteHostsConfig).map((k) => k !== localCluster ? <option key={k} value={k}>{k}</option> : null)}
116                                 </Select>
117                             </Grid>
118                             <Grid item>
119                                 <Button color="primary" variant="contained" onClick={() => startLinking(LinkAccountType.ACCESS_OTHER_REMOTE_ACCOUNT)}>
120                                     Link with an account on&nbsp;{hasRemoteHosts ? <label>{selectedCluster} </label> : null}
121                                 </Button>
122                             </Grid>
123                         </Grid>}
124                     </Grid> :
125                         <Grid container spacing={24}>
126                             <Grid container item direction="column" spacing={24}>
127                                 <Grid item>
128                                     You are currently logged in as {displayUser(targetUser, true, true)}
129                                 </Grid>
130                                 {targetUser.isActive ?
131                                     (loginCluster === "" ?
132                                         <> <Grid item>
133                                             This a remote account. You can link a local Arvados account to this one.
134                                             After linking, you can access the local account's data by logging into the
135                                             <b>{localCluster}</b> cluster as user <b>{targetUser.email}</b>
136                                             from <b>{targetUser.uuid.substr(0, 5)}</b>.
137                                         </Grid >
138                                         <Grid item>
139                                             <Button color="primary" variant="contained" onClick={() => startLinking(LinkAccountType.ADD_LOCAL_TO_REMOTE)}>
140                                                 Link an account from {localCluster} to this account
141                                             </Button>
142                                         </Grid> </>
143                                    : <Grid item>Please visit cluster
144                                        <a href={remoteHostsConfig[loginCluster].workbench2Url + "/link_account"}>{loginCluster}</a>
145                                        to perform account linking.</Grid>
146                                     )
147                                  : <Grid item>
148                                      This an inactive remote account. An administrator must activate your
149                                      account before you can proceed.  After your accounts is activated,
150                                      you can link a local Arvados account hosted by the <b>{localCluster}</b>
151                                      cluster to this one.
152                                  </Grid >}
153                             </Grid>
154                         </Grid>}
155                 </div>}
156                 {!isProcessing && (status === LinkAccountPanelStatus.LINKING || status === LinkAccountPanelStatus.ERROR) && userToLink && targetUser &&
157                     <Grid container spacing={24}>
158                         {status === LinkAccountPanelStatus.LINKING && <Grid container item direction="column" spacing={24}>
159                             <Grid item>
160                                 Clicking 'Link accounts' will link {displayUser(userToLink, true, !isLocalUser(targetUser.uuid, localCluster))} to {displayUser(targetUser, true, !isLocalUser(targetUser.uuid, localCluster))}.
161                     </Grid>
162                             {(isLocalUser(targetUser.uuid, localCluster)) && <Grid item>
163                                 After linking, logging in as {displayUser(userToLink)} will log you into the same account as {displayUser(targetUser)}.
164                     </Grid>}
165                             <Grid item>
166                                 Any object owned by {displayUser(userToLink)} will be transfered to {displayUser(targetUser)}.
167                     </Grid>
168                             {!isLocalUser(targetUser.uuid, localCluster) && <Grid item>
169                                 You can access <b>{userToLink.email}</b> data by logging into <b>{localCluster}</b> with the <b>{targetUser.email}</b> account.
170                     </Grid>}
171                         </Grid>}
172                         {error === LinkAccountPanelError.NON_ADMIN && <Grid item>
173                             Cannot link admin account {displayUser(userToLink)} to non-admin account {displayUser(targetUser)}.
174                 </Grid>}
175                         {error === LinkAccountPanelError.SAME_USER && <Grid item>
176                             Cannot link {displayUser(targetUser)} to the same account.
177                 </Grid>}
178                         {error === LinkAccountPanelError.INACTIVE && <Grid item>
179                             Cannot link account {displayUser(userToLink)} to inactive account {displayUser(targetUser)}.
180                 </Grid>}
181                         <Grid container item direction="row" spacing={24}>
182                             <Grid item>
183                                 <Button variant="contained" onClick={() => cancelLinking()}>
184                                     Cancel
185                         </Button>
186                             </Grid>
187                             <Grid item>
188                                 <Button disabled={status === LinkAccountPanelStatus.ERROR} color="primary" variant="contained" onClick={() => linkAccount()}>
189                                     Link accounts
190                         </Button>
191                             </Grid>
192                         </Grid>
193                     </Grid>}
194             </CardContent>
195         </Card>;
196     });