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 { ListWithDetails } from '../../../shared/classes/list-with-details.class';
10 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
11 import { FormModalComponent } from '../../../shared/components/form-modal/form-modal.component';
12 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
13 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
14 import { Icons } from '../../../shared/enum/icons.enum';
15 import { NotificationType } from '../../../shared/enum/notification-type.enum';
16 import { CdTableAction } from '../../../shared/models/cd-table-action';
17 import { CdTableColumn } from '../../../shared/models/cd-table-column';
18 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
19 import { Permission } from '../../../shared/models/permissions';
20 import { EmptyPipe } from '../../../shared/pipes/empty.pipe';
21 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
22 import { NotificationService } from '../../../shared/services/notification.service';
23 import { URLBuilderService } from '../../../shared/services/url-builder.service';
25 const BASE_URL = 'user-management/roles';
28 selector: 'cd-role-list',
29 templateUrl: './role-list.component.html',
30 styleUrls: ['./role-list.component.scss'],
31 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
33 export class RoleListComponent extends ListWithDetails implements OnInit {
34 permission: Permission;
35 tableActions: CdTableAction[];
36 columns: CdTableColumn[];
38 scopes: Array<string>;
39 selection = new CdTableSelection();
44 private roleService: RoleService,
45 private scopeService: ScopeService,
46 private emptyPipe: EmptyPipe,
47 private authStorageService: AuthStorageService,
48 private modalService: BsModalService,
49 private notificationService: NotificationService,
51 private urlBuilder: URLBuilderService,
52 public actionLabels: ActionLabelsI18n
55 this.permission = this.authStorageService.getPermissions().user;
56 const addAction: CdTableAction = {
59 routerLink: () => this.urlBuilder.getCreate(),
60 name: this.actionLabels.CREATE
62 const cloneAction: CdTableAction = {
65 name: this.actionLabels.CLONE,
66 disable: () => !this.selection.hasSingleSelection,
67 click: () => this.cloneRole()
69 const editAction: CdTableAction = {
72 disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
74 this.selection.first() && this.urlBuilder.getEdit(this.selection.first().name),
75 name: this.actionLabels.EDIT
77 const deleteAction: CdTableAction = {
80 disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
81 click: () => this.deleteRoleModal(),
82 name: this.actionLabels.DELETE
84 this.tableActions = [addAction, cloneAction, editAction, deleteAction];
90 name: this.i18n('Name'),
95 name: this.i18n('Description'),
101 name: this.i18n('System Role'),
103 cellClass: 'text-center',
105 cellTransformation: CellTemplate.checkIcon
111 forkJoin([this.roleService.list(), this.scopeService.list()]).subscribe(
112 (data: [Array<any>, Array<string>]) => {
113 this.roles = data[0];
114 this.scopes = data[1];
119 updateSelection(selection: CdTableSelection) {
120 this.selection = selection;
123 deleteRole(role: string) {
124 this.roleService.delete(role).subscribe(
127 this.modalRef.hide();
128 this.notificationService.show(
129 NotificationType.success,
130 this.i18n(`Deleted role '{{role_name}}'`, { role_name: role })
134 this.modalRef.content.stopLoadingSpinner();
140 const name = this.selection.first().name;
141 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
143 itemDescription: 'Role',
145 submitAction: () => this.deleteRole(name)
151 const name = this.selection.first().name;
152 this.modalRef = this.modalService.show(FormModalComponent, {
158 value: `${name}_clone`,
159 label: this.i18n('New name'),
163 titleText: this.i18n('Clone Role'),
164 submitButtonText: this.i18n('Clone Role'),
165 onSubmit: (values: object) => {
166 this.roleService.clone(name, values['newName']).subscribe(() => {
168 this.notificationService.show(
169 NotificationType.success,
170 this.i18n(`Cloned role '{{dst_name}}' from '{{src_name}}'`, {
172 dst_name: values['newName']