3df58daa6040ed1330418e7a19960e52d16cefdc
[portal.git] / ecomp-portal-FE-common / client / app / views / users / new-user-dialogs / new-user.controller.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 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 /**
39  * Created by nnaffar on 12/20/15.
40  */
41 'use strict';
42 (function () {
43     class NewUserModalCtrl {
44         constructor($scope, $log, usersService, applicationsService, confirmBoxService, items) {
45             var extRequestValue = false;
46             var isSystemUser = false;
47
48             $scope.ngRepeatDemo = [
49                         {id: 'userButton', value: 'true', labelvalue: 'user'},
50                         {id: 'systemUserButton', value: 'false', labelvalue: 'system'}
51                     ]
52             
53             $scope.selectedvalueradioButtonGroup = {
54                                 type: 'true'
55                             }
56             
57             
58             let init = () => {
59                 //$log.info('NewUserModalCtrl::init');
60                 this.isSaving = false;
61                 this.anyChanges = false;
62                 this.adminApps = [];
63                 this.isGettingAdminApps = false;
64                 if(items && items.selectedUser && items.dialogState){
65                     this.selectedUser = items.selectedUser;
66                     this.dialogState = items.dialogState;
67                     this.isShowBack = false;
68                     if(this.dialogState === 3){
69                         this.getUserAppsRoles();
70                     }
71                 }else{
72                     this.isShowBack = true;
73                     this.selectedUser = null;
74                     this.dialogState = 1;
75                 }
76             };
77
78             this.appChanged = (index) => {
79                 let myApp = this.adminApps[index];
80                 //$log.debug('NewUserModalCtrl::appChanged: index: ', index, '; app id: ', myApp.id, 'app name: ',myApp.name);
81                 myApp.isChanged = true;
82                 this.anyChanges = true;
83             }
84
85             this.deleteApp = (app) => {
86                 let appMessage = this.selectedUser.firstName + ' ' + this.selectedUser.lastName;
87                 confirmBoxService.deleteItem(appMessage).then(isConfirmed => {
88                     if(isConfirmed){
89                         this.anyChanges = true;
90                         app.isChanged = true;
91                         app.isDeleted = true; // use this to hide the app in the display
92                         app.appRoles.forEach(function(role){
93                             role.isApplied = false;
94                         });
95                     }
96                 }).catch(err => {
97                     $log.error('NewUserModalCtrl::deleteApp error: ',err);
98                     confirmBoxService.showInformation('There was a problem deleting the the applications. ' +
99                         'Please try again later. Error: ' + err.status).then(isConfirmed => {});
100                 });
101             };
102
103             this.getUserAppsRoles = () => {
104                 if (!this.selectedUser || !this.selectedUser.orgUserId) {
105                     $log.error('NewUserModalCtrl::getUserAppsRoles error: No user is selected');
106                     this.dialogState = 1;
107                     return;
108                 }
109                 $log.debug('NewUserModalCtrl::getUserAppsRoles: about to call getAdminAppsSimpler');
110                 this.isGettingAdminApps = true;
111                 applicationsService.getAdminAppsSimpler().then((apps) => {
112                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: beginning of then for getAdminAppsSimpler');
113                     this.isGettingAdminApps = false;
114                     if (!apps || !apps.length) {
115                         $log.error('NewUserModalCtrl::getUserAppsRoles error: no admin apps found');
116                         return null;
117                     }
118                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: then for getAdminAppsSimpler: step 2');
119                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: admin apps: ', apps);
120                     this.adminApps = apps;
121                     this.dialogState = 3;
122                     this.userAppRoles = {};
123                     this.numberAppsProcessed = 0;
124                     this.isLoading = true;
125                     apps.forEach(app => {
126                         //$log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
127                         // Keep track of which app has changed, so we know which apps to update using a BE API
128                         app.isChanged = false;
129                         // Each of these specifies a state, which corresponds to a different message and style that gets displayed
130                         app.isLoading = true;
131                         app.isError = false;
132                         app.isDeleted = false;
133                         app.printNoChanges = false;
134                         app.isUpdating = false;
135                         app.isErrorUpdating = false;
136                         app.isDoneUpdating = false;
137                         app.errorMessage = "";
138                         if($scope.selectedvalueradioButtonGroup.type == 'false')
139                                 {
140                                    isSystemUser = true;
141                                 }
142                         usersService.getUserAppRoles(app.id, this.selectedUser.orgUserId, extRequestValue,isSystemUser).promise().then((userAppRolesResult) => {
143                             //$log.debug('NewUserModalCtrl::getUserAppsRoles: got a result for app: ',app.id,': ',app.name,': ',userAppRolesResult);
144                             app.appRoles = userAppRolesResult;
145                             app.isLoading = false;
146                             for(var i=0;i<app.appRoles.length;i++){
147                                 
148                                 if(app.appRoles[i].roleName.indexOf('global_')!=-1){
149                                         app.appRoles[i].roleName='*'+app.appRoles[i].roleName;
150                                                         
151                                                 }
152                                 }
153
154                         }).catch(err => {
155                             $log.error(err);
156                             app.isError = true;
157                             app.isLoading = false;
158                             app.errorMessage = err.headers('FEErrorString');
159                             //$log.debug('NewUserModalCtrl::getUserAppsRoles: in new-user.controller: response header: '+err.headers('FEErrorString'));
160                         }).finally(()=>{
161                             this.numberAppsProcessed++;
162                             if (this.numberAppsProcessed === this.adminApps.length) {
163                                 this.isLoading = false;
164                             }
165                         });
166                     })
167                     return;
168                 }).catch(err => {
169                     $log.error(err);
170                 })
171
172             }
173
174             /**
175              * Update the selected user apps with the new roles.
176              * If no roles remain, set the user to inactive.
177              */
178             this.updateUserAppsRoles = () => {
179                 // $log.debug('NewUserModalCtrl::updateUserAppsRoles: entering updateUserAppsRoles');
180                 if(!this.selectedUser || !this.selectedUser.orgUserId || !this.adminApps){
181                     $log.error('NewUserModalCtrl::updateUserAppsRoles: mmissing arguments');
182                     return;
183                 }
184                 this.isSaving = true;
185                 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: going to update user: ' + this.selectedUser.orgUserId);
186                 this.numberAppsProcessed = 0;
187                 this.numberAppsSucceeded = 0;
188                 this.adminApps.forEach(app => {
189                     if (app.isChanged) {
190                         //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have changed; going to update: id: ', app.id, '; name: ', app.name);
191                         app.isUpdating = true;
192                         for(var i=0;i<app.appRoles.length;i++){
193                           if(app.appRoles[i].roleName.indexOf('*global_')!=-1){
194                                         app.appRoles[i].roleName=app.appRoles[i].roleName.replace('*','');
195                                                         
196                                                 }
197                             }
198                         if($scope.selectedvalueradioButtonGroup.type == 'false')
199                         {
200                            isSystemUser = true;
201                         }else{
202                                 isSystemUser = false;
203                         }
204                         var newUserAppRoles = {
205                                 orgUserId: this.selectedUser.orgUserId,
206                                 appId: app.id, 
207                                 appRoles: app.appRoles,
208                                 appName: app.name,
209                                 isSystemUser : isSystemUser
210                         }; 
211                         usersService.updateUserAppRoles(newUserAppRoles).promise()
212                         .then(res => {
213                             //$log.debug('NewUserModalCtrl::updateUserAppsRoles: User app roles updated successfully on app: ',app.id);
214                             app.isUpdating = false;
215                             app.isDoneUpdating = true;
216                             this.numberAppsSucceeded++;
217                         }).catch(err => {
218                                 $log.error(err);                       
219                             var errorMessage = 'Failed to update the user application roles: ' + err;
220                             if(err.status == 504){ 
221                                 this.numberAppsSucceeded++;
222                                 errorMessage = 'Request is being processed, please check back later!';
223                             } else{
224                                 app.isErrorUpdating = true;
225                             }
226                             confirmBoxService.showInformation(
227                                         errorMessage)
228                                         .then(isConfirmed => {});                       
229                         }).finally(()=>{
230                             this.numberAppsProcessed++;
231                             if (this.numberAppsProcessed === this.adminApps.length) {
232                                 this.isSaving = false; // hide the spinner
233                             }
234                             if (this.numberAppsSucceeded === this.adminApps.length) {
235                                 $scope.$dismiss('cancel');//close and resolve dialog promise with true (to update the table)
236                             }
237                         })
238                     } else {
239                         //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have NOT changed; NOT going to update: id: ', app.id, '; name: ', app.name);
240                         app.noChanges = true;
241                         app.isError = false; //remove the error message; just show the No Changes messages
242                         this.numberAppsProcessed++;
243                         this.numberAppsSucceeded++;
244                         if (this.numberAppsProcessed === this.adminApps.length) {
245                             this.isSaving = false; // hide the spinner
246                         }
247                         if (this.numberAppsSucceeded === this.adminApps.length) {
248                                 $scope.$dismiss('cancel');//close and resolve dialog promise with true (to update the table)
249                         }
250                     }
251                 });
252             };
253
254             /**
255              * Navigate between dialog screens using step number: 1,2,...
256              */
257             this.navigateBack = () => {
258                 if (this.dialogState === 1) {
259                     //back from 1st screen?
260                 }
261                 if (this.dialogState === 3) {
262                     this.dialogState = 1;
263                 }
264             };
265
266             init();
267
268             $scope.$on('$stateChangeStart', e => {
269                 //Disable navigation when modal is opened
270                 //**Nabil - note: this will cause the history back state to be replaced with current state
271                 e.preventDefault();
272             });
273         }
274     }
275     NewUserModalCtrl.$inject = ['$scope', '$log', 'usersService', 'applicationsService', 'confirmBoxService', 'items'];
276     angular.module('ecompApp').controller('NewUserModalCtrl', NewUserModalCtrl);
277 })();