Implement group member removal
[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 import { FilterBuilder } from '~/services/api/filter-builder';
19
20 export const GROUPS_PANEL_ID = "groupsPanel";
21 export const CREATE_GROUP_DIALOG = "createGroupDialog";
22 export const CREATE_GROUP_FORM = "createGroupForm";
23 export const CREATE_GROUP_NAME_FIELD_NAME = 'name';
24 export const CREATE_GROUP_USERS_FIELD_NAME = 'users';
25 export const GROUP_ATTRIBUTES_DIALOG = 'groupAttributesDialog';
26 export const GROUP_REMOVE_DIALOG = 'groupRemoveDialog';
27
28 export const GroupsPanelActions = bindDataExplorerActions(GROUPS_PANEL_ID);
29
30 export const loadGroupsPanel = () => GroupsPanelActions.REQUEST_ITEMS();
31
32 export const openCreateGroupDialog = () =>
33     (dispatch: Dispatch) => {
34         dispatch(dialogActions.OPEN_DIALOG({ id: CREATE_GROUP_DIALOG, data: {} }));
35         dispatch(reset(CREATE_GROUP_FORM));
36     };
37
38 export const openGroupAttributes = (uuid: string) =>
39     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
40         const { resources } = getState();
41         const data = getResource<GroupResource>(uuid)(resources);
42         dispatch(dialogActions.OPEN_DIALOG({ id: GROUP_ATTRIBUTES_DIALOG, data }));
43     };
44
45 export const removeGroup = (uuid: string) =>
46     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
47         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
48         await services.groupsService.delete(uuid);
49         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
50         dispatch<any>(loadGroupsPanel());
51     };
52
53 export const openRemoveGroupDialog = (uuid: string) =>
54     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55         dispatch(dialogActions.OPEN_DIALOG({
56             id: GROUP_REMOVE_DIALOG,
57             data: {
58                 title: 'Remove group',
59                 text: 'Are you sure you want to remove this group?',
60                 confirmButtonLabel: 'Remove',
61                 uuid
62             }
63         }));
64     };
65
66 export interface CreateGroupFormData {
67     [CREATE_GROUP_NAME_FIELD_NAME]: string;
68     [CREATE_GROUP_USERS_FIELD_NAME]?: Person[];
69 }
70
71 export const createGroup = ({ name, users = [] }: CreateGroupFormData) =>
72     async (dispatch: Dispatch, _: {}, { groupsService, permissionService }: ServiceRepository) => {
73
74         dispatch(startSubmit(CREATE_GROUP_FORM));
75
76         try {
77
78             const newGroup = await groupsService.create({ name });
79
80             for (const user of users) {
81
82                 await addGroupMember({
83                     user,
84                     group: newGroup,
85                     dispatch,
86                     permissionService,
87                 });
88
89             }
90
91             dispatch(dialogActions.CLOSE_DIALOG({ id: CREATE_GROUP_DIALOG }));
92             dispatch(reset(CREATE_GROUP_FORM));
93             dispatch(loadGroupsPanel());
94             dispatch(snackbarActions.OPEN_SNACKBAR({
95                 message: `${newGroup.name} group has been created`,
96                 kind: SnackbarKind.SUCCESS
97             }));
98
99             return newGroup;
100
101         } catch (e) {
102
103             const error = getCommonResourceServiceError(e);
104             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
105                 dispatch(stopSubmit(CREATE_GROUP_FORM, { name: 'Group with the same name already exists.' } as FormErrors));
106             }
107
108             return;
109
110         }
111     };
112
113 interface DeleteGroupMemberArgs {
114     user: { uuid: string, name: string };
115     group: { uuid: string, name: string };
116     dispatch: Dispatch;
117     permissionService: PermissionService;
118 }
119
120 export const deleteGroupMember = async ({ user, group, ...args }: DeleteGroupMemberArgs) => {
121
122     await deletePermission({
123         tail: user,
124         head: group,
125         ...args,
126     });
127
128     await deletePermission({
129         tail: group,
130         head: user,
131         ...args,
132     });
133
134 };
135
136 interface AddGroupMemberArgs {
137     user: { uuid: string, name: string };
138     group: { uuid: string, name: string };
139     dispatch: Dispatch;
140     permissionService: PermissionService;
141 }
142
143 export const addGroupMember = async ({ user, group, ...args }: AddGroupMemberArgs) => {
144
145
146
147     await createPermission({
148         head: { ...group },
149         tail: { ...user },
150         permissionLevel: PermissionLevel.CAN_MANAGE,
151         ...args,
152     });
153
154     await createPermission({
155         head: { ...user },
156         tail: { ...group },
157         permissionLevel: PermissionLevel.CAN_READ,
158         ...args,
159     });
160
161 };
162
163 interface CreatePermissionLinkArgs {
164     head: { uuid: string, name: string };
165     tail: { uuid: string, name: string };
166     permissionLevel: PermissionLevel;
167     dispatch: Dispatch;
168     permissionService: PermissionService;
169 }
170
171 const createPermission = async ({ head, tail, permissionLevel, dispatch, permissionService }: CreatePermissionLinkArgs) => {
172
173     try {
174
175         await permissionService.create({
176             tailUuid: tail.uuid,
177             headUuid: head.uuid,
178             name: permissionLevel,
179         });
180
181     } catch (e) {
182
183         dispatch(snackbarActions.OPEN_SNACKBAR({
184             message: `Could not add ${tail.name} -> ${head.name} relation`,
185             kind: SnackbarKind.ERROR,
186         }));
187
188     }
189
190 };
191
192 interface DeletePermissionLinkArgs {
193     head: { uuid: string, name: string };
194     tail: { uuid: string, name: string };
195     dispatch: Dispatch;
196     permissionService: PermissionService;
197 }
198
199 export const deletePermission = async ({ head, tail, dispatch, permissionService }: DeletePermissionLinkArgs) => {
200
201     try {
202
203         const permissionsResponse = await permissionService.list({
204
205             filters: new FilterBuilder()
206                 .addEqual('tailUuid', tail.uuid)
207                 .addEqual('headUuid', head.uuid)
208                 .getFilters()
209
210         });
211
212         const [permission] = permissionsResponse.items;
213
214         if (permission) {
215
216             await permissionService.delete(permission.uuid);
217
218         } else {
219
220             throw new Error('Permission not found');
221
222         }
223
224
225     } catch (e) {
226
227         dispatch(snackbarActions.OPEN_SNACKBAR({
228             message: `Could not delete ${tail.name} -> ${head.name} relation`,
229             kind: SnackbarKind.ERROR,
230         }));
231
232     }
233
234 };