Extract function for adding group member
[arvados-workbench2.git] / src / store / groups-panel / groups-panel-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from 'redux';
6 import { reset, startSubmit, stopSubmit, FormErrors } from 'redux-form';
7 import { bindDataExplorerActions } from "~/store/data-explorer/data-explorer-action";
8 import { dialogActions } from '~/store/dialog/dialog-actions';
9 import { Person } from '~/views-components/sharing-dialog/people-select';
10 import { RootState } from '~/store/store';
11 import { ServiceRepository } from '~/services/services';
12 import { getResource } from '~/store/resources/resources';
13 import { GroupResource } from '~/models/group';
14 import { getCommonResourceServiceError, CommonResourceServiceError } from '~/services/common-service/common-resource-service';
15 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
16 import { PermissionLevel, PermissionResource } from '~/models/permission';
17 import { PermissionService } from '~/services/permission-service/permission-service';
18
19 export const GROUPS_PANEL_ID = "groupsPanel";
20 export const CREATE_GROUP_DIALOG = "createGroupDialog";
21 export const CREATE_GROUP_FORM = "createGroupForm";
22 export const CREATE_GROUP_NAME_FIELD_NAME = 'name';
23 export const CREATE_GROUP_USERS_FIELD_NAME = 'users';
24 export const GROUP_ATTRIBUTES_DIALOG = 'groupAttributesDialog';
25 export const GROUP_REMOVE_DIALOG = 'groupRemoveDialog';
26 export const MEMBER_ATTRIBUTES_DIALOG = 'memberAttributesDialog';
27 export const MEMBER_REMOVE_DIALOG = 'memberRemoveDialog';
28
29 export const GroupsPanelActions = bindDataExplorerActions(GROUPS_PANEL_ID);
30
31 export const loadGroupsPanel = () => GroupsPanelActions.REQUEST_ITEMS();
32
33 export const openCreateGroupDialog = () =>
34     (dispatch: Dispatch) => {
35         dispatch(dialogActions.OPEN_DIALOG({ id: CREATE_GROUP_DIALOG, data: {} }));
36         dispatch(reset(CREATE_GROUP_FORM));
37     };
38
39 export const openGroupAttributes = (uuid: string) =>
40     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
41         const { resources } = getState();
42         const data = getResource<GroupResource>(uuid)(resources);
43         dispatch(dialogActions.OPEN_DIALOG({ id: GROUP_ATTRIBUTES_DIALOG, data }));
44     };
45
46 export const openGroupMemberAttributes = (uuid: string) =>
47     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
48         const { resources } = getState();
49         const data = getResource<PermissionResource>(uuid)(resources);
50         dispatch(dialogActions.OPEN_DIALOG({ id: MEMBER_ATTRIBUTES_DIALOG, data }));
51     };
52
53 export const removeGroup = (uuid: string) =>
54     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
56         await services.groupsService.delete(uuid);
57         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
58         dispatch<any>(loadGroupsPanel());
59     };
60
61 export const openRemoveGroupDialog = (uuid: string) =>
62     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
63         dispatch(dialogActions.OPEN_DIALOG({
64             id: GROUP_REMOVE_DIALOG,
65             data: {
66                 title: 'Remove group',
67                 text: 'Are you sure you want to remove this group?',
68                 confirmButtonLabel: 'Remove',
69                 uuid
70             }
71         }));
72     };
73
74 export const removeGroupMember = (uuid: string) =>
75     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
76         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
77         await services.permissionService.delete(uuid);
78         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
79         dispatch<any>(loadGroupsPanel());
80     };
81
82 export const openRemoveGroupMemberDialog = (uuid: string) =>
83     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
84         dispatch(dialogActions.OPEN_DIALOG({
85             id: MEMBER_REMOVE_DIALOG,
86             data: {
87                 title: 'Remove member',
88                 text: 'Are you sure you want to remove this member from this group?',
89                 confirmButtonLabel: 'Remove',
90                 uuid
91             }
92         }));
93     };
94
95 export interface CreateGroupFormData {
96     [CREATE_GROUP_NAME_FIELD_NAME]: string;
97     [CREATE_GROUP_USERS_FIELD_NAME]?: Person[];
98 }
99
100 export const createGroup = ({ name, users = [] }: CreateGroupFormData) =>
101     async (dispatch: Dispatch, _: {}, { groupsService, permissionService }: ServiceRepository) => {
102
103         dispatch(startSubmit(CREATE_GROUP_FORM));
104
105         try {
106
107             const newGroup = await groupsService.create({ name });
108
109             for (const user of users) {
110
111                 await addGroupMember({
112                     user,
113                     group: newGroup,
114                     dispatch,
115                     permissionService,
116                 });
117
118             }
119
120             dispatch(dialogActions.CLOSE_DIALOG({ id: CREATE_GROUP_DIALOG }));
121             dispatch(reset(CREATE_GROUP_FORM));
122             dispatch(loadGroupsPanel());
123             dispatch(snackbarActions.OPEN_SNACKBAR({
124                 message: `${newGroup.name} group has been created`,
125                 kind: SnackbarKind.SUCCESS
126             }));
127
128             return newGroup;
129
130         } catch (e) {
131
132             const error = getCommonResourceServiceError(e);
133             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
134                 dispatch(stopSubmit(CREATE_GROUP_FORM, { name: 'Group with the same name already exists.' } as FormErrors));
135             }
136
137             return;
138
139         }
140     };
141
142 interface AddGroupMemberArgs {
143     user: { uuid: string, name: string };
144     group: { uuid: string, name: string };
145     dispatch: Dispatch;
146     permissionService: PermissionService;
147 }
148
149 const addGroupMember = async ({ user, group, ...args }: AddGroupMemberArgs) => {
150
151     await createPermissionLink({
152         head: { ...group },
153         tail: { ...user },
154         permissionLevel: PermissionLevel.CAN_MANAGE,
155         ...args,
156     });
157
158     await createPermissionLink({
159         head: { ...user },
160         tail: { ...group },
161         permissionLevel: PermissionLevel.CAN_READ,
162         ...args,
163     });
164
165 };
166
167 interface CreatePermissionLinkArgs {
168     head: { uuid: string, name: string };
169     tail: { uuid: string, name: string };
170     permissionLevel: PermissionLevel;
171     dispatch: Dispatch;
172     permissionService: PermissionService;
173 }
174
175 const createPermissionLink = async ({ head, tail, permissionLevel, dispatch, permissionService }: CreatePermissionLinkArgs) => {
176
177     try {
178
179         await permissionService.create({
180             tailUuid: tail.uuid,
181             headUuid: head.uuid,
182             name: permissionLevel,
183         });
184
185     } catch (e) {
186
187         dispatch(snackbarActions.OPEN_SNACKBAR({
188             message: `Could not add ${tail.name} -> ${head.name} relation`,
189             kind: SnackbarKind.ERROR,
190         }));
191
192     }
193
194 };