1 import { Component, OnInit } from '@angular/core';
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
5 import { forkJoin } from 'rxjs';
7 import { RoleService } from '../../../shared/api/role.service';
8 import { ScopeService } from '../../../shared/api/scope.service';
9 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
10 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
11 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
12 import { NotificationType } from '../../../shared/enum/notification-type.enum';
13 import { CdTableAction } from '../../../shared/models/cd-table-action';
14 import { CdTableColumn } from '../../../shared/models/cd-table-column';
15 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
16 import { Permission } from '../../../shared/models/permissions';
17 import { EmptyPipe } from '../../../shared/pipes/empty.pipe';
18 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
19 import { NotificationService } from '../../../shared/services/notification.service';
20 import { URLBuilderService } from '../../../shared/services/url-builder.service';
22 const BASE_URL = 'user-management/roles';
25 selector: 'cd-role-list',
26 templateUrl: './role-list.component.html',
27 styleUrls: ['./role-list.component.scss'],
28 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
30 export class RoleListComponent implements OnInit {
31 permission: Permission;
32 tableActions: CdTableAction[];
33 columns: CdTableColumn[];
35 scopes: Array<string>;
36 selection = new CdTableSelection();
41 private roleService: RoleService,
42 private scopeService: ScopeService,
43 private emptyPipe: EmptyPipe,
44 private authStorageService: AuthStorageService,
45 private modalService: BsModalService,
46 private notificationService: NotificationService,
48 private urlBuilder: URLBuilderService,
49 public actionLabels: ActionLabelsI18n
51 this.permission = this.authStorageService.getPermissions().user;
52 const addAction: CdTableAction = {
55 routerLink: () => this.urlBuilder.getCreate(),
56 name: this.actionLabels.CREATE
58 const editAction: CdTableAction = {
61 disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
63 this.selection.first() && this.urlBuilder.getEdit(this.selection.first().name),
64 name: this.actionLabels.EDIT
66 const deleteAction: CdTableAction = {
69 disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
70 click: () => this.deleteRoleModal(),
71 name: this.actionLabels.DELETE
73 this.tableActions = [addAction, editAction, deleteAction];
79 name: this.i18n('Name'),
84 name: this.i18n('Description'),
90 name: this.i18n('System Role'),
92 cellClass: 'text-center',
94 cellTransformation: CellTemplate.checkIcon
100 forkJoin([this.roleService.list(), this.scopeService.list()]).subscribe(
101 (data: [Array<any>, Array<string>]) => {
102 this.roles = data[0];
103 this.scopes = data[1];
108 updateSelection(selection: CdTableSelection) {
109 this.selection = selection;
112 deleteRole(role: string) {
113 this.roleService.delete(role).subscribe(
116 this.modalRef.hide();
117 this.notificationService.show(
118 NotificationType.success,
119 this.i18n(`Deleted role '{{role_name}}'`, { role_name: role })
123 this.modalRef.content.stopLoadingSpinner();
129 const name = this.selection.first().name;
130 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
132 itemDescription: 'Role',
134 submitAction: () => this.deleteRole(name)