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';
18 import { FilterBuilder } from '~/services/api/filter-builder';
20 export const GROUPS_PANEL_ID = "groupsPanel";
21 export const CREATE_GROUP_DIALOG = "createGroupDialog";
22 export const CREATE_GROUP_FORM = "createGroupForm";
23 export const CREATE_GROUP_NAME_FIELD_NAME = 'name';
24 export const CREATE_GROUP_USERS_FIELD_NAME = 'users';
25 export const GROUP_ATTRIBUTES_DIALOG = 'groupAttributesDialog';
26 export const GROUP_REMOVE_DIALOG = 'groupRemoveDialog';
28 export const GroupsPanelActions = bindDataExplorerActions(GROUPS_PANEL_ID);
30 export const loadGroupsPanel = () => GroupsPanelActions.REQUEST_ITEMS();
32 export const openCreateGroupDialog = () =>
33 (dispatch: Dispatch) => {
34 dispatch(dialogActions.OPEN_DIALOG({ id: CREATE_GROUP_DIALOG, data: {} }));
35 dispatch(reset(CREATE_GROUP_FORM));
38 export const openGroupAttributes = (uuid: string) =>
39 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
40 const { resources } = getState();
41 const data = getResource<GroupResource>(uuid)(resources);
42 dispatch(dialogActions.OPEN_DIALOG({ id: GROUP_ATTRIBUTES_DIALOG, data }));
45 export const removeGroup = (uuid: string) =>
46 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
47 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
48 await services.groupsService.delete(uuid);
49 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
50 dispatch<any>(loadGroupsPanel());
53 export const openRemoveGroupDialog = (uuid: string) =>
54 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55 dispatch(dialogActions.OPEN_DIALOG({
56 id: GROUP_REMOVE_DIALOG,
58 title: 'Remove group',
59 text: 'Are you sure you want to remove this group?',
60 confirmButtonLabel: 'Remove',
66 export interface CreateGroupFormData {
67 [CREATE_GROUP_NAME_FIELD_NAME]: string;
68 [CREATE_GROUP_USERS_FIELD_NAME]?: Person[];
71 export const createGroup = ({ name, users = [] }: CreateGroupFormData) =>
72 async (dispatch: Dispatch, _: {}, { groupsService, permissionService }: ServiceRepository) => {
74 dispatch(startSubmit(CREATE_GROUP_FORM));
78 const newGroup = await groupsService.create({ name });
80 for (const user of users) {
82 await addGroupMember({
91 dispatch(dialogActions.CLOSE_DIALOG({ id: CREATE_GROUP_DIALOG }));
92 dispatch(reset(CREATE_GROUP_FORM));
93 dispatch(loadGroupsPanel());
94 dispatch(snackbarActions.OPEN_SNACKBAR({
95 message: `${newGroup.name} group has been created`,
96 kind: SnackbarKind.SUCCESS
103 const error = getCommonResourceServiceError(e);
104 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
105 dispatch(stopSubmit(CREATE_GROUP_FORM, { name: 'Group with the same name already exists.' } as FormErrors));
113 interface AddGroupMemberArgs {
114 user: { uuid: string, name: string };
115 group: { uuid: string, name: string };
117 permissionService: PermissionService;
121 * Group membership is determined by whether the group has can_read permission on an object.
122 * If a group G can_read an object A, then we say A is a member of G.
124 * [Permission model docs](https://doc.arvados.org/api/permission-model.html)
126 export const addGroupMember = async ({ user, group, ...args }: AddGroupMemberArgs) => {
128 await createPermission({
131 permissionLevel: PermissionLevel.CAN_READ,
137 interface CreatePermissionLinkArgs {
138 head: { uuid: string, name: string };
139 tail: { uuid: string, name: string };
140 permissionLevel: PermissionLevel;
142 permissionService: PermissionService;
145 const createPermission = async ({ head, tail, permissionLevel, dispatch, permissionService }: CreatePermissionLinkArgs) => {
149 await permissionService.create({
152 name: permissionLevel,
157 dispatch(snackbarActions.OPEN_SNACKBAR({
158 message: `Could not add ${tail.name} -> ${head.name} relation`,
159 kind: SnackbarKind.ERROR,
166 interface DeleteGroupMemberArgs {
167 user: { uuid: string, name: string };
168 group: { uuid: string, name: string };
170 permissionService: PermissionService;
173 export const deleteGroupMember = async ({ user, group, ...args }: DeleteGroupMemberArgs) => {
175 await deletePermission({
183 interface DeletePermissionLinkArgs {
184 head: { uuid: string, name: string };
185 tail: { uuid: string, name: string };
187 permissionService: PermissionService;
190 export const deletePermission = async ({ head, tail, dispatch, permissionService }: DeletePermissionLinkArgs) => {
194 const permissionsResponse = await permissionService.list({
196 filters: new FilterBuilder()
197 .addEqual('tailUuid', tail.uuid)
198 .addEqual('headUuid', head.uuid)
203 const [permission] = permissionsResponse.items;
207 await permissionService.delete(permission.uuid);
211 throw new Error('Permission not found');
218 dispatch(snackbarActions.OPEN_SNACKBAR({
219 message: `Could not delete ${tail.name} -> ${head.name} relation`,
220 kind: SnackbarKind.ERROR,