# All files / src/app/ceph/rgw/rgw-user-list rgw-user-list.component.ts

71.7% Statements 38/53
63.33% Branches 19/30
20% Functions 3/15
71.43% Lines 35/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    1471x

    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x

    1x

    1x
    1x

    1x

    1x

    1x

    17x
    17x
    17x

    17x
    17x
    17x
    17x
    17x
    17x
    17x

    17x
    17x

    17x

    16x

    17x

    17x
    17x

    1x

    1x

    1x
     import { Component, ViewChild } from '@angular/core';

    import { I18n } from '@ngx-translate/i18n-polyfill';
    import { BsModalService } from 'ngx-bootstrap/modal';
    import { forkJoin as observableForkJoin, Observable, Subscriber } from 'rxjs';

    import { RgwUserService } from '../../../shared/api/rgw-user.service';
    import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
    import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
    import { TableComponent } from '../../../shared/datatable/table/table.component';
    import { CellTemplate } from '../../../shared/enum/cell-template.enum';
    import { CdTableAction } from '../../../shared/models/cd-table-action';
    import { CdTableColumn } from '../../../shared/models/cd-table-column';
    import { CdTableFetchDataContext } from '../../../shared/models/cd-table-fetch-data-context';
    import { CdTableSelection } from '../../../shared/models/cd-table-selection';
    import { Permission } from '../../../shared/models/permissions';
    import { AuthStorageService } from '../../../shared/services/auth-storage.service';
    import { URLBuilderService } from '../../../shared/services/url-builder.service';

    const BASE_URL = 'rgw/user';

    @Component({
      selector: 'cd-rgw-user-list',
      template: require('./rgw-user-list.component.html'),
      styles: [],
      providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
    })
    export class RgwUserListComponent {
      @ViewChild(TableComponent)
      table: TableComponent;

      permission: Permission;
      tableActions: CdTableAction[];
      columns: CdTableColumn[] = [];
      users: object[] = [];
      selection: CdTableSelection = new CdTableSelection();

      constructor(
        private authStorageService: AuthStorageService,
        private rgwUserService: RgwUserService,
        private bsModalService: BsModalService,
        private i18n: I18n,
        private urlBuilder: URLBuilderService,
        public actionLabels: ActionLabelsI18n
      ) {
        this.permission = this.authStorageService.getPermissions().rgw;
        this.columns = [
          {
            name: this.i18n('Username'),
            prop: 'uid',
            flexGrow: 1
          },
          {
            name: this.i18n('Full name'),
            prop: 'display_name',
            flexGrow: 1
          },
          {
            name: this.i18n('Email address'),
            prop: 'email',
            flexGrow: 1
          },
          {
            name: this.i18n('Suspended'),
            prop: 'suspended',
            flexGrow: 1,
            cellClass: 'text-center',
            cellTransformation: CellTemplate.checkIcon
          },
          {
            name: this.i18n('Max. buckets'),
            prop: 'max_buckets',
            flexGrow: 1
          }
        ];
        const getUserUri = () =>
          this.selection.first() && `${encodeURIComponent(this.selection.first().uid)}`;
        const addAction: CdTableAction = {
          permission: 'create',
          icon: 'fa-plus',
          routerLink: () => this.urlBuilder.getCreate(),
          name: this.actionLabels.CREATE
        };
        const editAction: CdTableAction = {
          permission: 'update',
          icon: 'fa-pencil',
          routerLink: () => this.urlBuilder.getEdit(getUserUri()),
          name: this.actionLabels.EDIT
        };
        const deleteAction: CdTableAction = {
          permission: 'delete',
          icon: 'fa-times',
          click: () => this.deleteAction(),
          name: this.actionLabels.DELETE
        };
        this.tableActions = [addAction, editAction, deleteAction];
      }

      getUserList(context: CdTableFetchDataContext) {
        this.rgwUserService.list().subscribe(
          (resp: object[]) => {
            this.users = resp;
          },
          () => {
            context.error();
          }
        );
      }

      updateSelection(selection: CdTableSelection) {
        this.selection = selection;
      }

      deleteAction() {
        this.bsModalService.show(CriticalConfirmationModalComponent, {
          initialState: {
            itemDescription: this.selection.hasSingleSelection ? this.i18n('user') : this.i18n('users'),
            submitActionObservable: (): Observable<any> => {
              return new Observable((observer: Subscriber<any>) => {
                // Delete all selected data table rows.
                observableForkJoin(
                  this.selection.selected.map((user: any) => {
                    return this.rgwUserService.delete(user.uid);
                  })
                ).subscribe(
                  null,
                  (error) => {
                    // Forward the error to the observer.
                    observer.error(error);
                    // Reload the data table content because some deletions might
                    // have been executed successfully in the meanwhile.
                    this.table.refreshBtn();
                  },
                  () => {
                    // Notify the observer that we are done.
                    observer.complete();
                    // Reload the data table content.
                    this.table.refreshBtn();
                  }
                );
              });
            }
          }
        });
      }
    }