# All files / src/app/core/auth/role-list role-list.component.ts

71.19% Statements 42/59
68.29% Branches 28/41
33.33% Functions 5/15
74.07% Lines 40/54

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
    1386x

    6x
    6x
    6x

    6x
    6x
    6x
    6x
    6x
    6x

    6x

    6x
    6x
    6x
    6x

    6x

    6x

    17x

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

    17x

    16x

    16x

    4x

    17x

    6x
    17x

    6x

    6x

    6x

    6x

    6x
     import { Component, OnInit } from '@angular/core';

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

    import { RoleService } from '../../../shared/api/role.service';
    import { ScopeService } from '../../../shared/api/scope.service';
    import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
    import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
    import { CellTemplate } from '../../../shared/enum/cell-template.enum';
    import { NotificationType } from '../../../shared/enum/notification-type.enum';
    import { CdTableAction } from '../../../shared/models/cd-table-action';
    import { CdTableColumn } from '../../../shared/models/cd-table-column';
    import { CdTableSelection } from '../../../shared/models/cd-table-selection';
    import { Permission } from '../../../shared/models/permissions';
    import { EmptyPipe } from '../../../shared/pipes/empty.pipe';
    import { AuthStorageService } from '../../../shared/services/auth-storage.service';
    import { NotificationService } from '../../../shared/services/notification.service';
    import { URLBuilderService } from '../../../shared/services/url-builder.service';

    const BASE_URL = 'user-management/roles';

    @Component({
      selector: 'cd-role-list',
      template: require('./role-list.component.html'),
      styles: [],
      providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
    })
    export class RoleListComponent implements OnInit {
      permission: Permission;
      tableActions: CdTableAction[];
      columns: CdTableColumn[];
      roles: Array<any>;
      scopes: Array<string>;
      selection = new CdTableSelection();

      modalRef: BsModalRef;

      constructor(
        private roleService: RoleService,
        private scopeService: ScopeService,
        private emptyPipe: EmptyPipe,
        private authStorageService: AuthStorageService,
        private modalService: BsModalService,
        private notificationService: NotificationService,
        private i18n: I18n,
        private urlBuilder: URLBuilderService,
        public actionLabels: ActionLabelsI18n
      ) {
        this.permission = this.authStorageService.getPermissions().user;
        const addAction: CdTableAction = {
          permission: 'create',
          icon: 'fa-plus',
          routerLink: () => this.urlBuilder.getCreate(),
          name: this.actionLabels.CREATE
        };
        const editAction: CdTableAction = {
          permission: 'update',
          icon: 'fa-pencil',
          disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
          routerLink: () =>
            this.selection.first() && this.urlBuilder.getEdit(this.selection.first().name),
          name: this.actionLabels.EDIT
        };
        const deleteAction: CdTableAction = {
          permission: 'delete',
          icon: 'fa-times',
          disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
          click: () => this.deleteRoleModal(),
          name: this.actionLabels.DELETE
        };
        this.tableActions = [addAction, editAction, deleteAction];
      }

      ngOnInit() {
        this.columns = [
          {
            name: this.i18n('Name'),
            prop: 'name',
            flexGrow: 3
          },
          {
            name: this.i18n('Description'),
            prop: 'description',
            flexGrow: 5,
            pipe: this.emptyPipe
          },
          {
            name: this.i18n('System Role'),
            prop: 'system',
            cellClass: 'text-center',
            flexGrow: 1,
            cellTransformation: CellTemplate.checkIcon
          }
        ];
      }

      getRoles() {
        forkJoin([this.roleService.list(), this.scopeService.list()]).subscribe(
          (data: [Array<any>, Array<string>]) => {
            this.roles = data[0];
            this.scopes = data[1];
          }
        );
      }

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

      deleteRole(role: string) {
        this.roleService.delete(role).subscribe(
          () => {
            this.getRoles();
            this.modalRef.hide();
            this.notificationService.show(
              NotificationType.success,
              this.i18n(`Deleted role '{{role_name}}'`, { role_name: role })
            );
          },
          () => {
            this.modalRef.content.stopLoadingSpinner();
          }
        );
      }

      deleteRoleModal() {
        const name = this.selection.first().name;
        this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
          initialState: {
            itemDescription: 'Role',
            submitAction: () => this.deleteRole(name)
          }
        });
      }
    }