]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-list/role-list.component.ts
bump version to 18.2.2-pve1
[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 { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import { forkJoin } from 'rxjs';
5
6 import { RoleService } from '~/app/shared/api/role.service';
7 import { ScopeService } from '~/app/shared/api/scope.service';
8 import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
9 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
10 import { FormModalComponent } from '~/app/shared/components/form-modal/form-modal.component';
11 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
12 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
13 import { Icons } from '~/app/shared/enum/icons.enum';
14 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
15 import { CdTableAction } from '~/app/shared/models/cd-table-action';
16 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
17 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
18 import { Permission } from '~/app/shared/models/permissions';
19 import { EmptyPipe } from '~/app/shared/pipes/empty.pipe';
20 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
21 import { ModalService } from '~/app/shared/services/modal.service';
22 import { NotificationService } from '~/app/shared/services/notification.service';
23 import { URLBuilderService } from '~/app/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: NgbModalRef;
42
43 constructor(
44 private roleService: RoleService,
45 private scopeService: ScopeService,
46 private emptyPipe: EmptyPipe,
47 private authStorageService: AuthStorageService,
48 private modalService: ModalService,
49 private notificationService: NotificationService,
50 private urlBuilder: URLBuilderService,
51 public actionLabels: ActionLabelsI18n
52 ) {
53 super();
54 this.permission = this.authStorageService.getPermissions().user;
55 const addAction: CdTableAction = {
56 permission: 'create',
57 icon: Icons.add,
58 routerLink: () => this.urlBuilder.getCreate(),
59 name: this.actionLabels.CREATE
60 };
61 const cloneAction: CdTableAction = {
62 permission: 'create',
63 icon: Icons.clone,
64 name: this.actionLabels.CLONE,
65 disable: () => !this.selection.hasSingleSelection,
66 click: () => this.cloneRole()
67 };
68 const editAction: CdTableAction = {
69 permission: 'update',
70 icon: Icons.edit,
71 disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
72 routerLink: () =>
73 this.selection.first() && this.urlBuilder.getEdit(this.selection.first().name),
74 name: this.actionLabels.EDIT
75 };
76 const deleteAction: CdTableAction = {
77 permission: 'delete',
78 icon: Icons.destroy,
79 disable: () => !this.selection.hasSingleSelection || this.selection.first().system,
80 click: () => this.deleteRoleModal(),
81 name: this.actionLabels.DELETE
82 };
83 this.tableActions = [addAction, cloneAction, editAction, deleteAction];
84 }
85
86 ngOnInit() {
87 this.columns = [
88 {
89 name: $localize`Name`,
90 prop: 'name',
91 flexGrow: 3
92 },
93 {
94 name: $localize`Description`,
95 prop: 'description',
96 flexGrow: 5,
97 pipe: this.emptyPipe
98 },
99 {
100 name: $localize`System Role`,
101 prop: 'system',
102 cellClass: 'text-center',
103 flexGrow: 1,
104 cellTransformation: CellTemplate.checkIcon
105 }
106 ];
107 }
108
109 getRoles() {
110 forkJoin([this.roleService.list(), this.scopeService.list()]).subscribe(
111 (data: [Array<any>, Array<string>]) => {
112 this.roles = data[0];
113 this.scopes = data[1];
114 }
115 );
116 }
117
118 updateSelection(selection: CdTableSelection) {
119 this.selection = selection;
120 }
121
122 deleteRole(role: string) {
123 this.roleService.delete(role).subscribe(
124 () => {
125 this.getRoles();
126 this.modalRef.close();
127 this.notificationService.show(NotificationType.success, $localize`Deleted role '${role}'`);
128 },
129 () => {
130 this.modalRef.componentInstance.stopLoadingSpinner();
131 }
132 );
133 }
134
135 deleteRoleModal() {
136 const name = this.selection.first().name;
137 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
138 itemDescription: 'Role',
139 itemNames: [name],
140 submitAction: () => this.deleteRole(name)
141 });
142 }
143
144 cloneRole() {
145 const name = this.selection.first().name;
146 this.modalRef = this.modalService.show(FormModalComponent, {
147 fields: [
148 {
149 type: 'text',
150 name: 'newName',
151 value: `${name}_clone`,
152 label: $localize`New name`,
153 required: true
154 }
155 ],
156 titleText: $localize`Clone Role`,
157 submitButtonText: $localize`Clone Role`,
158 onSubmit: (values: object) => {
159 this.roleService.clone(name, values['newName']).subscribe(() => {
160 this.getRoles();
161 this.notificationService.show(
162 NotificationType.success,
163 $localize`Cloned role '${values['newName']}' from '${name}'`
164 );
165 });
166 }
167 });
168 }
169 }