1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { bindDataExplorerActions } from 'store/data-explorer/data-explorer-action';
7 import { RootState } from 'store/store';
8 import { getUserUuid } from "common/getuser";
9 import { ServiceRepository } from "services/services";
10 import { dialogActions } from 'store/dialog/dialog-actions';
11 import { startSubmit, reset } from "redux-form";
12 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
13 import { UserResource } from "models/user";
14 import { getResource } from 'store/resources/resources';
15 import { navigateTo, navigateToUsers, navigateToRootProject } from "store/navigation/navigation-action";
16 import { authActions } from 'store/auth/auth-action';
17 import { getTokenV2 } from "models/api-client-authorization";
19 export const USERS_PANEL_ID = 'usersPanel';
20 export const USER_ATTRIBUTES_DIALOG = 'userAttributesDialog';
21 export const USER_CREATE_FORM_NAME = 'userCreateFormName';
22 export const USER_MANAGEMENT_DIALOG = 'userManageDialog';
23 export const SETUP_SHELL_ACCOUNT_DIALOG = 'setupShellAccountDialog';
25 export interface UserCreateFormDialogData {
27 virtualMachineName: string;
28 groupVirtualMachine: string;
31 export interface SetupShellAccountFormDialogData {
33 virtualMachineName: string;
34 groupVirtualMachine: string;
37 export const openUserAttributes = (uuid: string) =>
38 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
39 const { resources } = getState();
40 const data = getResource<UserResource>(uuid)(resources);
41 dispatch(dialogActions.OPEN_DIALOG({ id: USER_ATTRIBUTES_DIALOG, data }));
44 export const openUserManagement = (uuid: string) =>
45 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
46 const { resources } = getState();
47 const data = getResource<UserResource>(uuid)(resources);
48 dispatch(dialogActions.OPEN_DIALOG({ id: USER_MANAGEMENT_DIALOG, data }));
51 export const openSetupShellAccount = (uuid: string) =>
52 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
53 const { resources } = getState();
54 const user = getResource<UserResource>(uuid)(resources);
55 const virtualMachines = await services.virtualMachineService.list();
56 dispatch(dialogActions.CLOSE_DIALOG({ id: USER_MANAGEMENT_DIALOG }));
57 dispatch(dialogActions.OPEN_DIALOG({ id: SETUP_SHELL_ACCOUNT_DIALOG, data: { user, ...virtualMachines } }));
60 export const loginAs = (uuid: string) =>
61 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
62 const { resources } = getState();
63 const data = getResource<UserResource>(uuid)(resources);
64 const client = await services.apiClientAuthorizationService.create({ ownerUuid: uuid });
66 dispatch<any>(authActions.INIT_USER({ user: data, token: getTokenV2(client) }));
67 window.location.reload();
68 dispatch<any>(navigateToRootProject);
72 export const openUserCreateDialog = () =>
73 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
74 const userUuid = getUserUuid(getState());
75 if (!userUuid) { return; }
76 const user = await services.userService.get(userUuid!);
77 const virtualMachines = await services.virtualMachineService.list();
78 dispatch(reset(USER_CREATE_FORM_NAME));
79 dispatch(dialogActions.OPEN_DIALOG({ id: USER_CREATE_FORM_NAME, data: { user, ...virtualMachines } }));
82 export const openUserProjects = (uuid: string) =>
83 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
84 dispatch<any>(navigateTo(uuid));
88 export const createUser = (user: UserCreateFormDialogData) =>
89 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
90 dispatch(startSubmit(USER_CREATE_FORM_NAME));
92 const newUser = await services.userService.create({ ...user });
93 dispatch(dialogActions.CLOSE_DIALOG({ id: USER_CREATE_FORM_NAME }));
94 dispatch(reset(USER_CREATE_FORM_NAME));
95 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "User has been successfully created.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
96 dispatch<any>(loadUsersPanel());
97 dispatch(userBindedActions.REQUEST_ITEMS());
105 export const setupUserVM = (setupData: SetupShellAccountFormDialogData) =>
106 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
107 dispatch(startSubmit(USER_CREATE_FORM_NAME));
109 // TODO: make correct API call
110 // const setupResult = await services.userService.setup({ ...setupData });
111 dispatch(dialogActions.CLOSE_DIALOG({ id: SETUP_SHELL_ACCOUNT_DIALOG }));
112 dispatch(reset(SETUP_SHELL_ACCOUNT_DIALOG));
113 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "User has been added to VM.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
114 dispatch<any>(loadUsersPanel());
115 dispatch(userBindedActions.REQUEST_ITEMS());
121 export const openUserPanel = () =>
122 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
123 const user = getState().auth.user;
124 if (user && user.isAdmin) {
125 dispatch<any>(navigateToUsers);
127 dispatch<any>(navigateToRootProject);
128 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "You don't have permissions to view this page", hideDuration: 2000 }));
132 export const toggleIsActive = (uuid: string) =>
133 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
134 const { resources } = getState();
135 const data = getResource<UserResource>(uuid)(resources);
136 const isActive = data!.isActive;
139 newActivity = await services.userService.unsetup(uuid);
141 newActivity = await services.userService.update(uuid, { isActive: true });
143 dispatch<any>(loadUsersPanel());
147 export const toggleIsAdmin = (uuid: string) =>
148 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
149 const { resources } = getState();
150 const data = getResource<UserResource>(uuid)(resources);
151 const isAdmin = data!.isAdmin;
152 const newActivity = await services.userService.update(uuid, { isAdmin: !isAdmin });
153 dispatch<any>(loadUsersPanel());
157 export const userBindedActions = bindDataExplorerActions(USERS_PANEL_ID);
159 export const loadUsersData = () =>
160 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
161 await services.userService.list({ count: "none" });
164 export const loadUsersPanel = () =>
165 (dispatch: Dispatch) => {
166 dispatch(userBindedActions.REQUEST_ITEMS());