]> 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
import 15.2.0 Octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / rgw / rgw-user-list / rgw-user-list.component.ts
1 import { Component, ViewChild } from '@angular/core';
2
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import { BsModalService } from 'ngx-bootstrap/modal';
5 import { forkJoin as observableForkJoin, Observable, Subscriber } from 'rxjs';
6
7 import { RgwUserService } from '../../../shared/api/rgw-user.service';
8 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
9 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
10 import { TableComponent } from '../../../shared/datatable/table/table.component';
11 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
12 import { Icons } from '../../../shared/enum/icons.enum';
13 import { CdTableAction } from '../../../shared/models/cd-table-action';
14 import { CdTableColumn } from '../../../shared/models/cd-table-column';
15 import { CdTableFetchDataContext } from '../../../shared/models/cd-table-fetch-data-context';
16 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
17 import { Permission } from '../../../shared/models/permissions';
18 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
19 import { URLBuilderService } from '../../../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 {
30 @ViewChild(TableComponent, { static: true })
31 table: TableComponent;
32
33 permission: Permission;
34 tableActions: CdTableAction[];
35 columns: CdTableColumn[] = [];
36 users: object[] = [];
37 selection: CdTableSelection = new CdTableSelection();
38
39 constructor(
40 private authStorageService: AuthStorageService,
41 private rgwUserService: RgwUserService,
42 private bsModalService: BsModalService,
43 private i18n: I18n,
44 private urlBuilder: URLBuilderService,
45 public actionLabels: ActionLabelsI18n
46 ) {
47 this.permission = this.authStorageService.getPermissions().rgw;
48 this.columns = [
49 {
50 name: this.i18n('Username'),
51 prop: 'uid',
52 flexGrow: 1
53 },
54 {
55 name: this.i18n('Full name'),
56 prop: 'display_name',
57 flexGrow: 1
58 },
59 {
60 name: this.i18n('Email address'),
61 prop: 'email',
62 flexGrow: 1
63 },
64 {
65 name: this.i18n('Suspended'),
66 prop: 'suspended',
67 flexGrow: 1,
68 cellClass: 'text-center',
69 cellTransformation: CellTemplate.checkIcon
70 },
71 {
72 name: this.i18n('Max. buckets'),
73 prop: 'max_buckets',
74 flexGrow: 1,
75 cellTransformation: CellTemplate.map,
76 customTemplateConfig: {
77 '-1': this.i18n('Disabled'),
78 0: this.i18n('Unlimited')
79 }
80 }
81 ];
82 const getUserUri = () =>
83 this.selection.first() && `${encodeURIComponent(this.selection.first().uid)}`;
84 const addAction: CdTableAction = {
85 permission: 'create',
86 icon: Icons.add,
87 routerLink: () => this.urlBuilder.getCreate(),
88 name: this.actionLabels.CREATE,
89 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
90 };
91 const editAction: CdTableAction = {
92 permission: 'update',
93 icon: Icons.edit,
94 routerLink: () => this.urlBuilder.getEdit(getUserUri()),
95 name: this.actionLabels.EDIT
96 };
97 const deleteAction: CdTableAction = {
98 permission: 'delete',
99 icon: Icons.destroy,
100 click: () => this.deleteAction(),
101 disable: () => !this.selection.hasSelection,
102 name: this.actionLabels.DELETE,
103 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
104 };
105 this.tableActions = [addAction, editAction, deleteAction];
106 }
107
108 getUserList(context: CdTableFetchDataContext) {
109 this.rgwUserService.list().subscribe(
110 (resp: object[]) => {
111 this.users = resp;
112 },
113 () => {
114 context.error();
115 }
116 );
117 }
118
119 updateSelection(selection: CdTableSelection) {
120 this.selection = selection;
121 }
122
123 deleteAction() {
124 this.bsModalService.show(CriticalConfirmationModalComponent, {
125 initialState: {
126 itemDescription: this.selection.hasSingleSelection ? this.i18n('user') : this.i18n('users'),
127 itemNames: this.selection.selected.map((user: any) => user['uid']),
128 submitActionObservable: (): Observable<any> => {
129 return new Observable((observer: Subscriber<any>) => {
130 // Delete all selected data table rows.
131 observableForkJoin(
132 this.selection.selected.map((user: any) => {
133 return this.rgwUserService.delete(user.uid);
134 })
135 ).subscribe(
136 null,
137 (error) => {
138 // Forward the error to the observer.
139 observer.error(error);
140 // Reload the data table content because some deletions might
141 // have been executed successfully in the meanwhile.
142 this.table.refreshBtn();
143 },
144 () => {
145 // Notify the observer that we are done.
146 observer.complete();
147 // Reload the data table content.
148 this.table.refreshBtn();
149 }
150 );
151 });
152 }
153 }
154 });
155 }
156 }