15685: Adds file path validation on rename file dialog.
[arvados-workbench2.git] / src / store / sharing-dialog / sharing-dialog-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { dialogActions } from "~/store/dialog/dialog-actions";
6 import { withDialog } from "~/store/dialog/with-dialog";
7 import { SHARING_DIALOG_NAME, SharingPublicAccessFormData, SHARING_PUBLIC_ACCESS_FORM_NAME, SHARING_INVITATION_FORM_NAME, SharingManagementFormData, SharingInvitationFormData, VisibilityLevel, getSharingMangementFormData, getSharingPublicAccessFormData } from './sharing-dialog-types';
8 import { Dispatch } from 'redux';
9 import { ServiceRepository } from "~/services/services";
10 import { FilterBuilder } from '~/services/api/filter-builder';
11 import { initialize, getFormValues, reset } from 'redux-form';
12 import { SHARING_MANAGEMENT_FORM_NAME } from '~/store/sharing-dialog/sharing-dialog-types';
13 import { RootState } from '~/store/store';
14 import { getDialog } from '~/store/dialog/dialog-reducer';
15 import { PermissionLevel } from '~/models/permission';
16 import { getPublicGroupUuid } from "~/store/workflow-panel/workflow-panel-actions";
17 import { PermissionResource } from '~/models/permission';
18 import { differenceWith } from "lodash";
19 import { withProgress } from "~/store/progress-indicator/with-progress";
20 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions.ts';
21 import { snackbarActions, SnackbarKind } from "../snackbar/snackbar-actions";
22 import { extractUuidKind, ResourceKind } from "~/models/resource";
23
24 export const openSharingDialog = (resourceUuid: string) =>
25     (dispatch: Dispatch) => {
26         dispatch(dialogActions.OPEN_DIALOG({ id: SHARING_DIALOG_NAME, data: resourceUuid }));
27         dispatch<any>(loadSharingDialog);
28     };
29
30 export const closeSharingDialog = () =>
31     dialogActions.CLOSE_DIALOG({ id: SHARING_DIALOG_NAME });
32
33 export const connectSharingDialog = withDialog(SHARING_DIALOG_NAME);
34 export const connectSharingDialogProgress = withProgress(SHARING_DIALOG_NAME);
35
36
37 export const saveSharingDialogChanges = async (dispatch: Dispatch) => {
38     dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
39     await dispatch<any>(savePublicPermissionChanges);
40     await dispatch<any>(saveManagementChanges);
41     await dispatch<any>(sendInvitations);
42     dispatch(reset(SHARING_INVITATION_FORM_NAME));
43     await dispatch<any>(loadSharingDialog);
44 };
45
46 export const sendSharingInvitations = async (dispatch: Dispatch) => {
47     dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
48     await dispatch<any>(sendInvitations);
49     dispatch(closeSharingDialog());
50     dispatch(snackbarActions.OPEN_SNACKBAR({
51         message: 'Resource has been shared',
52         kind: SnackbarKind.SUCCESS,
53     }));
54     dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
55 };
56
57 const loadSharingDialog = async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
58
59     const dialog = getDialog<string>(getState().dialog, SHARING_DIALOG_NAME);
60     if (dialog) {
61         dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
62         try {
63             const { items } = await permissionService.listResourcePermissions(dialog.data);
64             dispatch<any>(initializePublicAccessForm(items));
65             await dispatch<any>(initializeManagementForm(items));
66             dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
67         } catch (e) {
68             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'You do not have access to share this item', hideDuration: 2000, kind: SnackbarKind.ERROR }));
69             dispatch(dialogActions.CLOSE_DIALOG({ id: SHARING_DIALOG_NAME }));
70             dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
71         }
72     }
73 };
74
75 const initializeManagementForm = (permissionLinks: PermissionResource[]) =>
76     async (dispatch: Dispatch, getState: () => RootState, { userService, groupsService }: ServiceRepository) => {
77
78         const filters = new FilterBuilder()
79             .addIn('uuid', permissionLinks.map(({ tailUuid }) => tailUuid))
80             .getFilters();
81
82         const { items: users } = await userService.list({ filters, count: "none" });
83         const { items: groups } = await groupsService.list({ filters, count: "none" });
84
85         const getEmail = (tailUuid: string) => {
86             const user = users.find(({ uuid }) => uuid === tailUuid);
87             const group = groups.find(({ uuid }) => uuid === tailUuid);
88             return user
89                 ? user.email
90                 : group
91                     ? group.name
92                     : tailUuid;
93         };
94
95         const managementPermissions = permissionLinks
96             .filter(item =>
97                 item.tailUuid !== getPublicGroupUuid(getState()))
98             .map(({ tailUuid, name, uuid }) => ({
99                 email: getEmail(tailUuid),
100                 permissions: name as PermissionLevel,
101                 permissionUuid: uuid,
102             }));
103
104         const managementFormData: SharingManagementFormData = {
105             permissions: managementPermissions,
106             initialPermissions: managementPermissions,
107         };
108
109         dispatch(initialize(SHARING_MANAGEMENT_FORM_NAME, managementFormData));
110     };
111
112 const initializePublicAccessForm = (permissionLinks: PermissionResource[]) =>
113     (dispatch: Dispatch, getState: () => RootState, ) => {
114
115         const [publicPermission] = permissionLinks
116             .filter(item => item.tailUuid === getPublicGroupUuid(getState()));
117
118         const publicAccessFormData: SharingPublicAccessFormData = publicPermission
119             ? {
120                 visibility: VisibilityLevel.PUBLIC,
121                 permissionUuid: publicPermission.uuid,
122             }
123             : {
124                 visibility: permissionLinks.length > 0
125                     ? VisibilityLevel.SHARED
126                     : VisibilityLevel.PRIVATE,
127                 permissionUuid: '',
128             };
129
130         dispatch(initialize(SHARING_PUBLIC_ACCESS_FORM_NAME, publicAccessFormData));
131     };
132
133 const savePublicPermissionChanges = async (_: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
134     const state = getState();
135     const { user } = state.auth;
136     const dialog = getDialog<string>(state.dialog, SHARING_DIALOG_NAME);
137     if (dialog && user) {
138         const { permissionUuid, visibility } = getSharingPublicAccessFormData(state);
139
140         if (permissionUuid) {
141             if (visibility === VisibilityLevel.PUBLIC) {
142                 await permissionService.update(permissionUuid, {
143                     name: PermissionLevel.CAN_READ
144                 });
145             } else {
146                 await permissionService.delete(permissionUuid);
147             }
148
149         } else if (visibility === VisibilityLevel.PUBLIC) {
150
151             await permissionService.create({
152                 ownerUuid: user.uuid,
153                 headUuid: dialog.data,
154                 tailUuid: getPublicGroupUuid(state),
155                 name: PermissionLevel.CAN_READ,
156             });
157         }
158     }
159 };
160
161 const saveManagementChanges = async (_: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
162     const state = getState();
163     const { user } = state.auth;
164     const dialog = getDialog<string>(state.dialog, SHARING_DIALOG_NAME);
165     if (dialog && user) {
166
167         const { initialPermissions, permissions } = getSharingMangementFormData(state);
168         const { visibility } = getSharingPublicAccessFormData(state);
169
170
171         if (visibility === VisibilityLevel.PRIVATE) {
172
173             for (const permission of initialPermissions) {
174                 await permissionService.delete(permission.permissionUuid);
175             }
176
177         } else {
178
179             const cancelledPermissions = differenceWith(
180                 initialPermissions,
181                 permissions,
182                 (a, b) => a.permissionUuid === b.permissionUuid
183             );
184
185             for (const { permissionUuid } of cancelledPermissions) {
186                 await permissionService.delete(permissionUuid);
187             }
188
189             for (const permission of permissions) {
190                 await permissionService.update(permission.permissionUuid, { name: permission.permissions });
191             }
192
193         }
194     }
195 };
196
197 const sendInvitations = async (_: Dispatch, getState: () => RootState, { permissionService, userService }: ServiceRepository) => {
198     const state = getState();
199     const { user } = state.auth;
200     const dialog = getDialog<string>(state.dialog, SHARING_DIALOG_NAME);
201     if (dialog && user) {
202         const invitations = getFormValues(SHARING_INVITATION_FORM_NAME)(state) as SharingInvitationFormData;
203
204         const getGroupsFromForm = invitations.invitedPeople.filter((invitation) => extractUuidKind(invitation.uuid) === ResourceKind.GROUP);
205         const getUsersFromForm = invitations.invitedPeople.filter((invitation) => extractUuidKind(invitation.uuid) === ResourceKind.USER);
206
207         const invitationDataUsers = getUsersFromForm
208             .map(person => ({
209                 ownerUuid: user.uuid,
210                 headUuid: dialog.data,
211                 tailUuid: person.uuid,
212                 name: invitations.permissions
213             }));
214
215         const invitationsDataGroups = getGroupsFromForm.map(
216             group => ({
217                 ownerUuid: user.uuid,
218                 headUuid: dialog.data,
219                 tailUuid: group.uuid,
220                 name: invitations.permissions
221             })
222         );
223
224         const data = invitationDataUsers.concat(invitationsDataGroups);
225
226         for (const invitation of data) {
227             await permissionService.create(invitation);
228         }
229     }
230 };