1 import { Component, NgZone, OnInit, TemplateRef, ViewChild } from '@angular/core';
3 import { forkJoin as observableForkJoin, Observable, Subscriber } from 'rxjs';
5 import { RgwUserService } from '~/app/shared/api/rgw-user.service';
6 import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
7 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
8 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
9 import { TableComponent } from '~/app/shared/datatable/table/table.component';
10 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
11 import { Icons } from '~/app/shared/enum/icons.enum';
12 import { CdTableAction } from '~/app/shared/models/cd-table-action';
13 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
14 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
15 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
16 import { Permission } from '~/app/shared/models/permissions';
17 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
18 import { ModalService } from '~/app/shared/services/modal.service';
19 import { URLBuilderService } from '~/app/shared/services/url-builder.service';
21 const BASE_URL = 'rgw/user';
24 selector: 'cd-rgw-user-list',
25 templateUrl: './rgw-user-list.component.html',
26 styleUrls: ['./rgw-user-list.component.scss'],
27 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
29 export class RgwUserListComponent extends ListWithDetails implements OnInit {
30 @ViewChild(TableComponent, { static: true })
31 table: TableComponent;
32 @ViewChild('userSizeTpl', { static: true })
33 userSizeTpl: TemplateRef<any>;
34 @ViewChild('userObjectTpl', { static: true })
35 userObjectTpl: TemplateRef<any>;
36 permission: Permission;
37 tableActions: CdTableAction[];
38 columns: CdTableColumn[] = [];
40 selection: CdTableSelection = new CdTableSelection();
41 declare staleTimeout: number;
44 private authStorageService: AuthStorageService,
45 private rgwUserService: RgwUserService,
46 private modalService: ModalService,
47 private urlBuilder: URLBuilderService,
48 public actionLabels: ActionLabelsI18n,
49 protected ngZone: NgZone
55 this.permission = this.authStorageService.getPermissions().rgw;
58 name: $localize`Username`,
63 name: $localize`Tenant`,
68 name: $localize`Full name`,
73 name: $localize`Email address`,
78 name: $localize`Suspended`,
81 cellClass: 'text-center',
82 cellTransformation: CellTemplate.checkIcon
85 name: $localize`Max. buckets`,
88 cellTransformation: CellTemplate.map,
89 customTemplateConfig: {
90 '-1': $localize`Disabled`,
91 0: $localize`Unlimited`
95 name: $localize`Capacity Limit %`,
97 cellTemplate: this.userSizeTpl,
101 name: $localize`Object Limit %`,
102 prop: 'object_usage',
103 cellTemplate: this.userObjectTpl,
107 const getUserUri = () =>
108 this.selection.first() && `${encodeURIComponent(this.selection.first().uid)}`;
109 const addAction: CdTableAction = {
110 permission: 'create',
112 routerLink: () => this.urlBuilder.getCreate(),
113 name: this.actionLabels.CREATE,
114 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
116 const editAction: CdTableAction = {
117 permission: 'update',
119 routerLink: () => this.urlBuilder.getEdit(getUserUri()),
120 name: this.actionLabels.EDIT
122 const deleteAction: CdTableAction = {
123 permission: 'delete',
125 click: () => this.deleteAction(),
126 disable: () => !this.selection.hasSelection,
127 name: this.actionLabels.DELETE,
128 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
130 this.tableActions = [addAction, editAction, deleteAction];
131 this.setTableRefreshTimeout();
134 getUserList(context: CdTableFetchDataContext) {
135 this.setTableRefreshTimeout();
136 this.rgwUserService.list().subscribe(
137 (resp: object[]) => {
146 updateSelection(selection: CdTableSelection) {
147 this.selection = selection;
151 this.modalService.show(CriticalConfirmationModalComponent, {
152 itemDescription: this.selection.hasSingleSelection ? $localize`user` : $localize`users`,
153 itemNames: this.selection.selected.map((user: any) => user['uid']),
154 submitActionObservable: (): Observable<any> => {
155 return new Observable((observer: Subscriber<any>) => {
156 // Delete all selected data table rows.
158 this.selection.selected.map((user: any) => {
159 return this.rgwUserService.delete(user.uid);
163 // Forward the error to the observer.
164 observer.error(error);
165 // Reload the data table content because some deletions might
166 // have been executed successfully in the meanwhile.
167 this.table.refreshBtn();
170 // Notify the observer that we are done.
172 // Reload the data table content.
173 this.table.refreshBtn();