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, stopSubmit } 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";
18 import { VIRTUAL_MACHINE_ADD_LOGIN_GROUPS_FIELD, VIRTUAL_MACHINE_ADD_LOGIN_VM_FIELD } from "store/virtual-machines/virtual-machines-actions";
19 import { PermissionLevel } from "models/permission";
20 import { updateResources } from "store/resources/resources-actions";
22 export const USERS_PANEL_ID = 'usersPanel';
23 export const USER_ATTRIBUTES_DIALOG = 'userAttributesDialog';
24 export const USER_CREATE_FORM_NAME = 'userCreateFormName';
26 export interface UserCreateFormDialogData {
28 [VIRTUAL_MACHINE_ADD_LOGIN_VM_FIELD]: string;
29 [VIRTUAL_MACHINE_ADD_LOGIN_GROUPS_FIELD]: string[];
32 export const userBindedActions = bindDataExplorerActions(USERS_PANEL_ID);
34 export const openUserAttributes = (uuid: string) =>
35 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
36 const { resources } = getState();
37 const data = getResource<UserResource>(uuid)(resources);
38 dispatch(dialogActions.OPEN_DIALOG({ id: USER_ATTRIBUTES_DIALOG, data }));
41 export const loginAs = (uuid: string) =>
42 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
43 const { resources } = getState();
44 const data = getResource<UserResource>(uuid)(resources);
45 const client = await services.apiClientAuthorizationService.create({ ownerUuid: uuid });
47 dispatch<any>(authActions.INIT_USER({ user: data, token: getTokenV2(client) }));
48 window.location.reload();
49 dispatch<any>(navigateToRootProject);
53 export const openUserCreateDialog = () =>
54 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55 const userUuid = getUserUuid(getState());
56 if (!userUuid) { return; }
57 const user = await services.userService.get(userUuid!);
58 const virtualMachines = await services.virtualMachineService.list();
59 dispatch(reset(USER_CREATE_FORM_NAME));
60 dispatch(dialogActions.OPEN_DIALOG({ id: USER_CREATE_FORM_NAME, data: { user, ...virtualMachines } }));
63 export const openUserProjects = (uuid: string) =>
64 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65 dispatch<any>(navigateTo(uuid));
68 export const createUser = (data: UserCreateFormDialogData) =>
69 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
70 dispatch(startSubmit(USER_CREATE_FORM_NAME));
72 const newUser = await services.userService.create({
75 dispatch(updateResources([newUser]));
77 if (data[VIRTUAL_MACHINE_ADD_LOGIN_VM_FIELD]) {
78 const permission = await services.permissionService.create({
79 headUuid: data[VIRTUAL_MACHINE_ADD_LOGIN_VM_FIELD],
80 tailUuid: newUser.uuid,
81 name: PermissionLevel.CAN_LOGIN,
83 username: newUser.username,
87 dispatch(updateResources([permission]));
90 dispatch(dialogActions.CLOSE_DIALOG({ id: USER_CREATE_FORM_NAME }));
91 dispatch(reset(USER_CREATE_FORM_NAME));
92 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "User has been successfully created.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
93 dispatch<any>(loadUsersPanel());
94 dispatch(userBindedActions.REQUEST_ITEMS());
99 dispatch(stopSubmit(USER_CREATE_FORM_NAME));
103 export const openUserPanel = () =>
104 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
105 const user = getState().auth.user;
106 if (user && user.isAdmin) {
107 dispatch<any>(navigateToUsers);
109 dispatch<any>(navigateToRootProject);
110 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "You don't have permissions to view this page", hideDuration: 2000 }));
114 export const toggleIsActive = (uuid: string) =>
115 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
116 const { resources } = getState();
117 const data = getResource<UserResource>(uuid)(resources);
118 const isActive = data!.isActive;
121 newActivity = await services.userService.unsetup(uuid);
123 newActivity = await services.userService.update(uuid, { isActive: true });
125 dispatch<any>(loadUsersPanel());
129 export const toggleIsAdmin = (uuid: string) =>
130 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
131 const { resources } = getState();
132 const data = getResource<UserResource>(uuid)(resources);
133 const isAdmin = data!.isAdmin;
134 const newActivity = await services.userService.update(uuid, { isAdmin: !isAdmin });
135 dispatch<any>(loadUsersPanel());
139 export const loadUsersPanel = () =>
140 (dispatch: Dispatch) => {
141 dispatch(userBindedActions.REQUEST_ITEMS());