adding-posibility-to-choose-files-many-times-and-dont-replace-other-files
[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 ...', kind: SnackbarKind.INFO }));
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 AddGroupMemberArgs {
114     user: { uuid: string, name: string };
115     group: { uuid: string, name: string };
116     dispatch: Dispatch;
117     permissionService: PermissionService;
118 }
119
120 /**
121  * Group membership is determined by whether the group has can_read permission on an object. 
122  * If a group G can_read an object A, then we say A is a member of G.
123  * 
124  * [Permission model docs](https://doc.arvados.org/api/permission-model.html)
125  */
126 export const addGroupMember = async ({ user, group, ...args }: AddGroupMemberArgs) => {
127
128     await createPermission({
129         head: { ...user },
130         tail: { ...group },
131         permissionLevel: PermissionLevel.CAN_READ,
132         ...args,
133     });
134
135 };
136
137 interface CreatePermissionLinkArgs {
138     head: { uuid: string, name: string };
139     tail: { uuid: string, name: string };
140     permissionLevel: PermissionLevel;
141     dispatch: Dispatch;
142     permissionService: PermissionService;
143 }
144
145 const createPermission = async ({ head, tail, permissionLevel, dispatch, permissionService }: CreatePermissionLinkArgs) => {
146
147     try {
148
149         await permissionService.create({
150             tailUuid: tail.uuid,
151             headUuid: head.uuid,
152             name: permissionLevel,
153         });
154
155     } catch (e) {
156
157         dispatch(snackbarActions.OPEN_SNACKBAR({
158             message: `Could not add ${tail.name} -> ${head.name} relation`,
159             kind: SnackbarKind.ERROR,
160         }));
161
162     }
163
164 };
165
166 interface DeleteGroupMemberArgs {
167     user: { uuid: string, name: string };
168     group: { uuid: string, name: string };
169     dispatch: Dispatch;
170     permissionService: PermissionService;
171 }
172
173 export const deleteGroupMember = async ({ user, group, ...args }: DeleteGroupMemberArgs) => {
174
175     await deletePermission({
176         tail: group,
177         head: user,
178         ...args,
179     });
180
181 };
182
183 interface DeletePermissionLinkArgs {
184     head: { uuid: string, name: string };
185     tail: { uuid: string, name: string };
186     dispatch: Dispatch;
187     permissionService: PermissionService;
188 }
189
190 export const deletePermission = async ({ head, tail, dispatch, permissionService }: DeletePermissionLinkArgs) => {
191
192     try {
193
194         const permissionsResponse = await permissionService.list({
195
196             filters: new FilterBuilder()
197                 .addEqual('tailUuid', tail.uuid)
198                 .addEqual('headUuid', head.uuid)
199                 .getFilters()
200
201         });
202
203         const [permission] = permissionsResponse.items;
204
205         if (permission) {
206
207             await permissionService.delete(permission.uuid);
208
209         } else {
210
211             throw new Error('Permission not found');
212
213         }
214
215
216     } catch (e) {
217
218         dispatch(snackbarActions.OPEN_SNACKBAR({
219             message: `Could not delete ${tail.name} -> ${head.name} relation`,
220             kind: SnackbarKind.ERROR,
221         }));
222
223     }
224
225 };