1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch, MiddlewareAPI } from "redux";
6 import { DataExplorerMiddlewareService, dataExplorerToListParams, listResultsToDataExplorerItemsMeta } from "store/data-explorer/data-explorer-middleware-service";
7 import { RootState } from "store/store";
8 import { ServiceRepository } from "services/services";
9 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
10 import { DataExplorer, getDataExplorer } from "store/data-explorer/data-explorer-reducer";
11 import { FilterBuilder } from 'services/api/filter-builder';
12 import { updateResources } from 'store/resources/resources-actions';
13 import { getCurrentGroupDetailsPanelUuid, GroupPermissionsPanelActions } from 'store/group-details-panel/group-details-panel-actions';
14 import { LinkClass } from 'models/link';
15 import { ResourceKind } from 'models/resource';
17 export class GroupDetailsPanelPermissionsMiddlewareService extends DataExplorerMiddlewareService {
19 constructor(private services: ServiceRepository, id: string) {
23 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
24 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
25 const groupUuid = getCurrentGroupDetailsPanelUuid(api.getState().properties);
26 if (!dataExplorer || !groupUuid) {
27 // No-op if data explorer is not set since refresh may be triggered from elsewhere
30 const permissionsOut = await this.services.permissionService.list(getParams(dataExplorer, groupUuid));
31 api.dispatch(updateResources(permissionsOut.items));
33 api.dispatch(GroupPermissionsPanelActions.SET_ITEMS({
34 ...listResultsToDataExplorerItemsMeta(permissionsOut),
35 items: permissionsOut.items.map(item => item.uuid),
38 const usersOut = await this.services.userService.list({
39 filters: new FilterBuilder()
40 .addIn('uuid', permissionsOut.items
41 .filter((item) => item.headKind === ResourceKind.USER)
42 .map(item => item.headUuid))
46 api.dispatch(updateResources(usersOut.items));
48 const collectionsOut = await this.services.collectionService.list({
49 filters: new FilterBuilder()
50 .addIn('uuid', permissionsOut.items
51 .filter((item) => item.headKind === ResourceKind.COLLECTION)
52 .map(item => item.headUuid))
56 api.dispatch(updateResources(collectionsOut.items));
58 const projectsOut = await this.services.projectService.list({
59 filters: new FilterBuilder()
60 .addIn('uuid', permissionsOut.items
61 .filter((item) => item.headKind === ResourceKind.PROJECT)
62 .map(item => item.headUuid))
66 api.dispatch(updateResources(projectsOut.items));
68 api.dispatch(couldNotFetchGroupDetailsContents());
74 export const getParams = (dataExplorer: DataExplorer, groupUuid: string) => ({
75 ...dataExplorerToListParams(dataExplorer),
76 filters: getFilters(groupUuid),
79 export const getFilters = (groupUuid: string) => {
80 const filters = new FilterBuilder()
81 .addEqual('tail_uuid', groupUuid)
82 .addEqual('link_class', LinkClass.PERMISSION)
88 const couldNotFetchGroupDetailsContents = () =>
89 snackbarActions.OPEN_SNACKBAR({
90 message: 'Could not fetch group permissions.',
91 kind: SnackbarKind.ERROR