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';
25 export const USER_MANAGEMENT_DIALOG = 'userManageDialog';
27 export interface UserCreateFormDialogData {
29 [VIRTUAL_MACHINE_ADD_LOGIN_VM_FIELD]: string;
30 [VIRTUAL_MACHINE_ADD_LOGIN_GROUPS_FIELD]: string[];
33 export const userBindedActions = bindDataExplorerActions(USERS_PANEL_ID);
35 export const openUserAttributes = (uuid: string) =>
36 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
37 const { resources } = getState();
38 const data = getResource<UserResource>(uuid)(resources);
39 dispatch(dialogActions.OPEN_DIALOG({ id: USER_ATTRIBUTES_DIALOG, data }));
42 export const openUserManagement = (uuid: string) =>
43 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44 const { resources } = getState();
45 const data = getResource<UserResource>(uuid)(resources);
46 dispatch(dialogActions.OPEN_DIALOG({ id: USER_MANAGEMENT_DIALOG, data }));
49 export const loginAs = (uuid: string) =>
50 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
51 const { resources } = getState();
52 const data = getResource<UserResource>(uuid)(resources);
53 const client = await services.apiClientAuthorizationService.create({ ownerUuid: uuid });
55 dispatch<any>(authActions.INIT_USER({ user: data, token: getTokenV2(client) }));
56 window.location.reload();
57 dispatch<any>(navigateToRootProject);
61 export const openUserCreateDialog = () =>
62 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
63 const userUuid = getUserUuid(getState());
64 if (!userUuid) { return; }
65 const user = await services.userService.get(userUuid!);
66 const virtualMachines = await services.virtualMachineService.list();
67 dispatch(reset(USER_CREATE_FORM_NAME));
68 dispatch(dialogActions.OPEN_DIALOG({ id: USER_CREATE_FORM_NAME, data: { user, ...virtualMachines } }));
71 export const openUserProjects = (uuid: string) =>
72 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
73 dispatch<any>(navigateTo(uuid));
76 export const createUser = (data: UserCreateFormDialogData) =>
77 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
78 dispatch(startSubmit(USER_CREATE_FORM_NAME));
80 const newUser = await services.userService.create({
83 dispatch(updateResources([newUser]));
85 if (data[VIRTUAL_MACHINE_ADD_LOGIN_VM_FIELD]) {
86 const permission = await services.permissionService.create({
87 headUuid: data[VIRTUAL_MACHINE_ADD_LOGIN_VM_FIELD],
88 tailUuid: newUser.uuid,
89 name: PermissionLevel.CAN_LOGIN,
91 username: newUser.username,
95 dispatch(updateResources([permission]));
98 dispatch(dialogActions.CLOSE_DIALOG({ id: USER_CREATE_FORM_NAME }));
99 dispatch(reset(USER_CREATE_FORM_NAME));
100 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "User has been successfully created.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
101 dispatch<any>(loadUsersPanel());
102 dispatch(userBindedActions.REQUEST_ITEMS());
107 dispatch(stopSubmit(USER_CREATE_FORM_NAME));
111 export const openUserPanel = () =>
112 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
113 const user = getState().auth.user;
114 if (user && user.isAdmin) {
115 dispatch<any>(navigateToUsers);
117 dispatch<any>(navigateToRootProject);
118 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "You don't have permissions to view this page", hideDuration: 2000 }));
122 export const toggleIsActive = (uuid: string) =>
123 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
124 const { resources } = getState();
125 const data = getResource<UserResource>(uuid)(resources);
126 const isActive = data!.isActive;
129 newActivity = await services.userService.unsetup(uuid);
131 newActivity = await services.userService.update(uuid, { isActive: true });
133 dispatch<any>(loadUsersPanel());
137 export const toggleIsAdmin = (uuid: string) =>
138 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
139 const { resources } = getState();
140 const data = getResource<UserResource>(uuid)(resources);
141 const isAdmin = data!.isAdmin;
142 const newActivity = await services.userService.update(uuid, { isAdmin: !isAdmin });
143 dispatch<any>(loadUsersPanel());
147 export const loadUsersPanel = () =>
148 (dispatch: Dispatch) => {
149 dispatch(userBindedActions.REQUEST_ITEMS());