Merge branch '20455-noopener' refs #20455
[arvados.git] / services / workbench2 / src / store / group-details-panel / group-details-panel-permissions-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
16
17 export class GroupDetailsPanelPermissionsMiddlewareService extends DataExplorerMiddlewareService {
18
19     constructor(private services: ServiceRepository, id: string) {
20         super(id);
21     }
22
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
28         } else {
29             try {
30                 const permissionsOut = await this.services.permissionService.list(getParams(dataExplorer, groupUuid));
31                 api.dispatch(updateResources(permissionsOut.items));
32
33                 api.dispatch(GroupPermissionsPanelActions.SET_ITEMS({
34                     ...listResultsToDataExplorerItemsMeta(permissionsOut),
35                     items: permissionsOut.items.map(item => item.uuid),
36                 }));
37
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))
43                         .getFilters(),
44                     count: "none"
45                 });
46                 api.dispatch(updateResources(usersOut.items));
47
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))
53                         .getFilters(),
54                     count: "none"
55                 });
56                 api.dispatch(updateResources(collectionsOut.items));
57
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))
63                         .getFilters(),
64                     count: "none"
65                 });
66                 api.dispatch(updateResources(projectsOut.items));
67             } catch (e) {
68                 api.dispatch(couldNotFetchGroupDetailsContents());
69             }
70         }
71     }
72 }
73
74 export const getParams = (dataExplorer: DataExplorer, groupUuid: string) => ({
75     ...dataExplorerToListParams(dataExplorer),
76     filters: getFilters(groupUuid),
77 });
78
79 export const getFilters = (groupUuid: string) => {
80     const filters = new FilterBuilder()
81         .addEqual('tail_uuid', groupUuid)
82         .addEqual('link_class', LinkClass.PERMISSION)
83         .getFilters();
84
85     return filters;
86 };
87
88 const couldNotFetchGroupDetailsContents = () =>
89     snackbarActions.OPEN_SNACKBAR({
90         message: 'Could not fetch group permissions.',
91         kind: SnackbarKind.ERROR
92     });