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, dataExplorerToListParams, listResultsToDataExplorerItemsMeta } from 'store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from 'store/store';
9 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
10 import { DataExplorer, getDataExplorer } from 'store/data-explorer/data-explorer-reducer';
11 import { updateResources } from 'store/resources/resources-actions';
12 import { FilterBuilder } from 'services/api/filter-builder';
13 import { SortDirection } from 'components/data-table/data-column';
14 import { OrderDirection, OrderBuilder } from 'services/api/order-builder';
15 import { ListResults } from 'services/common-service/common-service';
16 import { userBindedActions } from 'store/users/users-actions';
17 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
18 import { UserResource } from 'models/user';
19 import { UserPanelColumnNames } from 'views/user-panel/user-panel';
20 import { BuiltinGroups, getBuiltinGroupUuid } from 'models/group';
21 import { LinkClass } from 'models/link';
22 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
24 export class UserMiddlewareService extends DataExplorerMiddlewareService {
25 constructor(private services: ServiceRepository, id: string) {
29 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
30 const state = api.getState();
31 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
33 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
34 const users = await this.services.userService.list(getParams(dataExplorer));
35 api.dispatch(updateResources(users.items));
36 api.dispatch(setItems(users));
38 // Get "all users" group memberships
39 const allUsersGroupUuid = getBuiltinGroupUuid(state.auth.localCluster, BuiltinGroups.ALL);
40 const allUserMemberships = await this.services.permissionService.list({
41 filters: new FilterBuilder()
42 .addEqual('head_uuid', allUsersGroupUuid)
43 .addEqual('link_class', LinkClass.PERMISSION)
46 api.dispatch(updateResources(allUserMemberships.items));
48 api.dispatch(couldNotFetchUsers());
50 api.dispatch(progressIndicatorActions.STOP_WORKING(this.getId()));
55 const getParams = (dataExplorer: DataExplorer) => ({
56 ...dataExplorerToListParams(dataExplorer),
57 order: getOrder(dataExplorer),
58 filters: new FilterBuilder()
59 .addFullTextSearch(dataExplorer.searchValue)
63 const getOrder = (dataExplorer: DataExplorer) => {
64 const sortColumn = getSortColumn<UserResource>(dataExplorer);
65 const order = new OrderBuilder<UserResource>();
66 if (sortColumn && sortColumn.sort) {
67 const sortDirection = sortColumn.sort.direction === SortDirection.ASC
69 : OrderDirection.DESC;
71 if (sortColumn.name === UserPanelColumnNames.NAME) {
72 order.addOrder(sortDirection, "firstName")
73 .addOrder(sortDirection, "lastName");
75 order.addOrder(sortDirection, sortColumn.sort.field);
78 // Use createdAt as a secondary sort column so we break ties consistently.
79 order.addOrder(OrderDirection.DESC, "createdAt");
81 return order.getOrder();
84 export const setItems = (listResults: ListResults<UserResource>) =>
85 userBindedActions.SET_ITEMS({
86 ...listResultsToDataExplorerItemsMeta(listResults),
87 items: listResults.items.map(resource => resource.uuid),
90 const couldNotFetchUsers = () =>
91 snackbarActions.OPEN_SNACKBAR({
92 message: 'Could not fetch users.',
93 kind: SnackbarKind.ERROR