metadata-refactor
[arvados.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
23 export const openSharingDialog = (resourceUuid: string) =>
24     (dispatch: Dispatch) => {
25         dispatch(dialogActions.OPEN_DIALOG({ id: SHARING_DIALOG_NAME, data: resourceUuid }));
26         dispatch<any>(loadSharingDialog);
27     };
28
29 export const closeSharingDialog = () =>
30     dialogActions.CLOSE_DIALOG({ id: SHARING_DIALOG_NAME });
31
32 export const connectSharingDialog = withDialog(SHARING_DIALOG_NAME);
33 export const connectSharingDialogProgress = withProgress(SHARING_DIALOG_NAME);
34
35
36 export const saveSharingDialogChanges = async (dispatch: Dispatch) => {
37     dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
38     await dispatch<any>(savePublicPermissionChanges);
39     await dispatch<any>(saveManagementChanges);
40     await dispatch<any>(sendInvitations);
41     dispatch(reset(SHARING_INVITATION_FORM_NAME));
42     await dispatch<any>(loadSharingDialog);
43 };
44
45 export const sendSharingInvitations = async (dispatch: Dispatch) => {
46     dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
47     await dispatch<any>(sendInvitations);
48     dispatch(closeSharingDialog());
49     dispatch(snackbarActions.OPEN_SNACKBAR({
50         message: 'Resource has been shared',
51         kind: SnackbarKind.SUCCESS,
52     }));
53     dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
54 };
55
56 const loadSharingDialog = async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
57
58     const dialog = getDialog<string>(getState().dialog, SHARING_DIALOG_NAME);
59     if (dialog) {
60         dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
61         try {
62             const { items } = await permissionService.listResourcePermissions(dialog.data);
63             dispatch<any>(initializePublicAccessForm(items));
64             await dispatch<any>(initializeManagementForm(items));
65             dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
66         } catch (e) {
67             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'You do not have access to share this item', hideDuration: 2000, kind: SnackbarKind.ERROR }));
68             dispatch(dialogActions.CLOSE_DIALOG({ id: SHARING_DIALOG_NAME }));
69             dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
70         }
71     }
72 };
73
74 const initializeManagementForm = (permissionLinks: PermissionResource[]) =>
75     async (dispatch: Dispatch, getState: () => RootState, { userService }: ServiceRepository) => {
76
77         const filters = new FilterBuilder()
78             .addIn('uuid', permissionLinks.map(({ tailUuid }) => tailUuid))
79             .getFilters();
80
81         const { items: users } = await userService.list({ filters });
82
83         const getEmail = (tailUuid: string) => {
84             const user = users.find(({ uuid }) => uuid === tailUuid);
85             return user
86                 ? user.email
87                 : tailUuid;
88         };
89
90         const managementPermissions = permissionLinks
91             .filter(item =>
92                 item.tailUuid !== getPublicGroupUuid(getState()))
93             .map(({ tailUuid, name, uuid }) => ({
94                 email: getEmail(tailUuid),
95                 permissions: name as PermissionLevel,
96                 permissionUuid: uuid,
97             }));
98
99         const managementFormData: SharingManagementFormData = {
100             permissions: managementPermissions,
101             initialPermissions: managementPermissions,
102         };
103
104         dispatch(initialize(SHARING_MANAGEMENT_FORM_NAME, managementFormData));
105     };
106
107 const initializePublicAccessForm = (permissionLinks: PermissionResource[]) =>
108     (dispatch: Dispatch, getState: () => RootState, ) => {
109
110         const [publicPermission] = permissionLinks
111             .filter(item => item.tailUuid === getPublicGroupUuid(getState()));
112
113         const publicAccessFormData: SharingPublicAccessFormData = publicPermission
114             ? {
115                 visibility: VisibilityLevel.PUBLIC,
116                 permissionUuid: publicPermission.uuid,
117             }
118             : {
119                 visibility: permissionLinks.length > 0
120                     ? VisibilityLevel.SHARED
121                     : VisibilityLevel.PRIVATE,
122                 permissionUuid: '',
123             };
124
125         dispatch(initialize(SHARING_PUBLIC_ACCESS_FORM_NAME, publicAccessFormData));
126     };
127
128 const savePublicPermissionChanges = async (_: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
129     const state = getState();
130     const { user } = state.auth;
131     const dialog = getDialog<string>(state.dialog, SHARING_DIALOG_NAME);
132     if (dialog && user) {
133         const { permissionUuid, visibility } = getSharingPublicAccessFormData(state);
134
135         if (permissionUuid) {
136             if (visibility === VisibilityLevel.PUBLIC) {
137                 await permissionService.update(permissionUuid, {
138                     name: PermissionLevel.CAN_READ
139                 });
140             } else {
141                 await permissionService.delete(permissionUuid);
142             }
143
144         } else if (visibility === VisibilityLevel.PUBLIC) {
145
146             await permissionService.create({
147                 ownerUuid: user.uuid,
148                 headUuid: dialog.data,
149                 tailUuid: getPublicGroupUuid(state),
150                 name: PermissionLevel.CAN_READ,
151             });
152         }
153     }
154 };
155
156 const saveManagementChanges = async (_: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
157     const state = getState();
158     const { user } = state.auth;
159     const dialog = getDialog<string>(state.dialog, SHARING_DIALOG_NAME);
160     if (dialog && user) {
161
162         const { initialPermissions, permissions } = getSharingMangementFormData(state);
163         const { visibility } = getSharingPublicAccessFormData(state);
164
165
166         if (visibility === VisibilityLevel.PRIVATE) {
167
168             for (const permission of initialPermissions) {
169                 await permissionService.delete(permission.permissionUuid);
170             }
171
172         } else {
173
174             const cancelledPermissions = differenceWith(
175                 initialPermissions,
176                 permissions,
177                 (a, b) => a.permissionUuid === b.permissionUuid
178             );
179
180             for (const { permissionUuid } of cancelledPermissions) {
181                 await permissionService.delete(permissionUuid);
182             }
183
184             for (const permission of permissions) {
185                 await permissionService.update(permission.permissionUuid, { name: permission.permissions });
186             }
187
188         }
189     }
190 };
191
192 const sendInvitations = async (_: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
193     const state = getState();
194     const { user } = state.auth;
195     const dialog = getDialog<string>(state.dialog, SHARING_DIALOG_NAME);
196     if (dialog && user) {
197
198         const invitations = getFormValues(SHARING_INVITATION_FORM_NAME)(state) as SharingInvitationFormData;
199
200         const invitationData = invitations.invitedPeople
201             .map(person => ({
202                 ownerUuid: user.uuid,
203                 headUuid: dialog.data,
204                 tailUuid: person.uuid,
205                 name: invitations.permissions
206             }));
207
208         for (const invitation of invitationData) {
209             await permissionService.create(invitation);
210         }
211
212     }
213 };