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 { CellTemplate } from '../../../shared/enum/cell-template.enum';
10 import { Icons } from '../../../shared/enum/icons.enum';
11 import { NotificationType } from '../../../shared/enum/notification-type.enum';
12 import { CdTableAction } from '../../../shared/models/cd-table-action';
13 import { CdTableColumn } from '../../../shared/models/cd-table-column';
14 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
15 import { Permission } from '../../../shared/models/permissions';
16 import { CdDatePipe } from '../../../shared/pipes/cd-date.pipe';
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/users';
25 selector: 'cd-user-list',
26 templateUrl: './user-list.component.html',
27 styleUrls: ['./user-list.component.scss'],
28 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
30 export class UserListComponent implements OnInit {
31 @ViewChild('userRolesTpl', { static: true })
32 userRolesTpl: TemplateRef<any>;
33 @ViewChild('userEnabledTpl', { static: true })
34 userEnabledTpl: TemplateRef<any>;
36 permission: Permission;
37 tableActions: CdTableAction[];
38 columns: CdTableColumn[];
40 selection = new CdTableSelection();
45 private userService: UserService,
46 private emptyPipe: EmptyPipe,
47 private modalService: BsModalService,
48 private notificationService: NotificationService,
49 private authStorageService: AuthStorageService,
51 private urlBuilder: URLBuilderService,
52 private cdDatePipe: CdDatePipe,
53 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 editAction: CdTableAction = {
66 this.selection.first() && this.urlBuilder.getEdit(this.selection.first().username),
67 name: this.actionLabels.EDIT
69 const deleteAction: CdTableAction = {
72 click: () => this.deleteUserModal(),
73 name: this.actionLabels.DELETE
75 this.tableActions = [addAction, editAction, deleteAction];
81 name: this.i18n('Username'),
86 name: this.i18n('Name'),
92 name: this.i18n('Email'),
98 name: this.i18n('Roles'),
101 cellTemplate: this.userRolesTpl
104 name: this.i18n('Enabled'),
107 cellTransformation: CellTemplate.checkIcon
110 name: this.i18n('Password expiration date'),
111 prop: 'pwdExpirationDate',
113 pipe: this.cdDatePipe
119 this.userService.list().subscribe((users: Array<any>) => {
120 users.forEach((user) => {
121 if (user['pwdExpirationDate'] && user['pwdExpirationDate'] > 0) {
122 user['pwdExpirationDate'] = user['pwdExpirationDate'] * 1000;
129 updateSelection(selection: CdTableSelection) {
130 this.selection = selection;
133 deleteUser(username: string) {
134 this.userService.delete(username).subscribe(
137 this.modalRef.hide();
138 this.notificationService.show(
139 NotificationType.success,
140 this.i18n('Deleted user "{{username}}"', { username: username })
144 this.modalRef.content.stopLoadingSpinner();
150 const sessionUsername = this.authStorageService.getUsername();
151 const username = this.selection.first().username;
152 if (sessionUsername === username) {
153 this.notificationService.show(
154 NotificationType.error,
155 this.i18n('Failed to delete user "{{username}}"', { username: username }),
156 this.i18n('You are currently logged in as "{{username}}".', { username: username })
160 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
162 itemDescription: 'User',
163 itemNames: [username],
164 submitAction: () => this.deleteUser(username)