1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { ServiceRepository } from 'services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import { DataExplorerMiddlewareService, listResultsToDataExplorerItemsMeta } from 'store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from 'store/store';
9 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
10 import { getCurrentUserProfilePanelUuid, UserProfileGroupsActions } from 'store/user-profile/user-profile-actions';
11 import { updateResources } from 'store/resources/resources-actions';
12 import { FilterBuilder } from 'services/api/filter-builder';
13 import { LinkClass } from 'models/link';
14 import { ResourceKind } from 'models/resource';
15 import { GroupClass } from 'models/group';
16 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
18 export class UserProfileGroupsMiddlewareService extends DataExplorerMiddlewareService {
19 constructor(private services: ServiceRepository, id: string) {
23 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
24 const state = api.getState();
25 const userUuid = getCurrentUserProfilePanelUuid(state.properties);
27 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
30 const user = await this.services.userService.get(userUuid || '');
31 api.dispatch(updateResources([user]));
33 // Get user's group memberships
34 const groupMembershipLinks = await this.services.permissionService.list({
35 filters: new FilterBuilder()
36 .addEqual('tail_uuid', userUuid)
37 .addEqual('link_class', LinkClass.PERMISSION)
38 .addEqual('head_kind', ResourceKind.GROUP)
41 // Update resources, includes "project" groups
42 api.dispatch(updateResources(groupMembershipLinks.items));
44 // Get user's groups details and filter to role groups
45 const groups = await this.services.groupsService.list({
46 filters: new FilterBuilder()
47 .addIn('uuid', groupMembershipLinks.items
48 .map(item => item.headUuid))
49 .addEqual('group_class', GroupClass.ROLE)
53 api.dispatch(updateResources(groups.items));
55 // Get permission links for only role groups
56 const roleGroupMembershipLinks = await this.services.permissionService.list({
57 filters: new FilterBuilder()
58 .addIn('head_uuid', groups.items.map(item => item.uuid))
59 .addEqual('tail_uuid', userUuid)
60 .addEqual('link_class', LinkClass.PERMISSION)
61 .addEqual('head_kind', ResourceKind.GROUP)
65 api.dispatch(UserProfileGroupsActions.SET_ITEMS({
66 ...listResultsToDataExplorerItemsMeta(roleGroupMembershipLinks),
67 items: roleGroupMembershipLinks.items.map(item => item.uuid),
70 api.dispatch(couldNotFetchGroups());
72 api.dispatch(progressIndicatorActions.STOP_WORKING(this.getId()));
77 const couldNotFetchGroups = () =>
78 snackbarActions.OPEN_SNACKBAR({
79 message: 'Could not fetch groups.',
80 kind: SnackbarKind.ERROR