]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-list/role-list.component.ts
import 15.2.4
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / core / auth / role-list / role-list.component.ts
1 import { Component, OnInit } from '@angular/core';
2
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
5 import { forkJoin } from 'rxjs';
6
7 import { RoleService } from '../../../shared/api/role.service';
8 import { ScopeService } from '../../../shared/api/scope.service';
9 import { ListWithDetails } from '../../../shared/classes/list-with-details.class';
10 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
11 import { FormModalComponent } from '../../../shared/components/form-modal/form-modal.component';
12 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
13 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
14 import { Icons } from '../../../shared/enum/icons.enum';
15 import { NotificationType } from '../../../shared/enum/notification-type.enum';
16 import { CdTableAction } from '../../../shared/models/cd-table-action';
17 import { CdTableColumn } from '../../../shared/models/cd-table-column';
18 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
19 import { Permission } from '../../../shared/models/permissions';
20 import { EmptyPipe } from '../../../shared/pipes/empty.pipe';
21 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
22 import { NotificationService } from '../../../shared/services/notification.service';
23 import { URLBuilderService } from '../../../shared/services/url-builder.service';
24
25 const BASE_URL = 'user-management/roles';
26
27 @Component({
28 selector: 'cd-role-list',
29 templateUrl: './role-list.component.html',
30 styleUrls: ['./role-list.component.scss'],
31 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
32 })
33 export class RoleListComponent extends ListWithDetails implements OnInit {
34 permission: Permission;
35 tableActions: CdTableAction[];
36 columns: CdTableColumn[];
37 roles: Array<any>;
38 scopes: Array<string>;
39 selection = new CdTableSelection();
40
41 modalRef: BsModalRef;
42
43 constructor(
44 private roleService: RoleService,
45 private scopeService: ScopeService,
46 private emptyPipe: EmptyPipe,
47 private authStorageService: AuthStorageService,
48 private modalService: BsModalService,
49 private notificationService: NotificationService,
50 private i18n: I18n,
51 private urlBuilder: URLBuilderService,
52 public actionLabels: ActionLabelsI18n
53 ) {
54 super();
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 cloneAction: CdTableAction = {
63 permission: 'create',
64 icon: Icons.clone,
65 name: this.actionLabels.CLONE,
66 disable: () => !this.selection.hasSingleSelection,
67 click: () => this.cloneRole()
68 };
69 const editAction: CdTableAction = {
70 permission: 'update',
71 icon: Icons.edit,
72 disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
73 routerLink: () =>
74 this.selection.first() && this.urlBuilder.getEdit(this.selection.first().name),
75 name: this.actionLabels.EDIT
76 };
77 const deleteAction: CdTableAction = {
78 permission: 'delete',
79 icon: Icons.destroy,
80 disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
81 click: () => this.deleteRoleModal(),
82 name: this.actionLabels.DELETE
83 };
84 this.tableActions = [addAction, cloneAction, editAction, deleteAction];
85 }
86
87 ngOnInit() {
88 this.columns = [
89 {
90 name: this.i18n('Name'),
91 prop: 'name',
92 flexGrow: 3
93 },
94 {
95 name: this.i18n('Description'),
96 prop: 'description',
97 flexGrow: 5,
98 pipe: this.emptyPipe
99 },
100 {
101 name: this.i18n('System Role'),
102 prop: 'system',
103 cellClass: 'text-center',
104 flexGrow: 1,
105 cellTransformation: CellTemplate.checkIcon
106 }
107 ];
108 }
109
110 getRoles() {
111 forkJoin([this.roleService.list(), this.scopeService.list()]).subscribe(
112 (data: [Array<any>, Array<string>]) => {
113 this.roles = data[0];
114 this.scopes = data[1];
115 }
116 );
117 }
118
119 updateSelection(selection: CdTableSelection) {
120 this.selection = selection;
121 }
122
123 deleteRole(role: string) {
124 this.roleService.delete(role).subscribe(
125 () => {
126 this.getRoles();
127 this.modalRef.hide();
128 this.notificationService.show(
129 NotificationType.success,
130 this.i18n(`Deleted role '{{role_name}}'`, { role_name: role })
131 );
132 },
133 () => {
134 this.modalRef.content.stopLoadingSpinner();
135 }
136 );
137 }
138
139 deleteRoleModal() {
140 const name = this.selection.first().name;
141 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
142 initialState: {
143 itemDescription: 'Role',
144 itemNames: [name],
145 submitAction: () => this.deleteRole(name)
146 }
147 });
148 }
149
150 cloneRole() {
151 const name = this.selection.first().name;
152 this.modalRef = this.modalService.show(FormModalComponent, {
153 initialState: {
154 fields: [
155 {
156 type: 'text',
157 name: 'newName',
158 value: `${name}_clone`,
159 label: this.i18n('New name'),
160 required: true
161 }
162 ],
163 titleText: this.i18n('Clone Role'),
164 submitButtonText: this.i18n('Clone Role'),
165 onSubmit: (values: object) => {
166 this.roleService.clone(name, values['newName']).subscribe(() => {
167 this.getRoles();
168 this.notificationService.show(
169 NotificationType.success,
170 this.i18n(`Cloned role '{{dst_name}}' from '{{src_name}}'`, {
171 src_name: name,
172 dst_name: values['newName']
173 })
174 );
175 });
176 }
177 }
178 });
179 }
180 }