]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-list/user-list.component.ts
import ceph 14.2.5
[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 { 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';
18
19 const BASE_URL = 'user-management/users';
20
21 @Component({
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) }]
26 })
27 export class UserListComponent implements OnInit {
28 @ViewChild('userRolesTpl')
29 userRolesTpl: TemplateRef<any>;
30
31 permission: Permission;
32 tableActions: CdTableAction[];
33 columns: CdTableColumn[];
34 users: Array<any>;
35 selection = new CdTableSelection();
36
37 modalRef: BsModalRef;
38
39 constructor(
40 private userService: UserService,
41 private emptyPipe: EmptyPipe,
42 private modalService: BsModalService,
43 private notificationService: NotificationService,
44 private authStorageService: AuthStorageService,
45 private i18n: I18n,
46 private urlBuilder: URLBuilderService,
47 public actionLabels: ActionLabelsI18n
48 ) {
49 this.permission = this.authStorageService.getPermissions().user;
50 const addAction: CdTableAction = {
51 permission: 'create',
52 icon: 'fa-plus',
53 routerLink: () => this.urlBuilder.getCreate(),
54 name: this.actionLabels.CREATE
55 };
56 const editAction: CdTableAction = {
57 permission: 'update',
58 icon: 'fa-pencil',
59 routerLink: () =>
60 this.selection.first() && this.urlBuilder.getEdit(this.selection.first().username),
61 name: this.actionLabels.EDIT
62 };
63 const deleteAction: CdTableAction = {
64 permission: 'delete',
65 icon: 'fa-times',
66 click: () => this.deleteUserModal(),
67 name: this.actionLabels.DELETE
68 };
69 this.tableActions = [addAction, editAction, deleteAction];
70 }
71
72 ngOnInit() {
73 this.columns = [
74 {
75 name: this.i18n('Username'),
76 prop: 'username',
77 flexGrow: 1
78 },
79 {
80 name: this.i18n('Name'),
81 prop: 'name',
82 flexGrow: 1,
83 pipe: this.emptyPipe
84 },
85 {
86 name: this.i18n('Email'),
87 prop: 'email',
88 flexGrow: 1,
89 pipe: this.emptyPipe
90 },
91 {
92 name: this.i18n('Roles'),
93 prop: 'roles',
94 flexGrow: 1,
95 cellTemplate: this.userRolesTpl
96 }
97 ];
98 }
99
100 getUsers() {
101 this.userService.list().subscribe((users: Array<any>) => {
102 this.users = users;
103 });
104 }
105
106 updateSelection(selection: CdTableSelection) {
107 this.selection = selection;
108 }
109
110 deleteUser(username: string) {
111 this.userService.delete(username).subscribe(
112 () => {
113 this.getUsers();
114 this.modalRef.hide();
115 this.notificationService.show(
116 NotificationType.success,
117 this.i18n('Deleted user "{{username}}"', { username: username })
118 );
119 },
120 () => {
121 this.modalRef.content.stopLoadingSpinner();
122 }
123 );
124 }
125
126 deleteUserModal() {
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 })
134 );
135 return;
136 }
137 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
138 initialState: {
139 itemDescription: 'User',
140 itemNames: [username],
141 submitAction: () => this.deleteUser(username)
142 }
143 });
144 }
145 }