18559: Disable user profile form fields when not admin or self
[arvados-workbench2.git] / src / views / user-profile-panel / user-profile-panel.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { RootState } from 'store/store';
6 import { compose, Dispatch } from 'redux';
7 import { reduxForm, isPristine, isValid } from 'redux-form';
8 import { connect } from 'react-redux';
9 import { saveEditedUser } from 'store/user-profile/user-profile-actions';
10 import { UserProfilePanelRoot, UserProfilePanelRootDataProps } from 'views/user-profile-panel/user-profile-panel-root';
11 import { openDeactivateDialog, USER_PROFILE_FORM } from "store/user-profile/user-profile-actions";
12 import { matchUserProfileRoute } from 'routes/routes';
13 import { UserResource } from 'models/user';
14 import { getResource } from 'store/resources/resources';
15 import { openSetupShellAccount, loginAs } from 'store/users/users-actions';
16
17 const mapStateToProps = (state: RootState): UserProfilePanelRootDataProps => {
18   const pathname = state.router.location ? state.router.location.pathname : '';
19   const match = matchUserProfileRoute(pathname);
20   const uuid = match ? match.params.id : state.auth.user?.uuid || '';
21   // get user resource
22   const user = getResource<UserResource>(uuid)(state.resources);
23   // const subprocesses = getSubprocesses(uuid)(resources);
24
25   return {
26     isAdmin: state.auth.user!.isAdmin,
27     isSelf: state.auth.user!.uuid === uuid,
28     isPristine: isPristine(USER_PROFILE_FORM)(state),
29     isValid: isValid(USER_PROFILE_FORM)(state),
30     initialValues: user,
31     localCluster: state.auth.localCluster
32 }};
33
34 const mapDispatchToProps = (dispatch: Dispatch) => ({
35     openSetupShellAccount: (uuid: string) => dispatch<any>(openSetupShellAccount(uuid)),
36     loginAs: (uuid: string) => dispatch<any>(loginAs(uuid)),
37     openDeactivateDialog: (uuid: string) => dispatch<any>(openDeactivateDialog(uuid)),
38 });
39
40 export const UserProfilePanel = compose(
41     connect(mapStateToProps, mapDispatchToProps),
42     reduxForm({
43         form: USER_PROFILE_FORM,
44         onSubmit: (data, dispatch) => {
45             dispatch(saveEditedUser(data));
46         }
47     }))(UserProfilePanelRoot);