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