Merge "Add proxy support in DockerFile"
[portal.git] / ecomp-portal-FE-os / client / src / views / catalog / add-catalog-dialogs / new-catalog.controller.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 /**
21  * Created by nnaffar on 12/20/15.
22  */
23 'use strict';
24 (function () {
25     class NewCatalogModalCtrl {
26         constructor($scope, $log, usersService, catalogService, applicationsService, confirmBoxService) {
27             let init = () => {
28                 //$log.info('NewUserModalCtrl::init');
29                 this.isSaving = false;
30                 this.adminApps =[];
31                 this.appRoles = [];
32                 $scope.userAppRoles = [];
33                 $scope.titleText = "Request for Access in MyLogins:";
34                 $scope.title ="Request is pending in MyLogins for the following Roles";
35                 this.isGettingAdminApps = false;
36                 if($scope.ngDialogData && $scope.ngDialogData.selectedUser && $scope.ngDialogData.dialogState){
37                     this.selectedUser = $scope.ngDialogData.selectedUser;
38                     this.dialogState = $scope.ngDialogData.dialogState;
39                     this.isShowBack = false;
40                     if(this.dialogState === 2){
41                         this.getUserAppsRoles();
42                     }
43                 }
44             };
45
46             this.appChanged = (index) => {
47                 let myApp = this.adminApps[index];
48                 //$log.debug('NewUserModalCtrl::appChanged: index: ', index, '; app id: ', myApp.id, 'app name: ',myApp.name);
49                 myApp.isChanged = true;
50                 this.anyChanges = true;
51             }
52             
53             this.deleteApp = (app) => {
54                 let appMessage = this.selectedUser.firstName + ' ' + this.selectedUser.lastName;
55                 confirmBoxService.deleteItem(appMessage).then(isConfirmed => {
56                     if(isConfirmed){
57                         this.anyChanges = true;
58                         app.isChanged = true;
59                         app.isDeleted = true; // use this to hide the app in the display
60                         app.appRoles.forEach(function(role){
61                             role.isApplied = false;
62                         });
63                     }
64                 }).catch(err => {
65                     $log.error('NewUserModalCtrl::deleteApp error: ',err);
66                     confirmBoxService.showInformation('There was a problem deleting the the applications. ' +
67                         'Please try again later. Error: ' + err.status).then(isConfirmed => {});
68                 });
69             };
70       
71             this.getUserAppsRoles = () => {
72
73                 //$log.debug('NewUserModalCtrl::getUserAppsRoles: about to call getAdminAppsSimpler');
74                 this.isGettingAdminApps = true;
75                 catalogService.getAppCatalog().then((apps) => {
76                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: beginning of then for getAdminAppsSimpler');
77                         
78                         this.isGettingAdminApps = false;
79                     if (!apps || !apps.length) {
80                         $log.error('NewUserModalCtrl::getUserApps error: no admin apps found');
81                         return null;
82                     }
83                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: then for getAdminAppsSimpler: step 2');
84                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: admin apps: ', apps);
85                     
86                     this.dialogState = 2; 
87                     this.isLoading = true;
88                     this.adminApps = apps;
89                    
90                                 catalogService.getuserAppRolesCatalog(this.selectedUser.headerText).then(
91                                                 function(result) {
92                                                         // $log.debug('CatalogCtrl:storeSelection result is ', result);
93                                                         
94                                                         $scope.userAppRoles = result;
95                                                         $scope.displyUserAppCatalogRoles = true;
96                                                         if(result.length === 1 && result[0].requestedRoleId === null)
97                                                                 {
98                                                                 $scope.title= "Removal Request is pending in MyLogins";
99                                                                 }
100                                                         
101                                                            if($scope.userAppRoles.length==0)
102                                                                 {
103                                                                 $scope.displyUserAppCatalogRoles = false;
104                                                                 }
105                                                 });
106                     apps.forEach(app => {
107                         if(app.name === this.selectedUser.headerText){
108                         //$log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
109                         // Keep track of which app has changed, so we know which apps to update using a BE API
110                         app.isChanged = false;
111                         // Each of these specifies a state, which corresponds to a different message and style that gets displayed
112                         app.isLoading = true;
113                         app.isError = false;
114                         app.isDeleted = false;
115                         app.printNoChanges = false;
116                         app.isUpdating = false;
117                         app.isErrorUpdating = false;
118                         app.isDoneUpdating = false;
119                         app.errorMessage = "";
120                         usersService.getUserAppRoles(app.id, this.selectedUser.attuid).promise().then((userAppRolesResult) => {
121                             //$log.debug('NewUserModalCtrl::getUserAppsRoles: got a result for app: ',app.id,': ',app.name,': ',userAppRolesResult);
122                                 app.appRoles = userAppRolesResult;
123                                 app.isLoading = false;
124                         }).catch(err => {
125                             $log.error(err);
126                             app.isError = true;
127                             app.isLoading = false;
128                             app.errorMessage = err.headers('FEErrorString');
129                             //$log.debug('NewUserModalCtrl::getUserAppsRoles: in new-user.controller: response header: '+err.headers('FEErrorString'));
130                         }).finally(()=>{
131                             this.numberAppsProcessed++;
132                             if (this.numberAppsProcessed === this.adminApps.length) {
133                                 this.isLoading = false;
134                             }
135                         });
136                         }
137                     })
138                     return;
139                 }).catch(err => {
140                     $log.error(err);
141                 })
142
143             }
144            
145                         
146             /**
147              * Update the selected user apps with the new roles.
148              * If no roles remain, set the user to inactive.
149              */
150             this.updateUserAppsRoles = () => {
151                 // $log.debug('NewUserModalCtrl::updateUserAppsRoles: entering updateUserAppsRoles');
152                 if(!this.selectedUser || !this.selectedUser.attuid || !this.adminApps){
153                     $log.error('NewUserModalCtrl::updateUserAppsRoles: mmissing arguments');
154                     return;
155                 }
156                 this.isSaving = true;
157                 confirmBoxService.makeUserAppRoleCatalogChanges('Are you sure you want to make these  changes?')
158                                             .then(confirmed => {
159                                                 if(confirmed === true){
160                 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: going to update user: ' + this.selectedUser.attuid);
161                 this.numberAppsProcessed = 0;
162                 this.numberAppsSucceeded = 0;
163                 this.adminApps.forEach(app => {
164                     if (app.isChanged) {
165                         //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have changed; going to update: id: ', app.id, '; name: ', app.name);
166                         app.isUpdating = true;
167                         var UserAppRolesRequest = {
168                                 attuid: this.selectedUser.attuid,
169                                 appId: app.id, 
170                                 appRoles: app.appRoles,
171                                 appName: app.name
172                         };                 
173                         this.isSaving = true;
174                         $log.debug('going to update user: ' + this.selectedUser.attuid + ' with app roles: ' + JSON.stringify(this.adminAppsRoles));
175                                 applicationsService.saveUserAppsRoles(UserAppRolesRequest).then(res => {
176                                          app.isUpdating = false;
177                                      $scope.closeThisDialog(true);
178                                 }).catch(err => {
179                                     $log.error('NewAdminModalCtrl.updateAdminAppsRoles:: Failed - ' + err);
180                                 }).finally(()=> {
181                                     this.isSaving = false;
182                                 })
183                         }
184                 });
185                                 }else{
186                                         this.isSaving = false;
187                                 }
188                     
189
190            
191                 });
192                 
193             };
194
195             /**
196              * Navigate between dialog screens using step number: 1,2,...
197              */
198             this.navigateBack = () => {
199                 if (this.dialogState === 1) {
200                     //back from 1st screen?
201                 }
202                 if (this.dialogState === 3) {
203                     this.dialogState = 1;
204                 }
205             };
206
207             init();
208
209             $scope.$on('$stateChangeStart', e => {
210                 //Disable navigation when modal is opened
211                 //**Nabil - note: this will cause the history back state to be replaced with current state
212                 e.preventDefault();
213             });
214         }
215     }
216     NewCatalogModalCtrl.$inject = ['$scope', '$log', 'usersService', 'catalogService', 'applicationsService', 'confirmBoxService'];
217     angular.module('ecompApp').controller('NewCatalogModalCtrl', NewCatalogModalCtrl);
218 })();