1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
23 export const openSharingDialog = (resourceUuid: string) =>
24 (dispatch: Dispatch) => {
25 dispatch(dialogActions.OPEN_DIALOG({ id: SHARING_DIALOG_NAME, data: resourceUuid }));
26 dispatch<any>(loadSharingDialog);
29 export const closeSharingDialog = () =>
30 dialogActions.CLOSE_DIALOG({ id: SHARING_DIALOG_NAME });
32 export const connectSharingDialog = withDialog(SHARING_DIALOG_NAME);
33 export const connectSharingDialogProgress = withProgress(SHARING_DIALOG_NAME);
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);
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,
53 dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
56 const loadSharingDialog = async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
58 const dialog = getDialog<string>(getState().dialog, SHARING_DIALOG_NAME);
60 dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
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));
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));
74 const initializeManagementForm = (permissionLinks: PermissionResource[]) =>
75 async (dispatch: Dispatch, getState: () => RootState, { userService }: ServiceRepository) => {
77 const filters = new FilterBuilder()
78 .addIn('uuid', permissionLinks.map(({ tailUuid }) => tailUuid))
81 const { items: users } = await userService.list({ filters });
83 const getEmail = (tailUuid: string) => {
84 const user = users.find(({ uuid }) => uuid === tailUuid);
90 const managementPermissions = permissionLinks
92 item.tailUuid !== getPublicGroupUuid(getState()))
93 .map(({ tailUuid, name, uuid }) => ({
94 email: getEmail(tailUuid),
95 permissions: name as PermissionLevel,
99 const managementFormData: SharingManagementFormData = {
100 permissions: managementPermissions,
101 initialPermissions: managementPermissions,
104 dispatch(initialize(SHARING_MANAGEMENT_FORM_NAME, managementFormData));
107 const initializePublicAccessForm = (permissionLinks: PermissionResource[]) =>
108 (dispatch: Dispatch, getState: () => RootState, ) => {
110 const [publicPermission] = permissionLinks
111 .filter(item => item.tailUuid === getPublicGroupUuid(getState()));
113 const publicAccessFormData: SharingPublicAccessFormData = publicPermission
115 visibility: VisibilityLevel.PUBLIC,
116 permissionUuid: publicPermission.uuid,
119 visibility: permissionLinks.length > 0
120 ? VisibilityLevel.SHARED
121 : VisibilityLevel.PRIVATE,
125 dispatch(initialize(SHARING_PUBLIC_ACCESS_FORM_NAME, publicAccessFormData));
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);
135 if (permissionUuid) {
136 if (visibility === VisibilityLevel.PUBLIC) {
137 await permissionService.update(permissionUuid, {
138 name: PermissionLevel.CAN_READ
141 await permissionService.delete(permissionUuid);
144 } else if (visibility === VisibilityLevel.PUBLIC) {
146 await permissionService.create({
147 ownerUuid: user.uuid,
148 headUuid: dialog.data,
149 tailUuid: getPublicGroupUuid(state),
150 name: PermissionLevel.CAN_READ,
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) {
162 const { initialPermissions, permissions } = getSharingMangementFormData(state);
163 const { visibility } = getSharingPublicAccessFormData(state);
166 if (visibility === VisibilityLevel.PRIVATE) {
168 for (const permission of initialPermissions) {
169 await permissionService.delete(permission.permissionUuid);
174 const cancelledPermissions = differenceWith(
177 (a, b) => a.permissionUuid === b.permissionUuid
180 for (const { permissionUuid } of cancelledPermissions) {
181 await permissionService.delete(permissionUuid);
184 for (const permission of permissions) {
185 await permissionService.update(permission.permissionUuid, { name: permission.permissions });
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) {
198 const invitations = getFormValues(SHARING_INVITATION_FORM_NAME)(state) as SharingInvitationFormData;
200 const invitationData = invitations.invitedPeople
202 ownerUuid: user.uuid,
203 headUuid: dialog.data,
204 tailUuid: person.uuid,
205 name: invitations.permissions
208 for (const invitation of invitationData) {
209 await permissionService.create(invitation);