]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-list/user-list.component.ts
bump version to 15.2.4-pve1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / core / auth / user-list / user-list.component.ts
1 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
2
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
5
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';
21
22 const BASE_URL = 'user-management/users';
23
24 @Component({
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) }]
29 })
30 export class UserListComponent implements OnInit {
31 @ViewChild('userRolesTpl', { static: true })
32 userRolesTpl: TemplateRef<any>;
33 @ViewChild('userEnabledTpl', { static: true })
34 userEnabledTpl: TemplateRef<any>;
35
36 permission: Permission;
37 tableActions: CdTableAction[];
38 columns: CdTableColumn[];
39 users: Array<any>;
40 selection = new CdTableSelection();
41
42 modalRef: BsModalRef;
43
44 constructor(
45 private userService: UserService,
46 private emptyPipe: EmptyPipe,
47 private modalService: BsModalService,
48 private notificationService: NotificationService,
49 private authStorageService: AuthStorageService,
50 private i18n: I18n,
51 private urlBuilder: URLBuilderService,
52 private cdDatePipe: CdDatePipe,
53 public actionLabels: ActionLabelsI18n
54 ) {
55 this.permission = this.authStorageService.getPermissions().user;
56 const addAction: CdTableAction = {
57 permission: 'create',
58 icon: Icons.add,
59 routerLink: () => this.urlBuilder.getCreate(),
60 name: this.actionLabels.CREATE
61 };
62 const editAction: CdTableAction = {
63 permission: 'update',
64 icon: Icons.edit,
65 routerLink: () =>
66 this.selection.first() && this.urlBuilder.getEdit(this.selection.first().username),
67 name: this.actionLabels.EDIT
68 };
69 const deleteAction: CdTableAction = {
70 permission: 'delete',
71 icon: Icons.destroy,
72 click: () => this.deleteUserModal(),
73 name: this.actionLabels.DELETE
74 };
75 this.tableActions = [addAction, editAction, deleteAction];
76 }
77
78 ngOnInit() {
79 this.columns = [
80 {
81 name: this.i18n('Username'),
82 prop: 'username',
83 flexGrow: 1
84 },
85 {
86 name: this.i18n('Name'),
87 prop: 'name',
88 flexGrow: 1,
89 pipe: this.emptyPipe
90 },
91 {
92 name: this.i18n('Email'),
93 prop: 'email',
94 flexGrow: 1,
95 pipe: this.emptyPipe
96 },
97 {
98 name: this.i18n('Roles'),
99 prop: 'roles',
100 flexGrow: 1,
101 cellTemplate: this.userRolesTpl
102 },
103 {
104 name: this.i18n('Enabled'),
105 prop: 'enabled',
106 flexGrow: 1,
107 cellTransformation: CellTemplate.checkIcon
108 },
109 {
110 name: this.i18n('Password expiration date'),
111 prop: 'pwdExpirationDate',
112 flexGrow: 1,
113 pipe: this.cdDatePipe
114 }
115 ];
116 }
117
118 getUsers() {
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;
123 }
124 });
125 this.users = users;
126 });
127 }
128
129 updateSelection(selection: CdTableSelection) {
130 this.selection = selection;
131 }
132
133 deleteUser(username: string) {
134 this.userService.delete(username).subscribe(
135 () => {
136 this.getUsers();
137 this.modalRef.hide();
138 this.notificationService.show(
139 NotificationType.success,
140 this.i18n('Deleted user "{{username}}"', { username: username })
141 );
142 },
143 () => {
144 this.modalRef.content.stopLoadingSpinner();
145 }
146 );
147 }
148
149 deleteUserModal() {
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 })
157 );
158 return;
159 }
160 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
161 initialState: {
162 itemDescription: 'User',
163 itemNames: [username],
164 submitAction: () => this.deleteUser(username)
165 }
166 });
167 }
168 }