1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from 'redux';
6 import { reset, startSubmit, stopSubmit, FormErrors } from 'redux-form';
7 import { bindDataExplorerActions } from "~/store/data-explorer/data-explorer-action";
8 import { dialogActions } from '~/store/dialog/dialog-actions';
9 import { Person } from '~/views-components/sharing-dialog/people-select';
10 import { RootState } from '~/store/store';
11 import { ServiceRepository } from '~/services/services';
12 import { getResource } from '~/store/resources/resources';
13 import { GroupResource } from '~/models/group';
14 import { getCommonResourceServiceError, CommonResourceServiceError } from '~/services/common-service/common-resource-service';
15 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
16 import { PermissionLevel, PermissionResource } from '~/models/permission';
17 import { PermissionService } from '~/services/permission-service/permission-service';
19 export const GROUPS_PANEL_ID = "groupsPanel";
20 export const CREATE_GROUP_DIALOG = "createGroupDialog";
21 export const CREATE_GROUP_FORM = "createGroupForm";
22 export const CREATE_GROUP_NAME_FIELD_NAME = 'name';
23 export const CREATE_GROUP_USERS_FIELD_NAME = 'users';
24 export const GROUP_ATTRIBUTES_DIALOG = 'groupAttributesDialog';
25 export const GROUP_REMOVE_DIALOG = 'groupRemoveDialog';
26 export const MEMBER_ATTRIBUTES_DIALOG = 'memberAttributesDialog';
27 export const MEMBER_REMOVE_DIALOG = 'memberRemoveDialog';
29 export const GroupsPanelActions = bindDataExplorerActions(GROUPS_PANEL_ID);
31 export const loadGroupsPanel = () => GroupsPanelActions.REQUEST_ITEMS();
33 export const openCreateGroupDialog = () =>
34 (dispatch: Dispatch) => {
35 dispatch(dialogActions.OPEN_DIALOG({ id: CREATE_GROUP_DIALOG, data: {} }));
36 dispatch(reset(CREATE_GROUP_FORM));
39 export const openGroupAttributes = (uuid: string) =>
40 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
41 const { resources } = getState();
42 const data = getResource<GroupResource>(uuid)(resources);
43 dispatch(dialogActions.OPEN_DIALOG({ id: GROUP_ATTRIBUTES_DIALOG, data }));
46 export const openGroupMemberAttributes = (uuid: string) =>
47 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
48 const { resources } = getState();
49 const data = getResource<PermissionResource>(uuid)(resources);
50 dispatch(dialogActions.OPEN_DIALOG({ id: MEMBER_ATTRIBUTES_DIALOG, data }));
53 export const removeGroup = (uuid: string) =>
54 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
56 await services.groupsService.delete(uuid);
57 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
58 dispatch<any>(loadGroupsPanel());
61 export const openRemoveGroupDialog = (uuid: string) =>
62 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
63 dispatch(dialogActions.OPEN_DIALOG({
64 id: GROUP_REMOVE_DIALOG,
66 title: 'Remove group',
67 text: 'Are you sure you want to remove this group?',
68 confirmButtonLabel: 'Remove',
74 export const removeGroupMember = (uuid: string) =>
75 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
76 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
77 await services.permissionService.delete(uuid);
78 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
79 dispatch<any>(loadGroupsPanel());
82 export const openRemoveGroupMemberDialog = (uuid: string) =>
83 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
84 dispatch(dialogActions.OPEN_DIALOG({
85 id: MEMBER_REMOVE_DIALOG,
87 title: 'Remove member',
88 text: 'Are you sure you want to remove this member from this group?',
89 confirmButtonLabel: 'Remove',
95 export interface CreateGroupFormData {
96 [CREATE_GROUP_NAME_FIELD_NAME]: string;
97 [CREATE_GROUP_USERS_FIELD_NAME]?: Person[];
100 export const createGroup = ({ name, users = [] }: CreateGroupFormData) =>
101 async (dispatch: Dispatch, _: {}, { groupsService, permissionService }: ServiceRepository) => {
103 dispatch(startSubmit(CREATE_GROUP_FORM));
107 const newGroup = await groupsService.create({ name });
109 for (const user of users) {
111 await addGroupMember({
120 dispatch(dialogActions.CLOSE_DIALOG({ id: CREATE_GROUP_DIALOG }));
121 dispatch(reset(CREATE_GROUP_FORM));
122 dispatch(loadGroupsPanel());
123 dispatch(snackbarActions.OPEN_SNACKBAR({
124 message: `${newGroup.name} group has been created`,
125 kind: SnackbarKind.SUCCESS
132 const error = getCommonResourceServiceError(e);
133 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
134 dispatch(stopSubmit(CREATE_GROUP_FORM, { name: 'Group with the same name already exists.' } as FormErrors));
142 interface AddGroupMemberArgs {
143 user: { uuid: string, name: string };
144 group: { uuid: string, name: string };
146 permissionService: PermissionService;
149 const addGroupMember = async ({ user, group, ...args }: AddGroupMemberArgs) => {
151 await createPermissionLink({
154 permissionLevel: PermissionLevel.CAN_MANAGE,
158 await createPermissionLink({
161 permissionLevel: PermissionLevel.CAN_READ,
167 interface CreatePermissionLinkArgs {
168 head: { uuid: string, name: string };
169 tail: { uuid: string, name: string };
170 permissionLevel: PermissionLevel;
172 permissionService: PermissionService;
175 const createPermissionLink = async ({ head, tail, permissionLevel, dispatch, permissionService }: CreatePermissionLinkArgs) => {
179 await permissionService.create({
182 name: permissionLevel,
187 dispatch(snackbarActions.OPEN_SNACKBAR({
188 message: `Could not add ${tail.name} -> ${head.name} relation`,
189 kind: SnackbarKind.ERROR,