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 = () => GroupsPanelActions.REQUEST_ITEMS();
30 export const openCreateGroupDialog = () =>
31 (dispatch: Dispatch, getState: () => RootState) => {
32 dispatch(initialize(PROJECT_CREATE_FORM_NAME, {}));
33 dispatch(dialogActions.OPEN_DIALOG({
34 id: PROJECT_CREATE_FORM_NAME,
36 sourcePanel: GroupClass.ROLE,
41 export const openGroupAttributes = (uuid: string) =>
42 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
43 const { resources } = getState();
44 const data = getResource<GroupResource>(uuid)(resources);
45 dispatch(dialogActions.OPEN_DIALOG({ id: GROUP_ATTRIBUTES_DIALOG, data }));
48 export const removeGroup = (uuid: string) =>
49 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
50 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
51 await services.groupsService.delete(uuid);
52 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
53 dispatch<any>(loadGroupsPanel());
56 export const openRemoveGroupDialog = (uuid: string) =>
57 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
58 dispatch(dialogActions.OPEN_DIALOG({
59 id: GROUP_REMOVE_DIALOG,
61 title: 'Remove group',
62 text: 'Are you sure you want to remove this group?',
63 confirmButtonLabel: 'Remove',
69 // Group edit dialog uses project update dialog with sourcePanel set to reload the appropriate parts
70 export const openGroupUpdateDialog = (resource: ProjectUpdateFormDialogData) =>
71 (dispatch: Dispatch, getState: () => RootState) => {
72 dispatch(initialize(PROJECT_UPDATE_FORM_NAME, resource));
73 dispatch(dialogActions.OPEN_DIALOG({
74 id: PROJECT_UPDATE_FORM_NAME,
76 sourcePanel: GroupClass.ROLE,
81 export const updateGroup = (project: ProjectUpdateFormDialogData) =>
82 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
83 const uuid = project.uuid || '';
84 dispatch(startSubmit(PROJECT_UPDATE_FORM_NAME));
86 const updatedGroup = await services.groupsService.update(uuid, { name: project.name, description: project.description });
87 dispatch(GroupsPanelActions.REQUEST_ITEMS());
88 dispatch(reset(PROJECT_UPDATE_FORM_NAME));
89 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_UPDATE_FORM_NAME }));
92 dispatch(stopSubmit(PROJECT_UPDATE_FORM_NAME));
93 const error = getCommonResourceServiceError(e);
94 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
95 dispatch(stopSubmit(PROJECT_UPDATE_FORM_NAME, { name: 'Group with the same name already exists.' } as FormErrors));
101 export const createGroup = ({ name, users = [], description }: ProjectUpdateFormDialogData) =>
102 async (dispatch: Dispatch, _: {}, { groupsService, permissionService }: ServiceRepository) => {
103 dispatch(startSubmit(PROJECT_CREATE_FORM_NAME));
105 const newGroup = await groupsService.create({ name, description, groupClass: GroupClass.ROLE });
106 for (const user of users) {
107 await addGroupMember({
114 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_CREATE_FORM_NAME }));
115 dispatch(reset(PROJECT_CREATE_FORM_NAME));
116 dispatch(loadGroupsPanel());
117 dispatch(snackbarActions.OPEN_SNACKBAR({
118 message: `${newGroup.name} group has been created`,
119 kind: SnackbarKind.SUCCESS
123 const error = getCommonResourceServiceError(e);
124 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
125 dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME, { name: 'Group with the same name already exists.' } as FormErrors));
131 interface AddGroupMemberArgs {
132 user: { uuid: string, name: string };
133 group: { uuid: string, name: string };
135 permissionService: PermissionService;
139 * Group membership is determined by whether the group has can_read permission on an object.
140 * If a group G can_read an object A, then we say A is a member of G.
142 * [Permission model docs](https://doc.arvados.org/api/permission-model.html)
144 export const addGroupMember = async ({ user, group, ...args }: AddGroupMemberArgs) => {
145 await createPermission({
148 permissionLevel: PermissionLevel.CAN_READ,
153 interface CreatePermissionLinkArgs {
154 head: { uuid: string, name: string };
155 tail: { uuid: string, name: string };
156 permissionLevel: PermissionLevel;
158 permissionService: PermissionService;
161 const createPermission = async ({ head, tail, permissionLevel, dispatch, permissionService }: CreatePermissionLinkArgs) => {
163 await permissionService.create({
166 name: permissionLevel,
169 dispatch(snackbarActions.OPEN_SNACKBAR({
170 message: `Could not add ${tail.name} -> ${head.name} relation`,
171 kind: SnackbarKind.ERROR,
176 interface DeleteGroupMemberArgs {
177 link: { uuid: string };
179 permissionService: PermissionService;
182 export const deleteGroupMember = async ({ link, ...args }: DeleteGroupMemberArgs) => {
183 await deletePermission({
189 interface DeletePermissionLinkArgs {
192 permissionService: PermissionService;
195 export const deletePermission = async ({ uuid, dispatch, permissionService }: DeletePermissionLinkArgs) => {
197 const permissionsResponse = await permissionService.list({
198 filters: new FilterBuilder()
199 .addEqual('uuid', uuid)
202 const [permission] = permissionsResponse.items;
204 await permissionService.delete(permission.uuid);
206 throw new Error('Permission not found');
209 dispatch(snackbarActions.OPEN_SNACKBAR({
210 message: `Could not delete ${uuid} permission`,
211 kind: SnackbarKind.ERROR,