1 import { Component, ViewChild } from '@angular/core';
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import { BsModalService } from 'ngx-bootstrap/modal';
5 import { forkJoin as observableForkJoin, Observable, Subscriber } from 'rxjs';
7 import { RgwUserService } from '../../../shared/api/rgw-user.service';
8 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
9 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
10 import { TableComponent } from '../../../shared/datatable/table/table.component';
11 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
12 import { Icons } from '../../../shared/enum/icons.enum';
13 import { CdTableAction } from '../../../shared/models/cd-table-action';
14 import { CdTableColumn } from '../../../shared/models/cd-table-column';
15 import { CdTableFetchDataContext } from '../../../shared/models/cd-table-fetch-data-context';
16 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
17 import { Permission } from '../../../shared/models/permissions';
18 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
19 import { URLBuilderService } from '../../../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 {
30 @ViewChild(TableComponent, { static: true })
31 table: TableComponent;
33 permission: Permission;
34 tableActions: CdTableAction[];
35 columns: CdTableColumn[] = [];
37 selection: CdTableSelection = new CdTableSelection();
40 private authStorageService: AuthStorageService,
41 private rgwUserService: RgwUserService,
42 private bsModalService: BsModalService,
44 private urlBuilder: URLBuilderService,
45 public actionLabels: ActionLabelsI18n
47 this.permission = this.authStorageService.getPermissions().rgw;
50 name: this.i18n('Username'),
55 name: this.i18n('Full name'),
60 name: this.i18n('Email address'),
65 name: this.i18n('Suspended'),
68 cellClass: 'text-center',
69 cellTransformation: CellTemplate.checkIcon
72 name: this.i18n('Max. buckets'),
75 cellTransformation: CellTemplate.map,
76 customTemplateConfig: {
77 '-1': this.i18n('Disabled'),
78 0: this.i18n('Unlimited')
82 const getUserUri = () =>
83 this.selection.first() && `${encodeURIComponent(this.selection.first().uid)}`;
84 const addAction: CdTableAction = {
87 routerLink: () => this.urlBuilder.getCreate(),
88 name: this.actionLabels.CREATE,
89 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
91 const editAction: CdTableAction = {
94 routerLink: () => this.urlBuilder.getEdit(getUserUri()),
95 name: this.actionLabels.EDIT
97 const deleteAction: CdTableAction = {
100 click: () => this.deleteAction(),
101 disable: () => !this.selection.hasSelection,
102 name: this.actionLabels.DELETE,
103 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
105 this.tableActions = [addAction, editAction, deleteAction];
108 getUserList(context: CdTableFetchDataContext) {
109 this.rgwUserService.list().subscribe(
110 (resp: object[]) => {
119 updateSelection(selection: CdTableSelection) {
120 this.selection = selection;
124 this.bsModalService.show(CriticalConfirmationModalComponent, {
126 itemDescription: this.selection.hasSingleSelection ? this.i18n('user') : this.i18n('users'),
127 itemNames: this.selection.selected.map((user: any) => user['uid']),
128 submitActionObservable: (): Observable<any> => {
129 return new Observable((observer: Subscriber<any>) => {
130 // Delete all selected data table rows.
132 this.selection.selected.map((user: any) => {
133 return this.rgwUserService.delete(user.uid);
138 // Forward the error to the observer.
139 observer.error(error);
140 // Reload the data table content because some deletions might
141 // have been executed successfully in the meanwhile.
142 this.table.refreshBtn();
145 // Notify the observer that we are done.
147 // Reload the data table content.
148 this.table.refreshBtn();