import { RootState } from '~/store/store';
import { ServiceRepository } from "~/services/services";
import { dialogActions } from '~/store/dialog/dialog-actions';
-import { startSubmit, reset, stopSubmit } from "redux-form";
-import { getCommonResourceServiceError, CommonResourceServiceError } from "~/services/common-service/common-resource-service";
+import { startSubmit, reset } from "redux-form";
import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
import { UserResource } from "~/models/user";
import { getResource } from '~/store/resources/resources';
-import { navigateToProject } from "~/store/navigation/navigation-action";
+import { navigateToProject, navigateToUsers, navigateToRootProject } from "~/store/navigation/navigation-action";
+import { saveApiToken } from '~/store/auth/auth-action';
export const USERS_PANEL_ID = 'usersPanel';
export const USER_ATTRIBUTES_DIALOG = 'userAttributesDialog';
-export const USER_CREATE_FORM_NAME = 'repositoryCreateFormName';
+export const USER_CREATE_FORM_NAME = 'userCreateFormName';
+export const USER_MANAGEMENT_DIALOG = 'userManageDialog';
+export const SETUP_SHELL_ACCOUNT_DIALOG = 'setupShellAccountDialog';
export interface UserCreateFormDialogData {
- firstName: string;
- lastName: string;
email: string;
- identityUrl: string;
virtualMachineName: string;
groupVirtualMachine: string;
}
dispatch(dialogActions.OPEN_DIALOG({ id: USER_ATTRIBUTES_DIALOG, data }));
};
+export const openUserManagement = (uuid: string) =>
+ async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+ const { resources } = getState();
+ const data = getResource<UserResource>(uuid)(resources);
+ dispatch(dialogActions.OPEN_DIALOG({ id: USER_MANAGEMENT_DIALOG, data }));
+ };
+
+export const openSetupShellAccount = (uuid: string) =>
+ async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+ const { resources } = getState();
+ const user = getResource<UserResource>(uuid)(resources);
+ const virtualMachines = await services.virtualMachineService.list();
+ dispatch(dialogActions.CLOSE_DIALOG({ id: USER_MANAGEMENT_DIALOG }));
+ dispatch(dialogActions.OPEN_DIALOG({ id: SETUP_SHELL_ACCOUNT_DIALOG, data: { user, ...virtualMachines } }));
+ };
+
+export const loginAs = (uuid: string) =>
+ async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+ const { resources } = getState();
+ const data = getResource<UserResource>(uuid)(resources);
+ if (data) {
+ services.authService.saveUser(data);
+ }
+ const client = await services.apiClientAuthorizationService.create({ ownerUuid: uuid });
+ dispatch<any>(saveApiToken(`v2/${client.uuid}/${client.apiToken}`));
+ location.reload();
+ dispatch<any>(navigateToRootProject);
+ };
+
export const openUserCreateDialog = () =>
async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
const userUuid = await services.authService.getUuid();
dispatch(userBindedActions.REQUEST_ITEMS());
return newUser;
} catch (e) {
- const error = getCommonResourceServiceError(e);
- if (error === CommonResourceServiceError.NAME_HAS_ALREADY_BEEN_TAKEN) {
- dispatch(stopSubmit(USER_CREATE_FORM_NAME, { name: 'User with the same name already exists.' }));
- }
- return undefined;
+ return;
+ }
+ };
+
+export const openUserPanel = () =>
+ async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+ const user = getState().auth.user;
+ if (user && user.isAdmin) {
+ dispatch<any>(navigateToUsers);
+ } else {
+ dispatch<any>(navigateToRootProject);
+ dispatch(snackbarActions.OPEN_SNACKBAR({ message: "You don't have permissions to view this page", hideDuration: 2000 }));
}
};