]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/blob
b56468b32767bb87d6748a94f7de88629f253dfc
[ceph-ci.git] /
1 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
2 import { catchError, switchMap } from 'rxjs/operators';
3 import { BehaviorSubject, Observable, of } from 'rxjs';
4
5 import _ from 'lodash';
6
7 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
8 import { CdTableAction } from '~/app/shared/models/cd-table-action';
9 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
10 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
11 import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
12 import { Permission } from '~/app/shared/models/permissions';
13
14 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
15 import { SmbService } from '~/app/shared/api/smb.service';
16 import { SMBUsersGroups } from '../smb.model';
17 import { Router } from '@angular/router';
18 import { Icons } from '~/app/shared/enum/icons.enum';
19 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
20 import { URLBuilderService } from '~/app/shared/services/url-builder.service';
21 import { DeleteConfirmationModalComponent } from '~/app/shared/components/delete-confirmation-modal/delete-confirmation-modal.component';
22 import { FinishedTask } from '~/app/shared/models/finished-task';
23 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
24 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
25
26 export const USERSGROUPS_PATH = 'cephfs/smb/standalone';
27
28 @Component({
29   selector: 'cd-smb-users-list',
30   templateUrl: './smb-usersgroups-list.component.html',
31   styleUrls: ['./smb-usersgroups-list.component.scss'],
32   providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(USERSGROUPS_PATH) }]
33 })
34 export class SmbUsersgroupsListComponent extends ListWithDetails implements OnInit {
35   @ViewChild('groupsNamesTpl', { static: true })
36   groupsNamesTpl: TemplateRef<any>;
37   columns: CdTableColumn[];
38   permission: Permission;
39   tableActions: CdTableAction[];
40   context: CdTableFetchDataContext;
41
42   usersGroups$: Observable<SMBUsersGroups[]>;
43   subject$ = new BehaviorSubject<SMBUsersGroups[]>([]);
44   selection: CdTableSelection = new CdTableSelection();
45
46   constructor(
47     private router: Router,
48     private urlBuilder: URLBuilderService,
49     private authStorageService: AuthStorageService,
50     public actionLabels: ActionLabelsI18n,
51     private smbService: SmbService,
52     private modalService: ModalCdsService,
53     private taskWrapper: TaskWrapperService
54   ) {
55     super();
56     this.permission = this.authStorageService.getPermissions().smb;
57   }
58
59   ngOnInit() {
60     this.columns = [
61       {
62         name: $localize`Name`,
63         prop: 'users_groups_id',
64         flexGrow: 2
65       },
66       {
67         name: $localize`Number of users`,
68         prop: 'values.users.length',
69         flexGrow: 2
70       },
71       {
72         name: $localize`Groups`,
73         prop: 'values.groups',
74         cellTemplate: this.groupsNamesTpl,
75         flexGrow: 2
76       },
77       {
78         name: $localize`Linked to cluster`,
79         prop: 'values.linked_to_cluster',
80         flexGrow: 2
81       }
82     ];
83
84     this.tableActions = [
85       {
86         name: `${this.actionLabels.CREATE} standalone`,
87         permission: 'create',
88         icon: Icons.add,
89         click: () => this.router.navigate([this.urlBuilder.getCreate()]),
90         canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
91       },
92       {
93         name: this.actionLabels.EDIT,
94         permission: 'update',
95         icon: Icons.edit,
96         click: () =>
97           this.router.navigate([
98             this.urlBuilder.getEdit(String(this.selection.first().users_groups_id))
99           ])
100       },
101       {
102         name: this.actionLabels.DELETE,
103         permission: 'delete',
104         icon: Icons.destroy,
105         click: () => this.openDeleteModal()
106       }
107     ];
108
109     this.usersGroups$ = this.subject$.pipe(
110       switchMap(() =>
111         this.smbService.listUsersGroups().pipe(
112           catchError(() => {
113             this.context.error();
114             return of(null);
115           })
116         )
117       )
118     );
119   }
120
121   loadUsersGroups() {
122     this.subject$.next([]);
123   }
124
125   openDeleteModal() {
126     const usersGroupsId = this.selection.first().users_groups_id;
127
128     this.modalService.show(DeleteConfirmationModalComponent, {
129       itemDescription: $localize`Users and groups access resource`,
130       itemNames: [usersGroupsId],
131       submitActionObservable: () =>
132         this.taskWrapper.wrapTaskAroundCall({
133           task: new FinishedTask(`${USERSGROUPS_PATH}/${URLVerbs.DELETE}`, {
134             usersGroupsId: usersGroupsId
135           }),
136           call: this.smbService.deleteUsersgroups(usersGroupsId)
137         })
138     });
139   }
140
141   updateSelection(selection: CdTableSelection) {
142     this.selection = selection;
143   }
144 }