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