Added portal-FE-os components
[portal.git] / portal-FE-os / src / app / pages / users / new-user-modal / new-user-modal.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, Input, EventEmitter, Output } from '@angular/core';
39 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
40 import { ApplicationsService, UsersService } from 'src/app/shared/services';
41 import { MatTableDataSource, MatRadioChange } from '@angular/material';
42 import { UserAdminApps, UserAccessRoles } from 'src/app/shared/model';
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 { HttpErrorResponse } from '@angular/common/http';
46
47 @Component({
48   selector: 'app-new-user-modal',
49   templateUrl: './new-user-modal.component.html',
50   styleUrls: ['./new-user-modal.component.scss']
51 })
52 export class NewUserModalComponent implements OnInit {
53   @Input() dialogState: number;
54   @Input() userTitle: string;
55   @Input() disableBack: boolean;
56   @Input() adminModalData: any;
57   @Input() userModalData: any;
58   @Output() passBackNewUserPopup: EventEmitter<any> = new EventEmitter();
59   changedSelectedUser: any;
60   showSpinner: boolean;
61   isGettingAdminApps: boolean;
62   adminApps: any;
63   modelSelectedRoles: any;
64   appRoles: Array<UserAccessRoles>;
65   numberAppsProcessed: number;
66   isSystemUserCheck = false;
67   extRequestValue = false;
68   searchTitleText = 'Enter First Name, Last Name or Org-ID';
69   placeholderText = 'Search';
70   ngRepeatDemo = [
71     { id: 'userButton', value: 'true', labelValue: 'User' },
72     { id: 'systemUserButton', value: 'false', labelValue: 'System' }
73   ]
74   selectedvalueradioButtonGroup = {
75     type: 'true'
76   }
77   userRadioSearchButton = this.ngRepeatDemo[0].labelValue;
78   displayedColumns: string[] = ['applications', 'roles', 'delete'];
79   userAdminAppsSource = new MatTableDataSource(this.adminApps);
80   showAppSpinner: boolean;
81   isError: boolean;
82   errorMessage: any;
83   originalSelectedRoles: any;
84   numberAppsSucceeded: number;
85   anyChanges: boolean;
86   orgUserIdValue: string;
87   constructor(public ngbModal: NgbModal, public activeModal: NgbActiveModal, private applicationsService: ApplicationsService, private usersService: UsersService) { }
88
89   ngOnInit() {
90     this.appRoles = [];
91     this.adminApps = [];
92     this.anyChanges = false;
93     this.modelSelectedRoles = [];
94     this.originalSelectedRoles = [];
95     this.changedSelectedUser = null;
96     if (this.dialogState === 2) {
97       this.changedSelectedUser = this.userModalData;
98       this.orgUserIdValue = this.changedSelectedUser.orgUserId;
99       this.getUserAppsRoles();
100     }
101   }
102
103   addUserFlag : boolean = false;
104
105   addNewUser(newUserFlag : boolean){
106     console.log("New user flag ", newUserFlag);
107     this.addUserFlag = true;
108   }
109
110   changeSelectedUser(user: any) {
111     this.changedSelectedUser = user;
112     if (this.changedSelectedUser.firstName === undefined || this.changedSelectedUser.lastName === undefined) {
113       this.userTitle = this.changedSelectedUser;
114       this.orgUserIdValue = this.changedSelectedUser;
115     }
116     else {
117       this.orgUserIdValue = this.changedSelectedUser.orgUserId;
118       this.userTitle = `${this.changedSelectedUser.firstName}, ` + ` ${this.changedSelectedUser.lastName} ` + ` (${this.changedSelectedUser.orgUserId})`;
119     }
120   }
121
122   searchUserRadioChange($event: MatRadioChange) {
123     if ($event.value === 'System') {
124       this.searchTitleText = 'Enter System UserID';
125       this.placeholderText = 'xxxxxx@org.com';
126       this.isSystemUserCheck = true;
127     } else {
128       this.searchTitleText = 'Enter First Name, Last Name or ATTUID';
129       this.placeholderText = 'Search';
130       this.isSystemUserCheck = false;
131     }
132   }
133
134   navigateBack() {
135     this.anyChanges = false;
136     this.dialogState = 1;
137   }
138
139   roleSelectChange(element: any) { // update this.adminApps list when user select roles from dropdown
140     let existingSelectedRoles = this.modelSelectedRoles;
141     this.modelSelectedRoles = [];
142     this.anyChanges = true;
143     this.adminApps.forEach(_item => {
144       if (_item.id === element.id) {
145         _item['isChanged'] = true;
146       }
147       let appRoleList = _item.appRoles;
148       if (appRoleList != undefined) {
149         appRoleList.forEach(_itemRole => {
150           if (_itemRole.appId === element.id) {
151             const index = existingSelectedRoles.indexOf(_itemRole);
152             if (index != -1) {
153               _itemRole.isApplied = true;
154             } else {
155               _itemRole.isApplied = false;
156             }
157           }
158           if (_itemRole.isApplied)
159             this.modelSelectedRoles.push(_itemRole);
160         });
161       }
162     });
163   }
164
165   getUserAppsRoles() {
166     if (this.changedSelectedUser === undefined) {
167       this.dialogState = 1;
168       return;
169     }
170     this.isGettingAdminApps = true;
171     this.applicationsService.getAdminApps().subscribe((apps: Array<UserAdminApps>) => {
172       this.isGettingAdminApps = false;
173       if (!apps || !apps.length) {
174         return null;
175       }
176       this.adminApps = apps;
177       this.dialogState = 2;
178       this.numberAppsProcessed = 0;
179       this.showSpinner = true;
180       apps.forEach(app => {
181         //$log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
182         // Keep track of which app has changed, so we know which apps to update using a BE API
183         app['isChanged'] = false;
184         // Each of these specifies a state, which corresponds to a different message and style that gets displayed
185         app['showSpinner'] = true;
186         app['isError'] = false;
187         app['isDeleted'] = false;
188         app['printNoChanges'] = false;
189         app['isUpdating'] = false;
190         app['isErrorUpdating'] = false;
191         app['isDoneUpdating'] = false;
192         app['errorMessage'] = "";
193         this.usersService.getUserAppRoles(app.id, this.orgUserIdValue, this.extRequestValue, this.isSystemUserCheck).toPromise().then((userAppRolesResult) => {
194           app['appRoles'] = userAppRolesResult;
195           app['showSpinner'] = false;
196           for (var i = 0; i < app['appRoles'].length; i++) {
197             app['appRoles'][i]['appId'] = app.id;
198             if (app['appRoles'][i].roleName.indexOf('global_') != -1) {
199               app['appRoles'][i].roleName = '*' + app['appRoles'][i].roleName;
200             }
201             if (app['appRoles'][i].isApplied)
202               this.modelSelectedRoles.push(app['appRoles'][i]);
203           }
204         }).catch((err: HttpErrorResponse) => {
205           app['isError'] = true;
206           app['showSpinner'] = false;
207           if (err.status == 200 || err.message.toLowerCase().includes("rollback"))
208             app['errorMessage'] = 'Error: ' + 500;
209           else
210             app['errorMessage'] = 'Error: ' + err.status;
211         }).finally(() => {
212           this.numberAppsProcessed++;
213           if (this.numberAppsProcessed === this.adminApps.length) {
214             this.originalSelectedRoles = this.modelSelectedRoles;
215             this.showSpinner = false;
216           }
217         });
218       })
219       this.userAdminAppsSource = new MatTableDataSource(this.adminApps);
220     }, (_err) => {
221
222     })
223   }
224
225   updateUserAppsRoles() {
226     this.anyChanges = false;
227     if (!this.changedSelectedUser ||
228         (this.changedSelectedUser.orgUserId == undefined && !this.isSystemUserCheck) ||
229           !this.adminApps) {
230       return;
231     }
232     this.showSpinner = true;
233     this.numberAppsProcessed = 0;
234     this.numberAppsSucceeded = 0;
235     this.adminApps.forEach(app => {
236       if (app.isChanged) {
237         app.isUpdating = true;
238         for (var i = 0; i < app.appRoles.length; i++) {
239           if (app.appRoles[i].roleName.indexOf('*global_') != -1) {
240             app.appRoles[i].roleName = app.appRoles[i].roleName.replace('*', '');
241           }
242         }
243         var newUserAppRoles = {
244           orgUserId: this.orgUserIdValue,
245           appId: app.id,
246           appRoles: app.appRoles,
247           appName: app.name,
248           isSystemUser: this.isSystemUserCheck
249         };
250         this.usersService.updateUserAppRoles(newUserAppRoles).toPromise()
251           .then((res: any) => {
252             if (res.status && res.status.toLowerCase() === 'error') {
253               const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
254               modalErrorRef.componentInstance.message = "Error: " + res.message;
255             }
256             app.isUpdating = false;
257             app.isDoneUpdating = true;
258             this.numberAppsSucceeded++;
259           }).catch(err => {
260             var errorMessage = 'Failed to update the user application roles: ' + err;
261             if (err.status == 504) {
262               this.numberAppsSucceeded++;
263               errorMessage = 'Request is being processed, please check back later!';
264             } else {
265               app.isErrorUpdating = true;
266             }
267             const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
268             modalErrorRef.componentInstance.message = errorMessage;
269           }).finally(() => {
270             this.numberAppsProcessed++;
271             if (this.numberAppsProcessed === this.adminApps.length) {
272               this.showSpinner = false; // hide the spinner
273             }
274             if (this.numberAppsSucceeded === this.adminApps.length) {
275               this.passBackNewUserPopup.emit();
276               this.activeModal.close('Close');//close and resolve dialog promise with true (to update the table)
277             }
278           })
279       } else {
280         app.noChanges = true;
281         app.isError = false; //remove the error message; just show the No Changes messages
282         this.numberAppsProcessed++;
283         this.numberAppsSucceeded++;
284         if (this.numberAppsProcessed === this.adminApps.length) {
285           this.showSpinner = false; // hide the spinner
286         }
287         if (this.numberAppsSucceeded === this.adminApps.length) {
288           this.activeModal.close('Close');
289         }
290       }
291     });
292   }
293
294   deleteApp(app) {
295     let appMessage = this.changedSelectedUser.firstName + ' ' + this.changedSelectedUser.lastName;
296     const ngbModalConfirm = this.ngbModal.open(InformationModalComponent);
297     ngbModalConfirm.componentInstance.title = 'Confirmation';
298     ngbModalConfirm.componentInstance.message = 'Are you sure you want to delete ' + appMessage;
299     ngbModalConfirm.result.then((_res) => {
300       if (_res === 'Ok') {
301         this.anyChanges = true;
302         app.isChanged = true;
303         app.isDeleted = true; // use this to hide the app in the display
304         app.appRoles.forEach((role) => {
305           role.isApplied = false;
306         });
307         // remove app roles if user app delete option is selected
308         this.modelSelectedRoles.forEach((_role, index) => {
309           if (_role.appId === app.id) this.modelSelectedRoles.splice(index, 1);
310         })
311         this.roleSelectChange(app);
312       }
313     }).catch(err => {
314       // $log.error('NewUserModalCtrl::deleteApp error: ',err);
315       const ngbModalError = this.ngbModal.open(InformationModalComponent);
316       ngbModalError.componentInstance.title = 'Error';
317       ngbModalError.componentInstance.message = 'There was a problem deleting the the applications. ' +
318         'Please try again later. Error: ' + err.status;
319     });
320   }
321
322 }