1 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
6 import { UserService } from '../../../shared/api/user.service';
7 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
8 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
9 import { NotificationType } from '../../../shared/enum/notification-type.enum';
10 import { CdTableAction } from '../../../shared/models/cd-table-action';
11 import { CdTableColumn } from '../../../shared/models/cd-table-column';
12 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
13 import { Permission } from '../../../shared/models/permissions';
14 import { EmptyPipe } from '../../../shared/pipes/empty.pipe';
15 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
16 import { NotificationService } from '../../../shared/services/notification.service';
17 import { URLBuilderService } from '../../../shared/services/url-builder.service';
19 const BASE_URL = 'user-management/users';
22 selector: 'cd-user-list',
23 templateUrl: './user-list.component.html',
24 styleUrls: ['./user-list.component.scss'],
25 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
27 export class UserListComponent implements OnInit {
28 @ViewChild('userRolesTpl')
29 userRolesTpl: TemplateRef<any>;
31 permission: Permission;
32 tableActions: CdTableAction[];
33 columns: CdTableColumn[];
35 selection = new CdTableSelection();
40 private userService: UserService,
41 private emptyPipe: EmptyPipe,
42 private modalService: BsModalService,
43 private notificationService: NotificationService,
44 private authStorageService: AuthStorageService,
46 private urlBuilder: URLBuilderService,
47 public actionLabels: ActionLabelsI18n
49 this.permission = this.authStorageService.getPermissions().user;
50 const addAction: CdTableAction = {
53 routerLink: () => this.urlBuilder.getCreate(),
54 name: this.actionLabels.CREATE
56 const editAction: CdTableAction = {
60 this.selection.first() && this.urlBuilder.getEdit(this.selection.first().username),
61 name: this.actionLabels.EDIT
63 const deleteAction: CdTableAction = {
66 click: () => this.deleteUserModal(),
67 name: this.actionLabels.DELETE
69 this.tableActions = [addAction, editAction, deleteAction];
75 name: this.i18n('Username'),
80 name: this.i18n('Name'),
86 name: this.i18n('Email'),
92 name: this.i18n('Roles'),
95 cellTemplate: this.userRolesTpl
101 this.userService.list().subscribe((users: Array<any>) => {
106 updateSelection(selection: CdTableSelection) {
107 this.selection = selection;
110 deleteUser(username: string) {
111 this.userService.delete(username).subscribe(
114 this.modalRef.hide();
115 this.notificationService.show(
116 NotificationType.success,
117 this.i18n('Deleted user "{{username}}"', { username: username })
121 this.modalRef.content.stopLoadingSpinner();
127 const sessionUsername = this.authStorageService.getUsername();
128 const username = this.selection.first().username;
129 if (sessionUsername === username) {
130 this.notificationService.show(
131 NotificationType.error,
132 this.i18n('Failed to delete user "{{username}}"', { username: username }),
133 this.i18n('You are currently logged in as "{{username}}".', { username: username })
137 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
139 itemDescription: 'User',
140 itemNames: [username],
141 submitAction: () => this.deleteUser(username)