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";
9 SHARING_INVITATION_FORM_NAME,
10 SharingManagementFormData,
11 SharingInvitationFormData,
12 getSharingMangementFormData,
13 SharingPublicAccessFormData,
15 SHARING_PUBLIC_ACCESS_FORM_NAME,
16 } from './sharing-dialog-types';
17 import { Dispatch } from 'redux';
18 import { ServiceRepository } from "services/services";
19 import { FilterBuilder } from 'services/api/filter-builder';
20 import { initialize, getFormValues, reset } from 'redux-form';
21 import { SHARING_MANAGEMENT_FORM_NAME } from 'store/sharing-dialog/sharing-dialog-types';
22 import { RootState } from 'store/store';
23 import { getDialog } from 'store/dialog/dialog-reducer';
24 import { PermissionLevel, PermissionResource } from 'models/permission';
25 import { differenceWith } from "lodash";
26 import { withProgress } from "store/progress-indicator/with-progress";
27 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
28 import { snackbarActions, SnackbarKind } from "../snackbar/snackbar-actions";
30 extractUuidObjectType,
32 } from "models/resource";
33 import { resourcesActions } from "store/resources/resources-actions";
34 import { getPublicGroupUuid } from "store/workflow-panel/workflow-panel-actions";
35 import { getSharingPublicAccessFormData } from './sharing-dialog-types';
37 export const openSharingDialog = (resourceUuid: string, refresh?: () => void) =>
38 (dispatch: Dispatch) => {
39 dispatch(dialogActions.OPEN_DIALOG({ id: SHARING_DIALOG_NAME, data: {resourceUuid, refresh} }));
40 dispatch<any>(loadSharingDialog);
43 export const closeSharingDialog = () =>
44 dialogActions.CLOSE_DIALOG({ id: SHARING_DIALOG_NAME });
46 export const connectSharingDialog = withDialog(SHARING_DIALOG_NAME);
47 export const connectSharingDialogProgress = withProgress(SHARING_DIALOG_NAME);
50 export const saveSharingDialogChanges = async (dispatch: Dispatch, getState: () => RootState) => {
51 dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
52 await dispatch<any>(savePublicPermissionChanges);
53 await dispatch<any>(saveManagementChanges);
54 await dispatch<any>(sendInvitations);
55 dispatch(reset(SHARING_INVITATION_FORM_NAME));
56 await dispatch<any>(loadSharingDialog);
57 dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
59 const dialog = getDialog<SharingDialogData>(getState().dialog, SHARING_DIALOG_NAME);
60 if (dialog && dialog.data.refresh) {
61 dialog.data.refresh();
65 export interface SharingDialogData {
70 export const createSharingToken = (expDate: Date | undefined) => async (dispatch: Dispatch, getState: () => RootState, { apiClientAuthorizationService }: ServiceRepository) => {
71 const dialog = getDialog<SharingDialogData>(getState().dialog, SHARING_DIALOG_NAME);
73 const resourceUuid = dialog.data.resourceUuid;
74 if (extractUuidObjectType(resourceUuid) === ResourceObjectType.COLLECTION) {
75 dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
77 const sharingToken = await apiClientAuthorizationService.createCollectionSharingToken(resourceUuid, expDate);
78 dispatch(resourcesActions.SET_RESOURCES([sharingToken]));
79 dispatch(snackbarActions.OPEN_SNACKBAR({
80 message: 'Sharing URL created',
82 kind: SnackbarKind.SUCCESS,
85 dispatch(snackbarActions.OPEN_SNACKBAR({
86 message: 'Failed to create sharing URL',
88 kind: SnackbarKind.ERROR,
91 dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
97 export const deleteSharingToken = (uuid: string) => async (dispatch: Dispatch, getState: () => RootState, { apiClientAuthorizationService }: ServiceRepository) => {
98 dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
100 await apiClientAuthorizationService.delete(uuid);
101 dispatch(resourcesActions.DELETE_RESOURCES([uuid]));
102 dispatch(snackbarActions.OPEN_SNACKBAR({
103 message: 'Sharing URL removed',
105 kind: SnackbarKind.SUCCESS,
108 dispatch(snackbarActions.OPEN_SNACKBAR({
109 message: 'Failed to remove sharing URL',
111 kind: SnackbarKind.ERROR,
114 dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
118 const loadSharingDialog = async (dispatch: Dispatch, getState: () => RootState, { apiClientAuthorizationService }: ServiceRepository) => {
120 const dialog = getDialog<SharingDialogData>(getState().dialog, SHARING_DIALOG_NAME);
121 const sharingURLsDisabled = getState().auth.config.clusterConfig.Workbench.DisableSharingURLsUI;
123 dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
125 const resourceUuid = dialog.data.resourceUuid;
126 await dispatch<any>(initializeManagementForm);
127 // For collections, we need to load the public sharing tokens
128 if (!sharingURLsDisabled && extractUuidObjectType(resourceUuid) === ResourceObjectType.COLLECTION) {
129 const sharingTokens = await apiClientAuthorizationService.listCollectionSharingTokens(resourceUuid);
130 dispatch(resourcesActions.SET_RESOURCES([...sharingTokens.items]));
133 dispatch(snackbarActions.OPEN_SNACKBAR({
134 message: 'You do not have access to share this item',
136 kind: SnackbarKind.ERROR }));
137 dispatch(dialogActions.CLOSE_DIALOG({ id: SHARING_DIALOG_NAME }));
139 dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
144 export const initializeManagementForm = async (dispatch: Dispatch, getState: () => RootState, { userService, groupsService, permissionService }: ServiceRepository) => {
146 const dialog = getDialog<SharingDialogData>(getState().dialog, SHARING_DIALOG_NAME);
150 dispatch(progressIndicatorActions.START_WORKING(SHARING_DIALOG_NAME));
151 const resourceUuid = dialog?.data.resourceUuid;
152 const { items: permissionLinks } = await permissionService.listResourcePermissions(resourceUuid);
153 dispatch<any>(initializePublicAccessForm(permissionLinks));
154 const filters = new FilterBuilder()
155 .addIn('uuid', Array.from(new Set(permissionLinks.map(({ tailUuid }) => tailUuid))))
158 const { items: users } = await userService.list({ filters, count: "none", limit: 1000 });
159 const { items: groups } = await groupsService.list({ filters, count: "none", limit: 1000 });
161 const getEmail = (tailUuid: string) => {
162 const user = users.find(({ uuid }) => uuid === tailUuid);
163 const group = groups.find(({ uuid }) => uuid === tailUuid);
171 const managementPermissions = permissionLinks
173 item.tailUuid !== getPublicGroupUuid(getState()))
174 .map(({ tailUuid, name, uuid }) => ({
175 email: getEmail(tailUuid),
176 permissions: name as PermissionLevel,
177 permissionUuid: uuid,
180 const managementFormData: SharingManagementFormData = {
181 permissions: managementPermissions,
182 initialPermissions: managementPermissions,
185 dispatch(initialize(SHARING_MANAGEMENT_FORM_NAME, managementFormData));
186 dispatch(progressIndicatorActions.STOP_WORKING(SHARING_DIALOG_NAME));
189 const initializePublicAccessForm = (permissionLinks: PermissionResource[]) =>
190 (dispatch: Dispatch, getState: () => RootState, ) => {
191 const [publicPermission] = permissionLinks
192 .filter(item => item.tailUuid === getPublicGroupUuid(getState()));
193 const publicAccessFormData: SharingPublicAccessFormData = publicPermission
195 visibility: VisibilityLevel.PUBLIC,
196 permissionUuid: publicPermission.uuid,
199 visibility: permissionLinks.length > 0
200 ? VisibilityLevel.SHARED
201 : VisibilityLevel.PRIVATE,
204 dispatch(initialize(SHARING_PUBLIC_ACCESS_FORM_NAME, publicAccessFormData));
207 const savePublicPermissionChanges = async (_: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
208 const state = getState();
209 const { user } = state.auth;
210 const dialog = getDialog<SharingDialogData>(state.dialog, SHARING_DIALOG_NAME);
211 if (dialog && user) {
212 const { permissionUuid, visibility } = getSharingPublicAccessFormData(state);
213 if (permissionUuid) {
214 if (visibility === VisibilityLevel.PUBLIC) {
215 await permissionService.update(permissionUuid, {
216 name: PermissionLevel.CAN_READ
219 await permissionService.delete(permissionUuid);
221 } else if (visibility === VisibilityLevel.PUBLIC) {
222 await permissionService.create({
223 ownerUuid: user.uuid,
224 headUuid: dialog.data.resourceUuid,
225 tailUuid: getPublicGroupUuid(state),
226 name: PermissionLevel.CAN_READ,
232 const saveManagementChanges = async (_: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
233 const state = getState();
234 const { user } = state.auth;
235 const dialog = getDialog<string>(state.dialog, SHARING_DIALOG_NAME);
236 if (dialog && user) {
237 const { initialPermissions, permissions } = getSharingMangementFormData(state);
238 const { visibility } = getSharingPublicAccessFormData(state);
239 const cancelledPermissions = visibility === VisibilityLevel.PRIVATE
244 (a, b) => a.permissionUuid === b.permissionUuid
247 const deletions = cancelledPermissions.map(({ permissionUuid }) =>
248 permissionService.delete(permissionUuid));
249 const updates = permissions.map(update =>
250 permissionService.update(update.permissionUuid, { name: update.permissions }));
251 await Promise.all([...deletions, ...updates]);
255 const sendInvitations = async (_: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
256 const state = getState();
257 const { user } = state.auth;
258 const dialog = getDialog<SharingDialogData>(state.dialog, SHARING_DIALOG_NAME);
259 if (dialog && user) {
260 const invitations = getFormValues(SHARING_INVITATION_FORM_NAME)(state) as SharingInvitationFormData;
261 const data = invitations.invitedPeople.map(invitee => ({
262 ownerUuid: user.uuid,
263 headUuid: dialog.data.resourceUuid,
264 tailUuid: invitee.uuid,
265 name: invitations.permissions
267 const changes = data.map( invitation => permissionService.create(invitation));
268 await Promise.all(changes);