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";
22 import { extractUuidKind, ResourceKind } from "~/models/resource";
24 export const openSharingDialog = (resourceUuid: string) =>
25 (dispatch: Dispatch) => {
26 dispatch(dialogActions.OPEN_DIALOG({ id: SHARING_DIALOG_NAME, data: resourceUuid }));
27 dispatch<any>(loadSharingDialog);
30 export const closeSharingDialog = () =>
31 dialogActions.CLOSE_DIALOG({ id: SHARING_DIALOG_NAME });
33 export const connectSharingDialog = withDialog(SHARING_DIALOG_NAME);
34 export const connectSharingDialogProgress = withProgress(SHARING_DIALOG_NAME);
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);
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,
54 dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
57 const loadSharingDialog = async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
59 const dialog = getDialog<string>(getState().dialog, SHARING_DIALOG_NAME);
61 dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
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));
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));
75 const initializeManagementForm = (permissionLinks: PermissionResource[]) =>
76 async (dispatch: Dispatch, getState: () => RootState, { userService, groupsService }: ServiceRepository) => {
78 const filters = new FilterBuilder()
79 .addIn('uuid', permissionLinks.map(({ tailUuid }) => tailUuid))
82 const { items: users } = await userService.list({ filters, count: "none" });
83 const { items: groups } = await groupsService.list({ filters, count: "none" });
85 const getEmail = (tailUuid: string) => {
86 const user = users.find(({ uuid }) => uuid === tailUuid);
87 const group = groups.find(({ uuid }) => uuid === tailUuid);
95 const managementPermissions = permissionLinks
97 item.tailUuid !== getPublicGroupUuid(getState()))
98 .map(({ tailUuid, name, uuid }) => ({
99 email: getEmail(tailUuid),
100 permissions: name as PermissionLevel,
101 permissionUuid: uuid,
104 const managementFormData: SharingManagementFormData = {
105 permissions: managementPermissions,
106 initialPermissions: managementPermissions,
109 dispatch(initialize(SHARING_MANAGEMENT_FORM_NAME, managementFormData));
112 const initializePublicAccessForm = (permissionLinks: PermissionResource[]) =>
113 (dispatch: Dispatch, getState: () => RootState, ) => {
115 const [publicPermission] = permissionLinks
116 .filter(item => item.tailUuid === getPublicGroupUuid(getState()));
118 const publicAccessFormData: SharingPublicAccessFormData = publicPermission
120 visibility: VisibilityLevel.PUBLIC,
121 permissionUuid: publicPermission.uuid,
124 visibility: permissionLinks.length > 0
125 ? VisibilityLevel.SHARED
126 : VisibilityLevel.PRIVATE,
130 dispatch(initialize(SHARING_PUBLIC_ACCESS_FORM_NAME, publicAccessFormData));
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);
140 if (permissionUuid) {
141 if (visibility === VisibilityLevel.PUBLIC) {
142 await permissionService.update(permissionUuid, {
143 name: PermissionLevel.CAN_READ
146 await permissionService.delete(permissionUuid);
149 } else if (visibility === VisibilityLevel.PUBLIC) {
151 await permissionService.create({
152 ownerUuid: user.uuid,
153 headUuid: dialog.data,
154 tailUuid: getPublicGroupUuid(state),
155 name: PermissionLevel.CAN_READ,
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) {
167 const { initialPermissions, permissions } = getSharingMangementFormData(state);
168 const { visibility } = getSharingPublicAccessFormData(state);
171 if (visibility === VisibilityLevel.PRIVATE) {
173 for (const permission of initialPermissions) {
174 await permissionService.delete(permission.permissionUuid);
179 const cancelledPermissions = differenceWith(
182 (a, b) => a.permissionUuid === b.permissionUuid
185 for (const { permissionUuid } of cancelledPermissions) {
186 await permissionService.delete(permissionUuid);
189 for (const permission of permissions) {
190 await permissionService.update(permission.permissionUuid, { name: permission.permissions });
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;
204 const getGroupsFromForm = invitations.invitedPeople.filter((invitation) => extractUuidKind(invitation.uuid) === ResourceKind.GROUP);
205 const getUsersFromForm = invitations.invitedPeople.filter((invitation) => extractUuidKind(invitation.uuid) === ResourceKind.USER);
207 const invitationDataUsers = getUsersFromForm
209 ownerUuid: user.uuid,
210 headUuid: dialog.data,
211 tailUuid: person.uuid,
212 name: invitations.permissions
215 const invitationsDataGroups = getGroupsFromForm.map(
217 ownerUuid: user.uuid,
218 headUuid: dialog.data,
219 tailUuid: group.uuid,
220 name: invitations.permissions
224 const data = invitationDataUsers.concat(invitationsDataGroups);
226 for (const invitation of data) {
227 await permissionService.create(invitation);