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, initialize } from 'redux-form';
7 import { bindDataExplorerActions } from "store/data-explorer/data-explorer-action";
8 import { dialogActions } from 'store/dialog/dialog-actions';
9 import { RootState } from 'store/store';
10 import { ServiceRepository } from 'services/services';
11 import { getResource } from 'store/resources/resources';
12 import { GroupResource, GroupClass } from 'models/group';
13 import { getCommonResourceServiceError, CommonResourceServiceError } from 'services/common-service/common-resource-service';
14 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
15 import { PermissionLevel } from 'models/permission';
16 import { PermissionService } from 'services/permission-service/permission-service';
17 import { FilterBuilder } from 'services/api/filter-builder';
18 import { ProjectUpdateFormDialogData, PROJECT_UPDATE_FORM_NAME } from 'store/projects/project-update-actions';
19 import { PROJECT_CREATE_FORM_NAME } from 'store/projects/project-create-actions';
21 export const GROUPS_PANEL_ID = "groupsPanel";
23 export const GROUP_ATTRIBUTES_DIALOG = 'groupAttributesDialog';
24 export const GROUP_REMOVE_DIALOG = 'groupRemoveDialog';
26 export const GroupsPanelActions = bindDataExplorerActions(GROUPS_PANEL_ID);
28 export const loadGroupsPanel = () => (dispatch: Dispatch) => {
29 dispatch(GroupsPanelActions.RESET_EXPLORER_SEARCH_VALUE());
30 dispatch(GroupsPanelActions.REQUEST_ITEMS());
33 export const openCreateGroupDialog = () =>
34 (dispatch: Dispatch, getState: () => RootState) => {
35 dispatch(initialize(PROJECT_CREATE_FORM_NAME, {}));
36 dispatch(dialogActions.OPEN_DIALOG({
37 id: PROJECT_CREATE_FORM_NAME,
39 sourcePanel: GroupClass.ROLE,
44 export const openGroupAttributes = (uuid: string) =>
45 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
46 const { resources } = getState();
47 const data = getResource<GroupResource>(uuid)(resources);
48 dispatch(dialogActions.OPEN_DIALOG({ id: GROUP_ATTRIBUTES_DIALOG, data }));
51 export const removeGroup = (uuid: string) =>
52 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
53 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
54 await services.groupsService.delete(uuid);
55 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
56 dispatch<any>(loadGroupsPanel());
59 export const openRemoveGroupDialog = (uuid: string) =>
60 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
61 dispatch(dialogActions.OPEN_DIALOG({
62 id: GROUP_REMOVE_DIALOG,
64 title: 'Remove group',
65 text: 'Are you sure you want to remove this group?',
66 confirmButtonLabel: 'Remove',
72 // Group edit dialog uses project update dialog with sourcePanel set to reload the appropriate parts
73 export const openGroupUpdateDialog = (resource: ProjectUpdateFormDialogData) =>
74 (dispatch: Dispatch, getState: () => RootState) => {
75 dispatch(initialize(PROJECT_UPDATE_FORM_NAME, resource));
76 dispatch(dialogActions.OPEN_DIALOG({
77 id: PROJECT_UPDATE_FORM_NAME,
79 sourcePanel: GroupClass.ROLE,
84 export const updateGroup = (project: ProjectUpdateFormDialogData) =>
85 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
86 const uuid = project.uuid || '';
87 dispatch(startSubmit(PROJECT_UPDATE_FORM_NAME));
89 const updatedGroup = await services.groupsService.update(uuid, { name: project.name, description: project.description });
90 dispatch(GroupsPanelActions.REQUEST_ITEMS());
91 dispatch(reset(PROJECT_UPDATE_FORM_NAME));
92 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_UPDATE_FORM_NAME }));
95 dispatch(stopSubmit(PROJECT_UPDATE_FORM_NAME));
96 const error = getCommonResourceServiceError(e);
97 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
98 dispatch(stopSubmit(PROJECT_UPDATE_FORM_NAME, { name: 'Group with the same name already exists.' } as FormErrors));
104 export const createGroup = ({ name, users = [], description }: ProjectUpdateFormDialogData) =>
105 async (dispatch: Dispatch, _: {}, { groupsService, permissionService }: ServiceRepository) => {
106 dispatch(startSubmit(PROJECT_CREATE_FORM_NAME));
108 const newGroup = await groupsService.create({ name, description, groupClass: GroupClass.ROLE });
109 for (const user of users) {
110 await addGroupMember({
117 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_CREATE_FORM_NAME }));
118 dispatch(reset(PROJECT_CREATE_FORM_NAME));
119 dispatch<any>(loadGroupsPanel());
120 dispatch(snackbarActions.OPEN_SNACKBAR({
121 message: `${newGroup.name} group has been created`,
122 kind: SnackbarKind.SUCCESS
126 const error = getCommonResourceServiceError(e);
127 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
128 dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME, { name: 'Group with the same name already exists.' } as FormErrors));
134 interface AddGroupMemberArgs {
135 user: { uuid: string, name: string };
136 group: { uuid: string, name: string };
138 permissionService: PermissionService;
142 * Group membership is determined by whether the group has can_read permission on an object.
143 * If a group G can_read an object A, then we say A is a member of G.
145 * [Permission model docs](https://doc.arvados.org/api/permission-model.html)
147 export const addGroupMember = async ({ user, group, ...args }: AddGroupMemberArgs) => {
148 await createPermission({
151 permissionLevel: PermissionLevel.CAN_READ,
156 interface CreatePermissionLinkArgs {
157 head: { uuid: string, name: string };
158 tail: { uuid: string, name: string };
159 permissionLevel: PermissionLevel;
161 permissionService: PermissionService;
164 const createPermission = async ({ head, tail, permissionLevel, dispatch, permissionService }: CreatePermissionLinkArgs) => {
166 await permissionService.create({
169 name: permissionLevel,
172 dispatch(snackbarActions.OPEN_SNACKBAR({
173 message: `Could not add ${tail.name} -> ${head.name} relation`,
174 kind: SnackbarKind.ERROR,
179 interface DeleteGroupMemberArgs {
180 link: { uuid: string };
182 permissionService: PermissionService;
185 export const deleteGroupMember = async ({ link, ...args }: DeleteGroupMemberArgs) => {
186 await deletePermission({
192 interface DeletePermissionLinkArgs {
195 permissionService: PermissionService;
198 export const deletePermission = async ({ uuid, dispatch, permissionService }: DeletePermissionLinkArgs) => {
200 const permissionsResponse = await permissionService.list({
201 filters: new FilterBuilder()
202 .addEqual('uuid', uuid)
205 const [permission] = permissionsResponse.items;
207 await permissionService.delete(permission.uuid);
209 throw new Error('Permission not found');
212 dispatch(snackbarActions.OPEN_SNACKBAR({
213 message: `Could not delete ${uuid} permission`,
214 kind: SnackbarKind.ERROR,