]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-list/rgw-user-list.component.ts
import ceph pacific 16.2.5
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / rgw / rgw-user-list / rgw-user-list.component.ts
CommitLineData
f67539c2 1import { Component, NgZone, OnInit, TemplateRef, ViewChild } from '@angular/core';
11fdf7f2 2
b3b6e05e 3import _ from 'lodash';
11fdf7f2
TL
4import { forkJoin as observableForkJoin, Observable, Subscriber } from 'rxjs';
5
f67539c2
TL
6import { RgwUserService } from '~/app/shared/api/rgw-user.service';
7import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
8import { TableStatus } from '~/app/shared/classes/table-status';
9import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
10import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
11import { TableComponent } from '~/app/shared/datatable/table/table.component';
12import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
13import { Icons } from '~/app/shared/enum/icons.enum';
14import { CdTableAction } from '~/app/shared/models/cd-table-action';
15import { CdTableColumn } from '~/app/shared/models/cd-table-column';
16import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
17import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
18import { Permission } from '~/app/shared/models/permissions';
19import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
20import { ModalService } from '~/app/shared/services/modal.service';
21import { URLBuilderService } from '~/app/shared/services/url-builder.service';
11fdf7f2
TL
22
23const BASE_URL = 'rgw/user';
24
25@Component({
26 selector: 'cd-rgw-user-list',
27 templateUrl: './rgw-user-list.component.html',
28 styleUrls: ['./rgw-user-list.component.scss'],
29 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
30})
f67539c2 31export class RgwUserListComponent extends ListWithDetails implements OnInit {
9f95a23c 32 @ViewChild(TableComponent, { static: true })
11fdf7f2 33 table: TableComponent;
f67539c2
TL
34 @ViewChild('userSizeTpl', { static: true })
35 userSizeTpl: TemplateRef<any>;
36 @ViewChild('userObjectTpl', { static: true })
37 userObjectTpl: TemplateRef<any>;
11fdf7f2
TL
38 permission: Permission;
39 tableActions: CdTableAction[];
40 columns: CdTableColumn[] = [];
41 users: object[] = [];
42 selection: CdTableSelection = new CdTableSelection();
f67539c2 43 tableStatus = new TableStatus();
1911f103 44 staleTimeout: number;
11fdf7f2
TL
45
46 constructor(
47 private authStorageService: AuthStorageService,
48 private rgwUserService: RgwUserService,
f67539c2 49 private modalService: ModalService,
11fdf7f2 50 private urlBuilder: URLBuilderService,
1911f103
TL
51 public actionLabels: ActionLabelsI18n,
52 private ngZone: NgZone
11fdf7f2 53 ) {
e306af50 54 super();
f67539c2
TL
55 }
56
57 ngOnInit() {
11fdf7f2
TL
58 this.permission = this.authStorageService.getPermissions().rgw;
59 this.columns = [
60 {
f67539c2 61 name: $localize`Username`,
11fdf7f2
TL
62 prop: 'uid',
63 flexGrow: 1
64 },
65 {
f67539c2
TL
66 name: $localize`Tenant`,
67 prop: 'tenant',
68 flexGrow: 1
69 },
70 {
71 name: $localize`Full name`,
11fdf7f2
TL
72 prop: 'display_name',
73 flexGrow: 1
74 },
75 {
f67539c2 76 name: $localize`Email address`,
11fdf7f2
TL
77 prop: 'email',
78 flexGrow: 1
79 },
80 {
f67539c2 81 name: $localize`Suspended`,
11fdf7f2
TL
82 prop: 'suspended',
83 flexGrow: 1,
84 cellClass: 'text-center',
85 cellTransformation: CellTemplate.checkIcon
86 },
87 {
f67539c2 88 name: $localize`Max. buckets`,
11fdf7f2 89 prop: 'max_buckets',
9f95a23c
TL
90 flexGrow: 1,
91 cellTransformation: CellTemplate.map,
92 customTemplateConfig: {
f67539c2
TL
93 '-1': $localize`Disabled`,
94 0: $localize`Unlimited`
9f95a23c 95 }
f67539c2
TL
96 },
97 {
98 name: $localize`Capacity Limit %`,
99 prop: 'size_usage',
100 cellTemplate: this.userSizeTpl,
101 flexGrow: 0.8
102 },
103 {
104 name: $localize`Object Limit %`,
105 prop: 'object_usage',
106 cellTemplate: this.userObjectTpl,
107 flexGrow: 0.8
11fdf7f2
TL
108 }
109 ];
110 const getUserUri = () =>
111 this.selection.first() && `${encodeURIComponent(this.selection.first().uid)}`;
112 const addAction: CdTableAction = {
113 permission: 'create',
9f95a23c 114 icon: Icons.add,
11fdf7f2 115 routerLink: () => this.urlBuilder.getCreate(),
9f95a23c
TL
116 name: this.actionLabels.CREATE,
117 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
11fdf7f2
TL
118 };
119 const editAction: CdTableAction = {
120 permission: 'update',
9f95a23c 121 icon: Icons.edit,
11fdf7f2
TL
122 routerLink: () => this.urlBuilder.getEdit(getUserUri()),
123 name: this.actionLabels.EDIT
124 };
125 const deleteAction: CdTableAction = {
126 permission: 'delete',
9f95a23c 127 icon: Icons.destroy,
11fdf7f2 128 click: () => this.deleteAction(),
9f95a23c
TL
129 disable: () => !this.selection.hasSelection,
130 name: this.actionLabels.DELETE,
131 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
11fdf7f2
TL
132 };
133 this.tableActions = [addAction, editAction, deleteAction];
1911f103
TL
134 this.timeConditionReached();
135 }
136
137 timeConditionReached() {
138 clearTimeout(this.staleTimeout);
139 this.ngZone.runOutsideAngular(() => {
140 this.staleTimeout = window.setTimeout(() => {
141 this.ngZone.run(() => {
f67539c2
TL
142 this.tableStatus = new TableStatus(
143 'warning',
144 $localize`The user list data might be stale. If needed, you can manually reload it.`
145 );
1911f103
TL
146 });
147 }, 10000);
148 });
11fdf7f2
TL
149 }
150
151 getUserList(context: CdTableFetchDataContext) {
f67539c2 152 this.tableStatus = new TableStatus();
1911f103 153 this.timeConditionReached();
11fdf7f2
TL
154 this.rgwUserService.list().subscribe(
155 (resp: object[]) => {
156 this.users = resp;
157 },
158 () => {
159 context.error();
160 }
161 );
162 }
163
164 updateSelection(selection: CdTableSelection) {
165 this.selection = selection;
166 }
167
168 deleteAction() {
f67539c2
TL
169 this.modalService.show(CriticalConfirmationModalComponent, {
170 itemDescription: this.selection.hasSingleSelection ? $localize`user` : $localize`users`,
171 itemNames: this.selection.selected.map((user: any) => user['uid']),
172 submitActionObservable: (): Observable<any> => {
173 return new Observable((observer: Subscriber<any>) => {
174 // Delete all selected data table rows.
175 observableForkJoin(
176 this.selection.selected.map((user: any) => {
177 return this.rgwUserService.delete(user.uid);
178 })
179 ).subscribe({
180 error: (error) => {
181 // Forward the error to the observer.
182 observer.error(error);
183 // Reload the data table content because some deletions might
184 // have been executed successfully in the meanwhile.
185 this.table.refreshBtn();
186 },
187 complete: () => {
188 // Notify the observer that we are done.
189 observer.complete();
190 // Reload the data table content.
191 this.table.refreshBtn();
192 }
11fdf7f2 193 });
f67539c2 194 });
11fdf7f2
TL
195 }
196 });
197 }
198}