fix for not displaying role sync button
[portal.git] / portal-FE-common / src / app / pages / role / role.component.ts
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 import { Component, OnInit, ViewChild } from '@angular/core';
39 import { RoleService, ApplicationsService } from 'src/app/shared/services';
40 import { HttpErrorResponse, HttpClient } from '@angular/common/http';
41 import { MatTableDataSource, MatSort, MatPaginator } from '@angular/material';
42 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
43 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
44 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
45 import { environment } from 'src/environments/environment';
46 import { BulkUploadRoleComponent } from './bulk-upload-role/bulk-upload-role.component';
47 import { AddRoleComponent } from './add-role/add-role.component';
48
49 @Component({
50   selector: 'app-role',
51   templateUrl: './role.component.html',
52   styleUrls: ['./role.component.scss']
53 })
54 export class RoleComponent implements OnInit {
55
56   selectedCentralizedApp: any;
57   centralizedApps: any;
58   showSpinner: boolean;
59   availableRoles: any[];
60   syncRolesApplied: boolean;
61   displayedColumns: string[] = ['name', 'priority', 'active', 'edit', 'delete'];
62   roleDataSource = new MatTableDataSource(this.availableRoles);
63   @ViewChild(MatSort) sort: MatSort;
64   @ViewChild(MatPaginator) paginator: MatPaginator;
65   appName: any;
66   api = environment.api;
67   availableRoleFunctions: any;
68   constructor(private roleService: RoleService, private applicationsService: ApplicationsService, public ngbModal: NgbModal, public http: HttpClient) { }
69
70   ngOnInit() {
71     this.centralizedApps = [];
72     this.availableRoles = [];
73     this.appName = '';
74     this.selectedCentralizedApp = '';
75     this.getCentralizedApps(sessionStorage.userId);
76   }
77
78   toggleRole(_element) {
79     let activeOrInactive = (_element.active) ? 'activate' : 'inactivate';
80     const modalInfoRef = this.ngbModal.open(InformationModalComponent);
81     modalInfoRef.componentInstance.title = 'Confirmation';
82     modalInfoRef.componentInstance.message = 'You are about to ' + activeOrInactive + ' the role ' + _element.name + '. Do you want to continue?';
83     modalInfoRef.result.then((_res) => {
84       if (_res === 'Ok') {
85         var uuu = this.api.toggleRole + '/' + this.selectedCentralizedApp + '/' + _element.id;
86         var postData = {
87           appId: this.selectedCentralizedApp,
88           role: _element
89         };
90         this.http.post(uuu, postData).toPromise().then((data: any) => {
91           if (typeof data === 'object' && data.restcallStatus == 'Success') {
92             this.availableRoles = data.availableRoles;
93             this.roleDataSource = new MatTableDataSource(this.availableRoles);
94             this.roleDataSource.sort = this.sort;
95             this.roleDataSource.paginator = this.paginator;
96             // $log.debug('role::availableRoles:'+$scope.availableRoles);
97           } else {
98             _element.active = !_element.active;
99             const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
100             modalErrorRef.componentInstance.title = 'Error';
101             modalErrorRef.componentInstance.message = 'Error while saving. ' + data.restCallStatus;
102           }
103
104         }, (response) => {
105           // debug.log('response:'+response);
106           _element.active = !_element.active;
107           const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
108           modalErrorRef.componentInstance.title = 'Error';
109           modalErrorRef.componentInstance.message = 'Error while saving. ' + response.restCallStatus;
110         });
111       } else {
112         _element.active = !_element.active;
113       }
114
115     }, (result) => {
116
117     })
118   }
119
120   openBulkUploadRolesAndFunctionsModal() {
121     const modalBulkUploadRole = this.ngbModal.open(BulkUploadRoleComponent);
122     modalBulkUploadRole.componentInstance.title = 'Bulk Upload Role-Function';
123     modalBulkUploadRole.componentInstance.dialogState = 1;
124     modalBulkUploadRole.componentInstance.appId = this.selectedCentralizedApp;
125   }
126
127   editRoleModalPopup(_element) {
128     this.showSpinner = true;
129     this.roleService.getRole(this.selectedCentralizedApp, _element.id).toPromise().then((data: any) => {
130       this.showSpinner = false;
131       var response = JSON.parse(data.data);
132       var availableRoleFunctions = JSON.parse(response.availableRoleFunctions);
133       const ngbModalCreateRole = this.ngbModal.open(AddRoleComponent);
134       ngbModalCreateRole.componentInstance.title = 'Role';
135       ngbModalCreateRole.componentInstance.dialogState = 2;
136       ngbModalCreateRole.componentInstance.availableRole = _element;
137       ngbModalCreateRole.componentInstance.appRoleFunctions = availableRoleFunctions;
138       ngbModalCreateRole.componentInstance.appId = this.selectedCentralizedApp;
139       ngbModalCreateRole.componentInstance.passBackAddRolePopup.subscribe((_result: any) => {
140         this.showSpinner = true;
141         this.getAppRoles(_result);
142       }, (_reason: any) => {
143         return;
144       });
145     }, (error) => {
146       this.showSpinner = false;
147     });
148   }
149
150   addRoleModalPopup() {
151     const ngbModalCreateRole = this.ngbModal.open(AddRoleComponent);
152     ngbModalCreateRole.componentInstance.title = 'Role';
153     ngbModalCreateRole.componentInstance.appId = this.selectedCentralizedApp;
154     ngbModalCreateRole.componentInstance.passBackAddRolePopup.subscribe((_result: any) => {
155       this.showSpinner = true;
156       this.getAppRoles(_result);
157     }, (_reason: any) => {
158       return;
159     });
160
161   }
162
163   removeRole(_element) {
164     if ((this.selectedCentralizedApp !== 1) && (_element.name.indexOf('global_') !== -1)) {
165       const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
166       modalInfoRef.componentInstance.title = 'Confirmation';
167       modalInfoRef.componentInstance.message = 'Global role cannot be deleted.';
168     }
169     else {
170       const modalInfoRef = this.ngbModal.open(InformationModalComponent);
171       modalInfoRef.componentInstance.title = 'Confirmation';
172       modalInfoRef.componentInstance.message = 'You are about to delete the role ' + _element.name + ' . Do you want to continue?';
173       modalInfoRef.result.then((_res) => {
174         if (_res === 'Ok') {
175           var uuu = this.api.removeRole + '/' + this.selectedCentralizedApp + '/' + _element.id;
176           var postData = {
177             appId: this.selectedCentralizedApp,
178             availableRoleId: _element.id
179           };
180           this.http.post(uuu, postData).toPromise().then((data: any) => {
181             if (typeof data === 'object' && data.restCallStatus == 'Success') {
182               this.availableRoles = data.availableRoles;
183               this.roleDataSource = new MatTableDataSource(this.availableRoles);
184               this.roleDataSource.sort = this.sort;
185               this.roleDataSource.paginator = this.paginator;
186             } else {
187               const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
188               modalErrorRef.componentInstance.title = 'Error';
189               modalErrorRef.componentInstance.message = 'Failed to remove role. ' + data.error;
190             }
191           }, (_err) => {
192             const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
193             modalErrorRef.componentInstance.title = 'Error';
194             modalErrorRef.componentInstance.message = 'Error while deleting: ' + _err.error;
195           })
196         }
197       }, (_dismiss) => {
198
199       });
200     }
201   }
202
203   // getCentalizedApps
204   getCentralizedApps(userId) {
205     this.showSpinner = true;
206     this.roleService.getCentralizedApps(userId).toPromise().then((res: any) => {
207       if (res.length > 0) {
208         this.centralizedApps = res;
209         this.selectedCentralizedApp = this.centralizedApps[0].appId;
210         this.getRolesForSelectedCentralizedApp(this.centralizedApps[0].appId);
211       }
212     }).catch(err => {
213       this.showSpinner = false;
214       // $log.error('RoleListCtrl::centralizedApps retrieval error: ', err);
215     })
216   }
217
218   syncRolesFromExternalAuthSystem() {
219     this.applicationsService.syncRolesEcompFromExtAuthSystem(this.selectedCentralizedApp).toPromise().then((res: any) => {
220       if (res.status == 'OK') {
221         const modalInfoRef = this.ngbModal.open(InformationModalComponent);
222         modalInfoRef.componentInstance.title = 'Success';
223         modalInfoRef.componentInstance.message = 'Sync operation completed successfully!';
224         modalInfoRef.result.then((_res) => {
225           if (_res === 'Ok')
226             this.getRolesForSelectedCentralizedApp(this.selectedCentralizedApp);
227         }, (result) => {
228
229         })
230       } else {
231         const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
232         modalErrorRef.componentInstance.title = 'Error';
233         modalErrorRef.componentInstance.message = 'Sync operation failed for ' + this.appName + 'res.message';
234       }
235     }).catch(err => {
236       const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
237       modalErrorRef.componentInstance.title = 'Error';
238       modalErrorRef.componentInstance.message = 'Sync operation failed for ' + this.appName + 'err.message';
239     });
240   };
241
242   getRolesForSelectedCentralizedApp(val) {
243     this.showSpinner = true;
244     this.availableRoles = [];
245     this.roleDataSource = new MatTableDataSource(this.availableRoles);
246     this.applicationsService.getSingleAppInfoById(val).subscribe((res: any) => {
247       this.appName = res.name;
248       if (res.rolesInAAF == true) {
249         this.syncRolesApplied = true;
250       }
251     });
252     this.getAppRoles(val);
253   }
254
255   private getAppRoles(val: any) {
256     this.roleService.getRoles(val).subscribe((data: any) => {
257       if (data) {
258         var j = data;
259         j = JSON.parse(j.data);
260         this.availableRoles = j.availableRoles;
261         this.roleDataSource = new MatTableDataSource(this.availableRoles);
262         this.roleDataSource.sort = this.sort;
263         this.roleDataSource.paginator = this.paginator;
264         this.showSpinner = false;
265       }
266       else {
267         this.showSpinner = false;
268         const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
269         modalErrorRef.componentInstance.title = 'Error';
270         modalErrorRef.componentInstance.message = 'Failed to get ' + this.appName + ' roles. Please try again later!';
271       }
272     }, (error: HttpErrorResponse) => {
273       this.showSpinner = false;
274       const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
275       modalErrorRef.componentInstance.title = 'Error';
276       modalErrorRef.componentInstance.message = 'Failed to get ' + this.appName + ' roles. Please try again later!';
277     });
278   }
279 }