1 import { Component, OnInit } from '@angular/core';
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import { forkJoin } from 'rxjs';
6 import { RoleService } from '~/app/shared/api/role.service';
7 import { ScopeService } from '~/app/shared/api/scope.service';
8 import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
9 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
10 import { FormModalComponent } from '~/app/shared/components/form-modal/form-modal.component';
11 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
12 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
13 import { Icons } from '~/app/shared/enum/icons.enum';
14 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
15 import { CdTableAction } from '~/app/shared/models/cd-table-action';
16 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
17 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
18 import { Permission } from '~/app/shared/models/permissions';
19 import { EmptyPipe } from '~/app/shared/pipes/empty.pipe';
20 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
21 import { ModalService } from '~/app/shared/services/modal.service';
22 import { NotificationService } from '~/app/shared/services/notification.service';
23 import { URLBuilderService } from '~/app/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();
41 modalRef: NgbModalRef;
44 private roleService: RoleService,
45 private scopeService: ScopeService,
46 private emptyPipe: EmptyPipe,
47 private authStorageService: AuthStorageService,
48 private modalService: ModalService,
49 private notificationService: NotificationService,
50 private urlBuilder: URLBuilderService,
51 public actionLabels: ActionLabelsI18n
54 this.permission = this.authStorageService.getPermissions().user;
55 const addAction: CdTableAction = {
58 routerLink: () => this.urlBuilder.getCreate(),
59 name: this.actionLabels.CREATE
61 const cloneAction: CdTableAction = {
64 name: this.actionLabels.CLONE,
65 disable: () => !this.selection.hasSingleSelection,
66 click: () => this.cloneRole()
68 const editAction: CdTableAction = {
71 disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
73 this.selection.first() && this.urlBuilder.getEdit(this.selection.first().name),
74 name: this.actionLabels.EDIT
76 const deleteAction: CdTableAction = {
79 disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
80 click: () => this.deleteRoleModal(),
81 name: this.actionLabels.DELETE
83 this.tableActions = [addAction, cloneAction, editAction, deleteAction];
89 name: $localize`Name`,
94 name: $localize`Description`,
100 name: $localize`System Role`,
102 cellClass: 'text-center',
104 cellTransformation: CellTemplate.checkIcon
110 forkJoin([this.roleService.list(), this.scopeService.list()]).subscribe(
111 (data: [Array<any>, Array<string>]) => {
112 this.roles = data[0];
113 this.scopes = data[1];
118 updateSelection(selection: CdTableSelection) {
119 this.selection = selection;
122 deleteRole(role: string) {
123 this.roleService.delete(role).subscribe(
126 this.modalRef.close();
127 this.notificationService.show(NotificationType.success, $localize`Deleted role '${role}'`);
130 this.modalRef.componentInstance.stopLoadingSpinner();
136 const name = this.selection.first().name;
137 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
138 itemDescription: 'Role',
140 submitAction: () => this.deleteRole(name)
145 const name = this.selection.first().name;
146 this.modalRef = this.modalService.show(FormModalComponent, {
151 value: `${name}_clone`,
152 label: $localize`New name`,
156 titleText: $localize`Clone Role`,
157 submitButtonText: $localize`Clone Role`,
158 onSubmit: (values: object) => {
159 this.roleService.clone(name, values['newName']).subscribe(() => {
161 this.notificationService.show(
162 NotificationType.success,
163 $localize`Cloned role '${values['newName']}' from '${name}'`